Spring Cloud Gateway-路由谓词工厂详解(Route Predicate Factories)

本文深入探讨SpringCloud Gateway的路由谓词工厂,解释如何使用路由谓词进行灵活的流量控制,包括时间、Cookie、Header、Host等条件的配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击上方"IT牧场",选择"设为星标"技术干货每日送达!

TIPS

本文基于Spring Cloud Greenwich SR2编写,兼容Spring Cloud Finchley及更高版本。

这一节来详细探讨Spring Cloud Gateway的路由谓词工厂(Route Predicate Factories),路由谓词工厂的作用是:符合Predicate的条件,就使用该路由的配置,否则就不管。 只要掌握这一句,掌握路由谓词工厂就比较轻松了。

TIPS

Predicate是Java 8提供的一个函数式编程接口。

本文探讨了Spring Cloud Gateway中内置的谓词工厂,包括:

路由配置的两种形式

先来探讨Spring Cloud Gateway路由配置的两种姿势:

路由到指定URL

示例1:通配

spring:	
  cloud:	
    gateway:	
      routes:	
      - id: {唯一标识}	
        uri: http://www.itmuch.com

表示访问 GATEWAY_URL/** 会转发到 http://www.itmuch.com/**

TIPS

这段配置不能直接使用,需要和下面的Predicate配合使用才行。

示例2:精确匹配

spring:	
  cloud:	
    gateway:	
      routes:	
      - id: {唯一标识}	
        uri: http://www.itmuch.com/spring-cloud/spring-cloud-stream-pan-ta/

表示访问 GATEWAY_URL/spring-cloud/spring-cloud-stream-pan-ta/ 会转发到 http://www.itmuch.com/spring-cloud/spring-cloud-stream-pan-ta/

TIPS

这段配置不能直接使用,需要和下面的Predicate配合使用才行。

路由到服务发现组件上的微服务

示例1:通配

spring:	
  cloud:	
    gateway:	
      routes:	
      - id: {唯一标识}	
        uri: lb://user-center

表示访问 GATEWAY_URL/** 会转发到 user-center 微服务的 /**

TIPS

这段配置不能直接使用,需要和下面的Predicate配合使用才行。

示例2:精确匹配

spring:	
  cloud:	
    gateway:	
      routes:	
      - id: {唯一标识}	
        uri: lb://user-center/shares/1

表示访问 GATEWAY_URL/shares/1 会转发到 user-center 微服务的 /shares/1

TIPS

这段配置不能直接使用,需要和下面的Predicate配合使用才行。

谓词工厂详解

下面正式探讨路由谓词工厂。Spring Cloud Gateway提供了十来种路由谓词工厂。为网关实现灵活的转发提供了基石。

After

示例:

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: after_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当请求时的时间After配置的时间时,才会转发到用户微服务	
            # 目前配置不会进该路由配置,所以返回404	
            # 将时间改成 < now的时间,则访问localhost:8040/** -> user-center/**	
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1	
            - After=2030-01-20T17:42:47.789-07:00[America/Denver]

TIPS

•技巧:时间可使用 System.out.println(ZonedDateTime.now()); 打印,然后即可看到时区。例如:2019-08-10T16:50:42.579+08:00[Asia/Shanghai]•时间格式的相关逻辑:

•默认时间格式:org.springframework.format.support.DefaultFormattingConversionService#addDefaultFormatters•时间格式注册:org.springframework.format.datetime.standard.DateTimeFormatterRegistrar#registerFormatters


Before

示例:

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: before_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当请求时的时间Before配置的时间时,才会转发到用户微服务	
            # 目前配置不会进该路由配置,所以返回404	
            # 将时间改成 > now的时间,则访问localhost:8040/** -> user-center/**	
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1	
            - Before=2018-01-20T17:42:47.789-07:00[America/Denver]

Between

示例:

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: between_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当请求时的时间Between配置的时间时,才会转发到用户微服务	
            # 因此,访问localhost:8040/** -> user-center/**	
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1	
            - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2027-01-21T17:42:47.789-07:00[America/Denver]

Cookie

示例:

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: cookie_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当带有名为somecookie,并且值符合正则ch.p的Cookie时,才会转发到用户微服务	
            # 如Cookie满足条件,则访问http://localhost:8040/** -> user-center/**	
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1	
            - Cookie=somecookie, ch.p

Header

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: header_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当带有名为X-Request-Id,并且值符合正则\d+的Header时,才会转发到用户微服务	
            # 如Header满足条件,则访问http://localhost:8040/** -> user-center/**	
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1	
            - Header=X-Request-Id, \d+

Host

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: host_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当名为Host的Header符合**.somehost.org或**.anotherhost.org时,才会转发用户微服务	
            # 如Host满足条件,则访问http://localhost:8040/** -> user-center/**	
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1	
            - Host=**.somehost.org,**.anotherhost.org

Method

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: method_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当HTTP请求方法是GET时,才会转发用户微服务	
            # 如请求方法满足条件,访问http://localhost:8040/** -> user-center/**	
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1	
            - Method=GET

Path

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: path_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当访问路径是/users/*或者/some-path/**,才会转发用户微服务	
            # segment是一个特殊的占位符,单层路径匹配	
            # eg. 访问http://localhost:8040/users/1 -> user-center/users/1	
            - Path=/users/{segment},/some-path/**

TIPS

建议大家看下这一部分的官方文档,里面有个segment编程技巧。比较简单,留个印象。

https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_path_route_predicate_factory

Query

示例1:

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: query_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当请求带有baz的参数,才会转发到用户微服务	
            # eg. 访问http://localhost:8040/users/1?baz=xx -> user-center的/users/1	
            - Query=baz

示例2:

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: query_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当请求带有名为foo的参数,且参数值符合正则ba.,才会转发到用户微服务	
            # eg. 访问http://localhost:8040/users/1?baz=baz -> user-center的/users/1?baz=baz	
            - Query=foo, ba.

RemoteAddr

示例:

spring:	
  cloud:	
    gateway:	
      routes:	
        - id: remoteaddr_route	
          uri: lb://user-center	
          predicates:	
            # 当且仅当请求IP是192.168.1.1/24网段,例如192.168.1.10,才会转发到用户微服务	
            # eg. 访问http://localhost:8040/users/1 -> user-center的/users/1	
            - RemoteAddr=192.168.1.1/24

TIPS

建议大家看下这一部分的官方文档,有个小编程技巧。比较简单,留个印象。

https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_remoteaddr_route_predicate_factory

相关代码

640?wx_fmt=png

干货分享

最近将个人学习笔记整理成册,使用PDF分享。关注我,回复如下代码,即可获得百度盘地址,无套路领取!

•001:《Java并发与高并发解决方案》学习笔记;•002:《深入JVM内核——原理、诊断与优化》学习笔记;•003:《Java面试宝典》•004:《Docker开源书》•005:《Kubernetes开源书》•006:《DDD速成(领域驱动设计速成)》•007:全部•008:加技术讨论群

近期热文

细说 Java 主流日志工具库Spring Cloud Stream知识点盘点亚马逊实践领域驱动设计之道缓存使用过程中的几种策略总结及优缺点组合分析秒懂 QPS、TPS、PV、UV、GMV、IP、RPS!多账户的统一登录 实现全过程


想知道更多?长按/扫码关注我吧↓↓↓640?wx_fmt=jpeg>>>技术讨论群<<<喜欢就点个"在看"呗^_^


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值