【笔记】Route Predicate Factories(路由谓词工厂)

本文介绍了SpringCloudGateway中用于路由决策的谓词工厂,包括After、Before、Between用于时间条件匹配,Cookie、Header、Host、Method针对请求属性过滤,Path、Query处理路径和查询参数,RemoteAddr基于源IP地址筛选,以及Weight进行路由权重分配。
摘要由CSDN通过智能技术生成

1.The After Route Predicate Factory

After路由谓词工厂接受一个参数,一个日期时间(它是一个java ZonedDateTime)。此谓词匹配在指定日期时间之后发生的请求。

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=red, gree.

2.The Before Route Predicate Factory

Before路由谓词工厂接受一个参数,一个日期时间(它是一个java ZonedDateTime)。此谓词匹配在指定日期时间之前发生的请求

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

3.The Between Route Predicate Factory

Between路由谓词工厂接受两个参数,datetime1和datetime2,它们是java ZonedDateTime对象。此谓词匹配发生在datetime1之后和datetime2之前的请求。datetime2参数必须在datetime1之后。

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

4.The Cookie Route Predicate Factory

Cookie路由谓词工厂接受两个参数,即Cookie名称和regexp(这是一个Java正则表达式)。此谓词匹配具有给定名称且其值与正则表达式匹配的cookie

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

5.The Header Route Predicate Factory

Header路由谓词工厂接受两个参数,Header和regexp(这是一个Java正则表达式)。此谓词与具有给定名称的标头匹配,该名称的值与正则表达式匹配。

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+

6.The Host Route Predicate Factory

Host路由谓词工厂接受一个参数:主机名模式列表。该模式是Ant样式的模式。作为分离器。此谓词与匹配模式的Host标头相匹配。

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org

7.The Method Route Predicate Factory

Method路由谓词工厂接受一个方法参数,该参数是一个或多个参数:要匹配的HTTP方法

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST

8.The Path Route Predicate Factory

路径路由谓词工厂采用两个参数:Spring PathMatcher模式列表和名为matchTrailingFlash的可选标志(默认为true)。

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

9.The Query Route Predicate Factory

Query路由谓词工厂接受两个参数:一个必需的param和一个可选的regexp(它是Java正则表达式)。

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green

10.The RemoteAddr Route Predicate Factory

RemoteAddr路由谓词工厂获取源的列表(最小大小1),这些源是CIDR表示法(IPv4或IPv6)字符串,例如192.168.0.1/16(其中192.168.0.1是IP地址,16是子网掩码)

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24

11.The Weight Route Predicate Factory

Weight路由谓词工厂接受两个参数:group和Weight(一个int)。重量按每组计算。

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值