spring cloud gateway谓词工厂 Predicate Factory

Predicate Factory 称为谓词工厂或断言工厂
默认的工厂类都位于 org.springframework.cloud.gateway.handler.predicate 包下
根据版本不同有多有少
本文spring-cloud.version=2021.0.5 spring-cloud-gateway=3.1.4
官方文档:https://docs.spring.io/spring-cloud-gateway/reference/spring-cloud-gateway/request-predicates-factories.html
官方文档2:https://cloud.spring.io/spring-cloud-gateway/multi/multi_gateway-request-predicates-factories.html

类型

工厂类

基于时间的谓词工厂

AfterRoutePredicateFactory

时间点之后

BeforeRoutePredicateFactory

时间点之前

BetweenRoutePredicateFactory

时间段中间

请求头

CookieRoutePredicateFactory

请求头cookie

HeaderRoutePredicateFactory

请求头

HostRoutePredicateFactory

请求头Host

请求体

ReadBodyRoutePredicateFactory

请求体

请求

MethodRoutePredicateFactory

请求方式post get put等

PathRoutePredicateFactory

请求路径

QueryRoutePredicateFactory

请求查询参数

RemoteAddrRoutePredicateFactory

远程地址,可自定义解析远程ip

XForwardedRemoteAddrRoutePredicateFactory

远程地址,通过 X-Forwarded-For 头解析远程ip

权重

WeightRoutePredicateFactory

按权重分配,group和weight

云平台

CloudFoundryRouteServiceRoutePredicateFactory

基于CloudFoundry云平台的路由分配,CloudFoundry可看做对标K8s的开源PaaS平台

一、基于时间的谓词工厂
(一)、AfterRoutePredicateFactory

这个断言接收一个时间参数(java ZonedDateTime类型),断言匹配在指定日期时间之后发生的请求。 下面是例子

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2023-09-26T08:00:00.000+08:00[Asia/Shanghai]

这个路由匹配东八区2023年9月26日8点之后的任何请求。

(二)、BeforeRoutePredicateFactory

这个断言接收一个时间参数(java ZonedDateTime类型),断言匹配在指定日期时间之前发生的请求。 下面是例子

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2023-09-26T08:00:00.000+08:00[Asia/Shanghai]

这个路由匹配东八区2023年9月26日8点之前的任何请求。

(三)、BetweenRoutePredicateFactory

这个断言接收两个时间参数(java ZonedDateTime类型),断言匹配在两个时间之前发生的请求。第二个时间参数必须大于第一个时间参数。 下面是例子

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        - Between=2023-09-26T08:00:00.000+08:00[Asia/Shanghai], 2023-09-27T08:00:00.000+08:00[Asia/Shanghai]

这个路由匹配东八区2023年9月26日8点之后且在2023年9月27日8点之前的任何请求。

二、请求头

TBD

三、请求体

TBD

四、请求

TBD

五、权重

TBD

六、云平台

(一)、CloudFoundryRouteServiceRoutePredicateFactory

基于CloudFoundry云平台的路由分配,CloudFoundry可看做对标K8s的开源PaaS平台,目前这个谓词官方文档没有解释,通过查看相关代码提交发现,这是为了支持CloudFoundry云平台里的CF Router Service组件,它判断header里面是否有属性:X-CF-Forwarded-Url、X-CF-Proxy-Signature和X-CF-Proxy-Metadata。当header里面同时有上述三个属性时,请求被转发。请求头里的url带的url是动态的。

相关github pull提交:Add predicate to detect requests made for a Cloud Foundry route service by fitzoh · Pull Request #241 · spring-cloud/spring-cloud-gateway · GitHub

相关github issue:Support CF Route Services · Issue #190 · spring-cloud/spring-cloud-gateway · GitHub

你可能感兴趣的:(java,spring,boot,spring)