內置Predicate

參考:官方文檔

https://www.cnblogs.com/babycomeon/p/11161073.html

Spring Cloud版本:Hoxton.SR5

Spring Cloud Gateway版本:2.2.3.RELEASE

1 簡介

Spring Cloud Gateway內置了很多Predicate,用來制定路由匹配規則。

Predicate來源於Java 8,是Java 8中引入的一個函數,Predicate接受一個輸入參數,返回一個布爾值結果。該接口包含多種默認方法來將Predicate組合成其他複雜的邏輯(比如:與、或、非)。可以用於接口請求參數校驗、判斷新老數據是否有變化需要進行更新操作。

Spring Cloud GatewaySpring利用Predicate的特性實現了各種路由匹配規則,有通過Header、請求參數等不同的條件來進行作爲條件匹配到對應的路由。

下圖總結了內置的Predicate

在這裏插入圖片描述

2 內置Predicate

2.1 匹配請求時間

2.1.1 AfterRoutePredicateFactory

AfterRoutePredicateFactory可以配置一個時間datetime,類型是ZonedDateTime。這個Predicate會匹配那些在設定的時間之後到達的請求。

ZonedDateTimeJava 8中日期時間功能類,用於表示帶時區的日期與時間信息的類,ZonedDateTime支持通過時區來設置時間,中國的時區是:Asia/Shanghai

下面是在application.yml文件中配置該Predicate的例子:

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2020-06-26T00:00:00+08:00[Asia/Shanghai]

2.1.2 BeforeRoutePredicateFactory

BeforeRoutePredicateFactory配置一個datetime時間。這個Predicate會匹配那些在設定時間之前到達的請求。

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2020-06-26T00:00:00+08:00[Asia/Shanghai]

2.1.3 BetweenRoutePredicateFactory

BetweenRoutePredicateFactory可以配置一個時間段。這個Predicate會匹配在時間段之間到達的請求。

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        - Between=2020-06-26T00:00:00+08:00[Asia/Shanghai], 2020-06-27T00:00:00+08:00[Asia/Shanghai]

2.2 匹配Cookie

2.2.1 CookieRoutePredicateFactory

CookieRoutePredicateFactory接受兩個參數,一個是Cookie的名稱name,一個是正則表達式regexp。該Predicate將會根據name獲取請求中對應的Cookie值,並與regexp正則表達式進行匹配,匹配上了就會路由該請求。

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=sessionId, c52e52258f86b61ab25fa0268be34f08

2.3 匹配Header

2.3.1 HeaderRoutePredicateFactory

HeaderRoutePredicateFactory接受兩個參數,一個是Header中的屬性名name,一個是正則表達式regexp。該Predicate將會根據name獲取請求的Header中對應的屬性名,並與regexp正則表達式進行匹配,匹配上了就會路由該請求。

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

2.4 匹配Host

2.4.1 HostRoutePredicateFactory

HostRoutePredicateFactory接收一組參數,一組域名模板。這個模板是一個 Ant風格的模板,用.號作爲分隔符,組與組之間用,分隔。它將會去匹配請求Header中的Host屬性值。

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

如上的配置,www.somehost.orgbeta.somehost.orgwww.anotherhost.org將會匹配成功。

2.5 匹配請求方法

2.5.1 MethodRoutePredicateFactory

MethodRoutePredicateFactory接受一組請求方法,包括GETPOSTPUTDELETE等。該Predicate將會匹配被指定的請求方法的請求。

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

2.6 匹配請求路徑

2.6.1 PathRoutePredicateFactory

PathRoutePredicateFactory接受一組參數,一個path列表。該Predicate匹配列表中的path

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

/red/1/red/blue/blue/green將會被匹配。

2.7 匹配請求參數

2.7.1 QueryRoutePredicateFactory

QueryRoutePredicateFactory接受兩個參數,一個是參數名name,一個是正則表達式regexp。該Predicate將會匹配指定名稱的參數值。

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

也可以只指定參數名。如上,這樣請求中有參數green的,都將會匹配成功。

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

如上,將會匹配請求中的參數green的值,滿足pu.表達式。

2.8 匹配請求者的ip地址

2.8.1 RemoteAddrRoutePredicateFactory

RemoteAddrRoutePredicateFactory接受一組地址,地址是cidr符號(IPv4IPv6 )字符串,例如 192.168.0.1/16 (其中 192.168.0.1IP 地址,16 是子網掩碼)。

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

如上的配置,192.168.1.10IP地址將會被匹配。

2.9 匹配路由權重(Weight)

2.9.1 WeightRoutePredicateFactory

WeightRoutePredicateFactory接受兩個參數,一個是分組名,一個是權重(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=group2, 2

基於以上配置,將會有80%的請求路由到https://weightHigh.org20%的請求路由到https://weightLow.org

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章