内置Predicate

参考:官方文档

https://www.cnblogs.com/babycomeon/p/11161073.html

Spring Cloud版本:Hoxton.SR5

Spring Cloud Gateway版本:2.2.3.RELEASE

1 简介

Spring Cloud Gateway内置了很多Predicate,用来制定路由匹配规则。

Predicate来源于Java 8,是Java 8中引入的一个函数,Predicate接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将Predicate组合成其他复杂的逻辑(比如:与、或、非)。可以用于接口请求参数校验、判断新老数据是否有变化需要进行更新操作。

Spring Cloud GatewaySpring利用Predicate的特性实现了各种路由匹配规则,有通过Header、请求参数等不同的条件来进行作为条件匹配到对应的路由。

下图总结了内置的Predicate

内置Predicate_第1张图片

2 内置Predicate

2.1 匹配请求时间

2.1.1 AfterRoutePredicateFactory

AfterRoutePredicateFactory可以配置一个时间datetime,类型是ZonedDateTime。这个Predicate会匹配那些在设定的时间之后到达的请求。

ZonedDateTimeJava 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]

2.1.2 BeforeRoutePredicateFactory

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]

2.1.3 BetweenRoutePredicateFactory

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]

2.2 匹配Cookie

2.2.1 CookieRoutePredicateFactory

CookieRoutePredicateFactory接受两个参数,一个是Cookie的名称name,一个是正则表达式regexp。该Predicate将会根据name获取请求中对应的Cookie值,并与regexp正则表达式进行匹配,匹配上了就会路由该请求。

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=sessionId, c52e52258f86b61ab25fa0268be34f08

2.3 匹配Header

2.3.1 HeaderRoutePredicateFactory

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+

2.4 匹配Host

2.4.1 HostRoutePredicateFactory

HostRoutePredicateFactory接收一组参数,一组域名模板。这个模板是一个 Ant风格的模板,用.号作为分隔符,组与组之间用,分隔。它将会去匹配请求Header中的Host属性值。

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org

如上的配置,www.somehost.orgbeta.somehost.orgwww.anotherhost.org将会匹配成功。

2.5 匹配请求方法

2.5.1 MethodRoutePredicateFactory

MethodRoutePredicateFactory接受一组请求方法,包括GETPOSTPUTDELETE等。该Predicate将会匹配被指定的请求方法的请求。

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST

2.6 匹配请求路径

2.6.1 PathRoutePredicateFactory

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将会被匹配。

2.7 匹配请求参数

2.7.1 QueryRoutePredicateFactory

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.表达式。

2.8 匹配请求者的ip地址

2.8.1 RemoteAddrRoutePredicateFactory

RemoteAddrRoutePredicateFactory接受一组地址,地址是cidr符号(IPv4IPv6 )字符串,例如 192.168.0.1/16 (其中 192.168.0.1IP 地址,16 是子网掩码)。

spring:
  cloud:
    gateway:
      routes:
      - id: remote_addr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24

如上的配置,192.168.1.10IP地址将会被匹配。

2.9 匹配路由权重(Weight)

2.9.1 WeightRoutePredicateFactory

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.org20%的请求路由到https://weightLow.org

你可能感兴趣的:(Spring,Cloud)