SpringCloud系列--Gateway(3) Predicate的使用

1、简述

1.1Route Predicate Factories是什么?

Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。

Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配,可以组合多个路由断言工厂,并通过逻辑and进行组合。

SpringCloud Gateway创建Route对象时,使用RoutePredicateFactory创建Predicate对象,Predicate对象可以赋值给Route。

Spring Cloud Gateway 提供的断言工厂有11种之多:
在这里插入图片描述

2、After路由断言工厂

After路由断言工厂有一个参数:datetime(Java的ZonedDateTime对象)。此断言匹配在指定日期时间之后发生的请求。示例如下:

示例 1. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2020-09-18T22:18:30.369+08:00[Asia/Shanghai]

这个路由匹配上海时间2020年09月18日22:18:30.369之后的任何请求。也就是说需要在这个时间之后,https://example.org这个请求的访问才有效果,在此时间之前的话会报404错误。

3、Before路由断言工厂

Before路由断言工厂有一个参数:datetime(Java的ZonedDateTime对象)。此断言匹配在指定时间之前发生的请求。示例如下:

示例 2. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2020-09-18T22:18:30.369+08:00[Asia/Shanghai]

这个路由匹配上海时间2020年09月18日22:18:30.369之前的任何请求。也就是说需要在这个时间之前,https://example.org这个请求的访问才有效果,在此时间之后的话就无法正常访问,会报404错误。

4、Between路由断言工厂

Between路由断言工厂有两个参数:datetime1和datetime2 (两个都是Java ZonedDateTime对象)。此断言匹配datetime1之后和datetime2之前发生的请求。datetime2参数必须晚于datetime1。也就是匹配datetime1到datetime2这段时间的路由请求。示例如下:

示例 3. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        - Between=2020-09-18T22:18:30.369+08:00[Asia/Shanghai], 2020-10-18T22:18:30.369+08:00[Asia/Shanghai]

这个路由上海时间2020年09月18日22:18:30.369到上海时间2020年10月18日22:18:30.369之间的任何请求相匹配。这对于维护时段可能很有用。

5、Cookie路由断言工厂

Cookie路由断言工厂采用两个参数:cookie name和regexp(Java正则表达式)。该断言匹配具有给定名称且其值与正则表达式匹配的cookie。示例如下:

示例 4. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

此路由匹配具有名称为chocolate且与ch.p正则表达式匹配的cookie的请求。

6、Header路由断言工厂

Header路由断言工厂采用两个参数:header的name和一个regexp(Java正则表达式)。该断言与具有给定名称的header匹配,该header的值与正则表达式匹配。示例如下:

示例 5. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+

如果请求中具有名为X-Request-Id、且其值与\d+正则表达式匹配的标头(即其值为一个或多个数字),则此路由匹配。

7、 Host路由断言工厂

Host路由断言工厂需要一个参数:主机名模式的列表。该模式是带有.分隔符的Ant样式的模式。当主机名与对应模式匹配时则路由匹配。示例如下:

示例 6. application.yml

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

此时此路由只匹配主机名符合**.somehost.org或者 , **.anotherhost.org的请求。

8、 Method路由断言工厂

Method路由断言工厂需要methods的参数,它可以是一个或多个参数:HTTP方法来匹配。示例如下:

示例 7. application.yml

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

此路由匹配GET或POST方法的请求。

9、 Path路由断言工厂

Path路由断言工厂有两个参数:spring PathMatcher patterns列表和一个可选的标志叫matchOptionalTrailingSeparator。示例如下:

示例 8. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

此路由匹配类似/red/1 或者 /red/blue 或者 /blue/green的请求路径。

10、 Query路由断言工厂

Query路由断言工厂采用两个参数:所要求的param和可选的regexp(Java正则表达式)。示例如下:

示例 9. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green

此路由匹配携带参数名为green的请求。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=red, gree.

此路由匹配携带参数名为red的请求,且red参数的值须与gree.正则匹配:所以值为green和greet将匹配。

11、 RemoteAddr路由断言工厂

RemoteAddr路由断言工厂需要 参数:sources列表(最少含1个),其是CIDR的表示法(IPv4或IPv6)的字符串,如192.168.0.1/16(其中192.168.0.1是一个IP地址和16一个子网掩码)。也就是访问此服务的客户端ip地址(源ip地址)。示例如下:

示例 10. application.yml

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

此路由匹配来自192.168.1.10的请求,也就是只能让ip为192.168.1.10的客户端来访问此服务。

12、 Weight 路由断言工厂

Weight 路由断言工厂有两个参数:group和weight(int类型)。权重是按组计算的。示例如下:

示例 11. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

这个路由会将约80%的流量转发至weighthigh.org,并将约20%的流量转发至weightlow.org。

Spring Cloud Gateway官网:Spring Cloud Gateway

Happy Learning ~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值