Gateway 常用的 Predicate
Gateway 常用的 Predicate
上面用的path 只是predicate 中的一个,只要使predicates为true 就访问,false不访问。

Route PredicateFactories
Gateway 常用的 Predicate
SpringCloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。
SpringCloud Gateway包括许多内置的RoutePredicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个Route Predicate工厂可以进行组合。

SpringCloud Gateway创建Route对象时,使用RoutePredicateFactory创建Predicate对象,Predicate对象可以赋值给Route。SpringCloud Gateway包括许多内置的Route Predicate Factories。

所有这些谓词都匹配HTTP请求的不同属性。多种谓词工厂可以组合,并通过逻辑and。

有哪几种?
Gateway 常用的 Predicate
After、Before、Between

yml 配置
Gateway 常用的 Predicate
但这是美国时间,需要转成亚洲时间,转换:
Gateway 常用的 Predicate
替换成这个时间,并往后调一小时
Gateway 常用的 Predicate
只有在这个点之后才能被访问,否则是false,不能访问。那我们来看一下访问情况
Gateway 常用的 Predicate
After是在时间之后,Before是在时间之前,Between是在之间,需要传两个时间,就不详细写了

Cookie
Gateway 常用的 Predicate
修改yml:
Gateway 常用的 Predicate
重启9527,使用curl 命令测试(curl是postman 图形化界面的命令)
启动 cmd命令界面

不带cookie访问:
Gateway 常用的 Predicate
404 not found不让访问,再来看带cookie:
Gateway 常用的 Predicate
这样,时间上匹配且带着cookie就能访问了。

Header

Gateway 常用的 Predicate
修改 yml
Gateway 常用的 Predicate
重启9527,使用curl 命令测试
Gateway 常用的 Predicate

Host
Gateway 常用的 Predicate
修改yml
Gateway 常用的 Predicate
重启,测试
Gateway 常用的 Predicate

Method
Gateway 常用的 Predicate
只有请求方法是GET方法才允许访问

Path
Gateway 常用的 Predicate
Query
Gateway 常用的 Predicate
修改ym
Gateway 常用的 Predicate
重启,测试
Gateway 常用的 Predicate
小总结

说白了,Predicate就是为了实现一组匹配规则,让请求过来找对应的Route进行处理。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2021-07-18
  • 2021-11-19
  • 2021-06-13
  • 2021-07-01
猜你喜欢
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2021-05-15
  • 2021-05-16
  • 2022-12-23
相关资源
相似解决方案