SpringCloud系列(39)--SpringCloud Gateway常用的Route Predicate

发布于:2025-06-28 ⋅ 阅读:(13) ⋅ 点赞:(0)

前言:在上一节中我们实现了SpringCloud Gateway的动态路由 ,而在本节中我们将着重介绍各种Route Predicate的作用。

1、可以到官方文档里查看常用的Route Predicate的种类

https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#the-after-route-predicate-factory

2、After Route Predicate

可以用于提前上线新功能,然后在指定时间后才能路由

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        #这里配置的意思为时间在2017-01-20 17:42:47之后才能路由
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

例如,我这里设置2024-05-11 23:15:10之后才能路由

效果图: 

3、Before route predicate
spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        #这里配置的意思时间为在2017-01-20 17:42:47之前才能路由
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

4、Between route predicate
spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        #这里配置的意思为时间在2017-01-20 17:42:47到2017-01-21 17:42:47之间才能路由
        - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

5、 Cookie route predicate

Cookie Route Predicate需要两个参数,一个是 Cookie name ,一个是正则表达式;路由规则会通过获取对应的Cookie name值和正则表达式去匹配,如果匹配上就会执行路由,如果没有匹配上则不执行

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        #这里配置的意思为请求带上指定的Cookie才能路由
        - Cookie=chocolate, ch.p

例如,我这里设置指定Cookie是username为ken时才能路由(可以用win+R唤出运行,然后输入cmd进入命令行模式,然后用curl命令来模拟请求来进行测试)

效果图:

没带Cookie时请求不通过

指定Cookie时请求通过

6、Header route predicate
spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        #这里配置的意思为请求头要有X-Requegt-Id属性并且值为整数的正则表达式才能路由
        - Header=X-Request-Id, \d+

例如,我这里设置指定Header=X-Request-Id是整数时才能路由(可以用win+R唤出运行,然后输入cmd进入命令行模式,然后用curl命令来模拟请求来进行测试)

效果图:

请求头X-Requegt-Id属性的值为字母时请求不通过

 请求头X-Requegt-Id属性的值为整数时请求通过

7、Host route predicate
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        #这里配置的意思为请求头要有Host属性并且值为**.somehost.org或**.anotherhost.org的正则表达式才能路由
        - Host=**.somehost.org,**.anotherhost.org

例如,我这里设置指定Host是为**.ken.com时才能路由(可以用win+R唤出运行,然后输入cmd进入命令行模式,然后用curl命令来模拟请求来进行测试)

效果图:

请求头Host属性的值为www.baid.com时请求不通过

 请求头Host属性的值为xxx.ken.com时请求通过

8、Method Route Predicate
spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        #这里配置的意思为请求要是GET或者POST才能路由
        - Method=GET,POST

9、Path Route Predicate
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        #这里配置的意思为请求的路径要是/red/1 或者 /red/blue 或者 /blue/green才能路由
        - Path=/red/{segment},/blue/{segment}

例:

效果图:

10、Query route predicate
spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        #这里配置的意思为请求要有参数green才能路由
        - Query=green

例:

效果图:

请求带username,请求通过

请求不带username,请求不通过


网站公告

今日签到

点亮在社区的每一天
去签到