SpringCloud gateway谓词
1、After Route Predicate Factory
After路由谓词工厂接受一个参数,一个日期时间(它是一个java ZonedDateTime)。此谓词匹配在指定日期时间之后发生的请求。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: http://www.baidu.com
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- After=2020-01-20T17:42:47.789-07:00[Asia/Shanghai]
filters:
- StripPrefix=1
如果时间在2020-01-20T17:42:47.789-07:00[Asia/Shanghai]之后就会处理请求。访问http://localhost:8500/跳转到百度首页。将时间改成2030-01-20T17:42:47.789-07:00[Asia/Shanghai],访问http://localhost:8500/报404错误。
2、The Before Route Predicate Factory
Before路由谓词工厂接受一个参数,一个日期时间(它是一个java ZonedDateTime)。此谓词匹配在指定日期时间之前发生的请求。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: http://www.baidu.com
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Before=2030-01-20T17:42:47.789-07:00[Asia/Shanghai]
filters:
- StripPrefix=1
如果时间在2030-01-20T17:42:47.789-07:00[Asia/Shanghai]之前就会处理请求。访问http://localhost:8500/跳转到百度首页。将时间改成2010-01-20T17:42:47.789-07:00[Asia/Shanghai],访问http://localhost:8500/报404错误。
3、The Between Route Predicate Factory
Between路由谓词工厂接受两个参数,datetime1和datetime2,这两个参数是java ZonedDateTime对象。此谓词匹配发生在datetime1之后和datetime2之前的请求。datetime2参数必须在datetime1之后。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: http://www.baidu.com
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Between=2010-01-20T17:42:47.789-07:00[Asia/Shanghai],2030-01-20T17:42:47.789-07:00[Asia/Shanghai]
filters:
- StripPrefix=1
如果时间在2010-01-20T17:42:47.789-07:00[Asia/Shanghai]到2030-01-20T17:42:47.789-07:00[Asia/Shanghai]之间则会处理请求。访问http://localhost:8500/跳转百度首页。将时间改成2025-01-20T17:42:47.789-07:00[Asia/Shanghai],2030-01-20T17:42:47.789-07:00[Asia/Shanghai],访问http://localhost:8500/报404.
4、The Cookie Route Predicate Factory
Cookie路由谓词工厂接受两个参数,即Cookie名称和regexp(这是一个Java正则表达式)。此谓词匹配具有给定名称且其值与正则表达式匹配的cookie。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: http://www.baidu.com
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Cookie=chocolate, ch.p
filters:
- StripPrefix=1
用Postman调接口:
可以跳转到百度首页。
将cookie值改为ch.p34在访问报404。
5、The Header Route Predicate Factory
Header路由谓词工厂接受两个参数,Header和regexp(这是一个Java正则表达式)。此谓词与具有给定名称的标头匹配,该名称的值与正则表达式匹配。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: http://www.baidu.com
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Header=X-Request-Id, \d+
filters:
- StripPrefix=1
用postman调用接口:
可以访问百度首页。将123改成123we,在访问报404.
6、The Host Route Predicate Factory
主机路由谓词工厂接受一个参数:主机名模式列表。该模式是Ant样式的模式。作为分离器。此谓词与匹配模式的Host请求头相匹配。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: http://www.baidu.com
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Host=**.somehost.org,**.anotherhost.org
filters:
- StripPrefix=1
用postman调用:
可以跳转百度首页。将Host头的值改成beta.somehost.org和www.anotherhost.org,也能访问。
7、The Method Route Predicate Factory
方法路由谓词工厂接受一个方法参数,该参数是一个或多个参数:要匹配的HTTP方法。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: http://www.baidu.com
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Method=GET,POST
filters:
- StripPrefix=1
用Postman调用,以get和post分别调用都能跳转百度首页。改成PUT方式报404。
8、 The Path Route Predicate Factory
路径路由谓词工厂采用两个参数:一个是Spring PathMatcher模式列表,另一个是名为matchTrailingFlash的可选标志(默认为true)。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/**
filters:
- StripPrefix=1
如果访问路径是/producer/**模式就会转发到producer服务,lb表示负载均衡。启动EurekaServer,两个producer服务。同时在gateway模块加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
gateway负载均衡是使用spring-cloud-starter-loadbalancer实现的。
访问http://localhost:8500/producer/hello,可以看到端口出现变化。
9、 The Query Route Predicate Factory
查询路由谓词工厂接受两个参数:一个必需的param和一个可选的regexp(这是一个Java正则表达式)。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Query=green
filters:
- StripPrefix=1
只有查询参数有green就会处理请求。访问http://localhost:8500/producer/hello报404。访问http://localhost:8500/producer/hello?green=123,成功。
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Query=red, gree.
filters:
- StripPrefix=1
查询参数名为red,参数值匹配正则表达式gree.
的请求才会被处理。访问http://localhost:8500/producer/hello?red=green,成功。访问http://localhost:8500/producer/hello?red=grenn报404.
10、The RemoteAddr Route Predicate Factory
RemoteAddr路由谓词工厂采用源列表(最小大小为1),这些源是CIDR表示法(IPv4或IPv6)字符串,例如192.168.0.1/16(其中192.168.0.1是IP地址,16是子网掩码)。例如:
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- RemoteAddr=192.168.1.1/24
filters:
- StripPrefix=1
如果请求的远程地址为,例如192.168.1.10,则此路由匹配。
11、The Weight Route Predicate Factory
Weight路由谓词工厂接受两个参数:group和Weight(一个int)。权重按每组计算。例如:
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: http://localhost:8002
predicates:
- Weight=group1, 8
filters:
- StripPrefix=1
- id: weight_low
uri: http://localhost:8004
predicates:
- Weight=group1, 2
filters:
- StripPrefix=1
启动了两个producer,一个端口是8002,一个端口是8004。访问http://localhost:8500/producer/hello这个地址10次,端口是8002的producer接收请求8次,端口是8004的producer接收请求2次。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?