Predicate简介
- Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。
- Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个RoutePredicate工厂可以进行组合。
- Spring Cloud Gateway创建Route对象时,使用RoutePredicateFactory 创建 Predicate对象,Predicate 对象可以赋值给Route。Spring Cloud Gateway包含许多内置的Route Predicate Factories。
所有这些谓词都匹配HTTP请求的不同属性。多种谓词工厂可以组合,并通过逻辑and。
The After Route Predicate Factory(规定时间之后才能请求)
spring:
application:
name: cloud-gateway-service
cloud:
gateway:
routes:
- id: getPaymentById
uri: lb://CLOUD-PAYMENT-SERVICE
predicates:
- Path=/payment/get/**
- After=2021-12-13T17:42:47.789-07:00[America/Denver]
获得指定格式的时间戳字符串
import java.time.ZonedDateTime;
public class Test {
public static void main(String[] args) {
ZonedDateTime zbt = ZonedDateTime.now();
// 2021-12-13T17:22:35.259+08:00[Asia/Shanghai]
System.out.println(zbt);
}
}
The Between Route Predicate Factory(指定时间范围内才能请求)
spring:
application:
name: cloud-gateway-service
cloud:
gateway:
routes:
- id: getPaymentById
uri: lb://CLOUD-PAYMENT-SERVICE
predicates:
- Path=/payment/get/**
- Between=2021-12-13T17:22:35.259+08:00[Asia/Shanghai], 2021-12-13T17:42:47.789-07:00[America/Denver]