时间断言工厂(ZonedDateTime.now())
AfterRoutePredicateFactory
接收一个日期参数,判断请求日期 是否在指定日期之后
路由配置
server:
port: 8088
spring:
application:
name: api-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8847
username: nacos
password: nacos
gateway:
routes:
- id: order_route # 路由唯一标识
#uri: http://localhost:8020 # 需要转发的地址
uri: lb://order-service # 需要转发的地址
# 断言规则 用于路由规则的匹配
predicates:
- After=2024-01-01T23:00:11.518+08:00[GMT+08:00]
# http://localhost:8088/order-serv/order/add => http://localhost:8020/order-serv/order/add
filters:
- StripPrefix=1 # 转发去掉第一层路径
# http://localhost:8020/order-serv/order/add => http://localhost:8020/order/add
访问效果
- After=2024-01-01T23:00:11.518+08:00[GMT+08:00]
- After=2024-01-01T23:00:11.518+08:00[Asia/Shanghai]
- After=2025-01-01T23:00:11.518+08:00[Asia/Shanghai]
BeforeRoutePredicateFactory
接收一个日期参数,判断请求日期 是否在指定日期之前
路由配置
server:
port: 8088
spring:
application:
name: api-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8847
username: nacos
password: nacos
gateway:
routes:
- id: order_route # 路由唯一标识
#uri: http://localhost:8020 # 需要转发的地址
uri: lb://order-service # 需要转发的地址
# 断言规则 用于路由规则的匹配
predicates:
- Before=2024-01-01T23:00:11.518+08:00[GMT+08:00]
# http://localhost:8088/order-serv/order/add => http://localhost:8020/order-serv/order/add
filters:
- StripPrefix=1 # 转发去掉第一层路径
# http://localhost:8020/order-serv/order/add => http://localhost:8020/order/add
访问效果
- Before=2024-01-01T23:00:11.518+08:00[GMT+08:00]
- Before=2024-01-01T23:00:11.518+08:00[Asia/Shanghai]
- Before=2025-01-01T23:00:11.518+08:00[Asia/Shanghai]
BetweenRoutePredicateFactory
接收两个日期参数,判断请求日期 是否在指定的时间段内(先小后大)
- - Between=2024-01-01T23:00:11.518+08:00[Asia/Shanghai], 2025-01-01T23:00:11.518+08:00[Asia/Shanghai]
CooKie断言工厂
CookieRoutePredicateFactory
1. 接收两个参数,cookie名字和一个正则表达式
2. 判断请求cookie是否 具有给定的名称 且 值与正则表达式匹配
- - Cookie=chocolate, ch.p
远程地址断言工厂
RemoteAddrRoutePredicateFactory
接口一个IP地址段,判断请求主机地址是否在地址段中
- - RemoteAddr=192.168.1.1/24
Header断言工厂
HeaderRoutePredicateFactory
1. 接收两个参数,标题名称和正则表达式
2. 判断请求Header是否 具有给定的名称 且 值与正则表达式匹配
- - Header=X-Request-Id,\d+
Host断言工厂
HostRoutePredicateFactory
接收一个参数,主机名模式,判断请求的Host是否满足匹配规则(多个逗号分隔)
- - Host=**.somehost.org,**.anotherhost.org
Method断言工厂
MethodRoutePredicateFactory
接收一个参数,判断请求方法类型是否满足匹配规则(多个逗号分隔)
- - Method=GET,POST
Path断言工厂
PathRoutePredicateFactory
接收一个参数,判断请求的URL部分是否满足路径的规则(多个逗号分隔)
- - Path=/red/{segment},/blue/{segment}
Query断言工厂
QueryRoutePredicateFactory
1. 接收两个参数,参数名称和值正则表达式
2. 判断请求参数中 是否具有给定的名称 且 值与正则表达式匹配
- - Query=green 是否有green这个参数
- - Query=red, gree. 是否有red这个参数 且 值匹配gree.
路由权重断言工厂
WeightRoutePredicateFactory
接收一个[组名,权重], 然后同一组内的路由按照权重转发
路由配置
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