Spring Cloud Gateway -- 路由断言工厂

路由断言工厂(Route Predicate Factory)

Spring Cloud Gateway的路由匹配的功能是以Spring WebFlux中的Handler Mapping为基础实现的。
Spring Cloud Gateway也是由许多的路由断言工厂组成的。
Http Request请求进入Spring Cloud Gateway的时候,网关路由断言工厂对请求进行断言匹配。匹配成功允许进入,失败则返回错误信息。

1、After Route Predicate Factory

After Route Predicate Factory 中获取一个UTC时间格式的参数, 当请求的当前时间在配置的UTC时间之后,则会成功匹配,否则不能成功匹配。

application.yml配置如下:

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

该路由匹配所有UTC时间Jan 20, 2019 17:42后的请求,并且路由到uri:https://example.org

2、Before Route Predicate Factory

Before Route Predicate Factory 中获取一个UTC时间格式的参数, 当请求的当前时间在配置的UTC时间之前,则会成功匹配,否则不能成功匹配。
application.yml

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

该路由匹配所有UTC时间Jan 20, 2019 17:42之前的请求,并且路由到uri:https://example.org

3、Between Route Predicate Factory

Between Route Predicate Factory 中获取一个UTC时间格式的参数, 当请求的当前时间在配置的UTC时间之间,则会成功匹配,否则不能成功匹配。
application.yml

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

如果在这个区间,就可以正常匹配路由并访问。

4、Cookie Route Predicate Factory

Cookie Route Predicate Factory 会取两个参数(Header中以“Cookie”命名的名称,对应的Key和Value)。当请求携带的cookie和Cookie断言工厂配置的一致,则路由匹配成功,否则匹配失败。
application.yml

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

这个路由将匹配cookie中存在chocolate=ch.p的请求。

5、Header Route Predicate Factory

Header Route Predicate Factory 根据配置的路由Header信息进行断言匹配路由,匹配成功进行转发,否则不进行转发。
application.yml

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

这个路由将匹配请求头中包含X-Request-Id且值为一个或者多个数字的请求

6、Host Route Predicate Factory

Host Route Predicate Factory 根据配置的Host,对请求中的Host进行断言处理,断言匹配成功进行转发,否则不进行转发。
application.yml

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

这个路由将匹配请求的Host头为www.somehost.org or beta.somehost.org or www.anotherhost.org.

7 、Method Route Predicate Factory

Method Route Predicate Factory 根据路由信息配置的Method对请求方式是Get或者Post等进行断言匹配,断言匹配成功进行转发,否则不进行转发
application.yml

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

改路由匹配HttpMethod为Get的请求

8、Path Route Predicate Factory

Path Route Predicate Factory 基于Spring PathMatcher 模式的路径匹配路由

application.yml
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Path=/foo/{segment},/bar/{segment}

如有请求地址如 /foo/1 or /foo/bar or /bar/baz,那么将匹配该路由

9、Query Route Predicate Factory

Query Route Predicate Factory 根据请求中的两个参数进行断言匹配,断言匹配成功进行转发,否则不进行转发。
application.yml

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

当前路由将匹配路径参数包含foo=baz的请求

10、RemoteAddr Route Predicate Factory

RemoteAddr Route Predicate Factory 配置一个IPv4或者IPv6网段的字符串或者IP。当请求IP地址在网段之内或者和配置的IP相同,断言匹配成功进行转发,否则不进行转发。
application.yml

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

请求发起端ip为192.168.1.1~192.168.1.255的请求

你可能感兴趣的:(Spring Cloud Gateway -- 路由断言工厂)