• Spring Cloud Gateway


    路由谓词工厂 Route Predicate Factory

    1. The After Route Predicate Factory        

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: after_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - After=2017-01-20T17:42:47.789-07:00[America/Denver]
    9. # 用日期时间匹配

    2. The Before Route Predicate Factory

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: before_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - Before=2017-01-20T17:42:47.789-07:00[America/Denver]
    9. # 用日期时间匹配

    3. The Between Route Predicate Factory

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: between_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
    9. # 用日期时间匹配

    4. The Cookie Route Predicate Factory        

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: cookie_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - Cookie=chocolate, ch.p
    9. # 用 cookie 名称和值匹配

    5. The Header Route Predicate Factory       

    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+
    9. # 用 header 名称和值匹配

    6. The Host Route Predicate Factory      

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: host_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - Host=**.somehost.org,**.anotherhost.org
    9. # 用 Host header 的值匹配

    7. The Method Route Predicate Factory        

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: method_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - Method=GET,POST
    9. # 用请求方法的类型匹配

    8. The Path Route Predicate Factory        

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: path_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - Path=/red/{segment},/blue/{segment}
    9. # 用请求路径的值匹配

    9. The Query Route Predicate Factory        

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: query_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - Query=green
    9. # 用查询参数的名称和值(值可以省略)匹配

    10. The RemoteAddr Route Predicate Factory        

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: remoteaddr_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - RemoteAddr=192.168.1.1/24
    9. # 用 RemoteAddr(CIDR格式,如:192.168.1.1/24) header 的值匹配

    11. The Weight Route Predicate Factory        

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: weight_high
    6.         uri: https://weighthigh.org
    7.         predicates:
    8.         - Weight=group1, 8
    9.       - id: weight_low
    10.         uri: https://weightlow.org
    11.         predicates:
    12.         - Weight=group1, 2
    13. # 用分组名称和权重匹配

    12. The XForwarded Remote Addr Route Predicate Factory        

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: xforwarded_remoteaddr_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - XForwardedRemoteAddr=192.168.1.1/24
    9. # 用 X-Forwarded-For(CIDR格式,如:192.168.1.1/24) header 值匹配

    13. 自定义路由谓词工厂:继承自 AbstractRoutePredicateFactory

    https://blog.csdn.net/abu935009066/article/details/112260405

    网关过滤器工厂 GatewayFilter Factories

    包括这些过滤器:

    添加请求头、添加请求参数、添加响应头、断路器、缓存请求体、去重响应头、回退头、JSON转gRPC、本地响应缓存、映射请求头、修改请求体、修改响应体、前缀路径、保留主机头、重定向、移除JSON属性响应体、移除请求头、移除请求参数、移除响应头、请求头大小、请求速率限制器、重写位置响应头、重写路径、重写响应头、保存会话、安全头、设置路径、设置请求头、设置响应头、设置状态、去除前缀、重试、请求大小、设置请求主机头、令牌中继等过滤器和默认过滤器。

    https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories

    路由谓词和过滤器在 yml 中的两种配置方式

    Shortcut annotation,简洁型写法:

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

    Fully expanded arguments,复杂型写法(这种方式需要详细写出每一项):

    1. spring:
    2.   cloud:
    3.     gateway:
    4.       routes:
    5.       - id: cookie_route
    6.         uri: https://example.org
    7.         predicates:
    8.         - name: Cookie
    9.            args: 
    10.               name: chocolate
    11.               regexp: ch.p

  • 相关阅读:
    04_SpringBoot整合Mybatis
    vue学习笔记23-组件事件⭐
    【linux命令讲解大全】113.网络接口和系统设备监测工具ifstat和iostat的使用
    Android13集成paho.mqtt.android启动异常
    再也不用担心因为PowerBI 可视化看板加班了——手把手详细教学
    torch、(三) Random sampling
    【题目讲解】序列变换
    SpringMVC执行流程
    GEE(11):为影像添加图例
    以哈战争或推动美国「屠杀」比特币
  • 原文地址:https://blog.csdn.net/npk191954/article/details/132834496