参考:官方文档
https://www.cnblogs.com/babycomeon/p/11161073.html
Spring Cloud
版本:Hoxton.SR5
Spring Cloud Gateway
版本:2.2.3.RELEASE
Spring Cloud Gateway
内置了很多Predicate
,用来制定路由匹配规则。
Predicate
来源于Java 8
,是Java 8
中引入的一个函数,Predicate
接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将Predicate
组合成其他复杂的逻辑(比如:与、或、非)。可以用于接口请求参数校验、判断新老数据是否有变化需要进行更新操作。
在Spring Cloud Gateway
中Spring
利用Predicate
的特性实现了各种路由匹配规则,有通过Header
、请求参数等不同的条件来进行作为条件匹配到对应的路由。
下图总结了内置的Predicate
:
AfterRoutePredicateFactory
可以配置一个时间datetime
,类型是ZonedDateTime
。这个Predicate
会匹配那些在设定的时间之后到达的请求。
ZonedDateTime
是Java 8
中日期时间功能类,用于表示带时区的日期与时间信息的类,ZonedDateTime
支持通过时区来设置时间,中国的时区是:Asia/Shanghai
。
下面是在application.yml
文件中配置该Predicate
的例子:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2020-06-26T00:00:00+08:00[Asia/Shanghai]
BeforeRoutePredicateFactory
配置一个datetime
时间。这个Predicate
会匹配那些在设定时间之前到达的请求。
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2020-06-26T00:00:00+08:00[Asia/Shanghai]
BetweenRoutePredicateFactory
可以配置一个时间段。这个Predicate
会匹配在时间段之间到达的请求。
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2020-06-26T00:00:00+08:00[Asia/Shanghai], 2020-06-27T00:00:00+08:00[Asia/Shanghai]
CookieRoutePredicateFactory
接受两个参数,一个是Cookie
的名称name
,一个是正则表达式regexp
。该Predicate
将会根据name
获取请求中对应的Cookie
值,并与regexp
正则表达式进行匹配,匹配上了就会路由该请求。
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=sessionId, c52e52258f86b61ab25fa0268be34f08
HeaderRoutePredicateFactory
接受两个参数,一个是Header
中的属性名name
,一个是正则表达式regexp
。该Predicate
将会根据name
获取请求的Header
中对应的属性名,并与regexp
正则表达式进行匹配,匹配上了就会路由该请求。
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
HostRoutePredicateFactory
接收一组参数,一组域名模板。这个模板是一个 Ant
风格的模板,用.
号作为分隔符,组与组之间用,
分隔。它将会去匹配请求Header
中的Host
属性值。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
如上的配置,www.somehost.org
、 beta.somehost.org
、 www.anotherhost.org
将会匹配成功。
MethodRoutePredicateFactory
接受一组请求方法,包括GET
、POST
、PUT
、DELETE
等。该Predicate
将会匹配被指定的请求方法的请求。
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
PathRoutePredicateFactory
接受一组参数,一个path
列表。该Predicate
匹配列表中的path
。
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
/red/1
、/red/blue
、/blue/green
将会被匹配。
QueryRoutePredicateFactory
接受两个参数,一个是参数名name
,一个是正则表达式regexp
。该Predicate
将会匹配指定名称的参数值。
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
也可以只指定参数名。如上,这样请求中有参数green
的,都将会匹配成功。
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green, pu.
如上,将会匹配请求中的参数green
的值,满足pu.
表达式。
RemoteAddrRoutePredicateFactory
接受一组地址,地址是cidr
符号(IPv4
或IPv6
)字符串,例如 192.168.0.1/16
(其中 192.168.0.1
是 IP
地址,16
是子网掩码)。
spring:
cloud:
gateway:
routes:
- id: remote_addr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
如上的配置,192.168.1.10
的IP
地址将会被匹配。
WeightRoutePredicateFactory
接受两个参数,一个是分组名,一个是权重(int
值)。
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weightHigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightLow.org
predicates:
- Weight=group2, 2
基于以上配置,将会有80%
的请求路由到https://weightHigh.org
,20%
的请求路由到https://weightLow.org
。