• 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

    1. spring:
    2. cloud:
    3. gateway:
    4. routes:
    5. - id: after_route
    6. uri: https://example.org
    7. predicates:
    8. - After=2023-09-26T08:00:00.000+08:00[Asia/Shanghai]

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

    (二)、BeforeRoutePredicateFactory

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

    application.yml

    1. spring:
    2. cloud:
    3. gateway:
    4. routes:
    5. - id: before_route
    6. uri: https://example.org
    7. predicates:
    8. - Before=2023-09-26T08:00:00.000+08:00[Asia/Shanghai]

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

    (三)、BetweenRoutePredicateFactory

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

    1. spring:
    2. cloud:
    3. gateway:
    4. routes:
    5. - id: between_route
    6. uri: https://example.org
    7. predicates:
    8. - 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点之前的任何请求。

    二、请求头

    (一)、CookieRoutePredicateFactory

    这个断言接收两个参数,cookie名和正则表达式regexp,正则表达式是String.matche()里的参数,断言匹配cookie名对应的值符合正则表达式的请求。 下面是例子

    application.yml

    1. spring:
    2. cloud:
    3. gateway:
    4. routes:
    5. - id: cookie_route
    6. uri: https://example.org
    7. predicates:
    8. - Cookie=chocolate, ch.p

    这个路由匹配cookie名为chocolate,并且值符合正则表达式ch.p的请求。

    (二)、HeaderRoutePredicateFactory

    这个断言接收两个参数,header名和正则表达式regexp,正则表达式是String.matche()里的参数,断言匹配header名对应的值符合正则表达式的请求。 下面是例子

    application.yml

    1. spring:
    2. cloud:
    3. gateway:
    4. routes:
    5. - id: header_route
    6. uri: https://example.org
    7. predicates:
    8. - Header=X-Request-Id, \d+

    这个路由匹配Header名为X-Request-Id,并且值符合正则表达式\d+的请求。\d+正则表达式表示1个或多个0到9的数字。

    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

  • 相关阅读:
    22071华清远见(上海中心)
    学习c#的第二十三天
    让ChatGPT等模型学会自主思考!开创性技术“自主认知”框架
    前端工程化:使用 shelljs 生成 yapi 接口文件
    信必优收到中国首批成立的期货公司之一表扬信
    Excel文件读写(创建与解析)
    大数据组件Sqoop-安装与验证
    为什么禁止MyBatis批量插入几千条数据使用foreach?
    基于vue实现的资金管理系统(带权限)
    计算机类编程课学生编写的代码应该如何管理
  • 原文地址:https://blog.csdn.net/gsls200808/article/details/133323537