Spring Cloud GateWay 帮我们内置了很多 Predicates功能,实现了各种路由匹配规
则(通过 Header、请求参数等作为条件)匹配到对应的路由
1 时间点后匹配
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- After=2023-03-18T17:42:47.789-07:00[America/Denver]
2 时间点前匹配
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- Before=2025-03-18T17:42:47.789+08:00[Asia/Shanghai]
3 时间区间匹配
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- Between=2022-03-18T17:42:47.789+08:00[Asia/Shanghai],2025-03-18T17:42:47.789+08:00[Asia/Shanghai]
4 指定Cookie正则匹配指定值
这个配置的含义是当接收到的请求中包含名为
sessionId
的 Cookie,且其值匹配正则表达式\d+
(表示任意数字)时,该请求将被路由到http://example.org
。换句话说,这个规则会筛选出带有特定 Cookie 值的请求,并将其转发到指定的 URI。
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- Cookie=sessionId, \d+
5 指定Header正则匹配指定值
这个配置指定了一个名为
consumer
的路由,它将请求转发到https://consumer-service
服务上。该路由的谓词条件是请求头中的requestinfo
的值必须是一个数字。
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- Header=requestinfo, \d+
6 请求Host匹配指定值
这个配置定义了一个名为
consumer
的路由,它将请求转发到https://consumer-service
服务上。该路由的谓词条件是请求的 Host 头必须匹配**.consumer-service.**
的正则表达式模式。
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- Host=**.consumer-service.**
7 请求Method匹配指定请求方式
指定请求类型是get 或者让post请求
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- Method=GET,POST
8 请求路径正则匹配
这个配置中的
Path=/consumer-service/**
表示当请求的路径以/consumer-service/
开头时,将请求转发到https://consumer-service
服务上
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- Path=/consumer-service/**
9 请求包含某参数
在这个配置中,使用了
Query
谓词来匹配请求中的查询参数。具体来说,Query=name
表示只有请求中带有名为name
的查询参数时,该路由规则才会被匹配。这意味着只有符合该条件的请求会被转发到https://consumer-service
服务。
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- Query=name
10 远程地址匹配
在这个配置中,使用了
RemoteAddr
谓词来匹配请求的远程地址。具体来说,RemoteAddr=http://192.168.201.116
表示只有来自远程地址为192.168.201.116
的请求才会被匹配到该路由规则。这意味着只有符合该条件的请求会被转发到https://consumer-service
服务。
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: consumer
uri: https://consumer-service
predicates:
- RemoteAddr=192.168.201.116