Spring Cloud Gateway has built-in instructions for the use of various types of predicate (assertion)

SpringCloudAlibaba+Nacos integration Gateway
Spring cloud gateway combined with Sentienl to implement gateway current limiting mechanism
Spring Cloud Gateway filter factory

Spring Cloud Gateway contains many built-in routing assertion factories. All of these assertions match different attributes of the HTTP request. You can combine multiple route assertion factories with logical and statements.

1,Path Route Predicate

Path is the most common assertion request. It matches the request under the specified path. It can be a specific request or / * * can be used to match all child requests. The configuration is as follows.

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - Path=/user/**
        - Path=/wage/list

The configuration matches the request starting with / user or the URL is / wave / list. If the request from other URLs enters the system, an error will appear.

2. Datetimepredict (match request time)

2.1 after route predict

After route predict can match the time of ZonedDateTime type, which means that it matches the requests that occur after the specified date and time. The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - Path=/user/**
        - After=2022-02-26T13:00:00+08:00[Asia/Shanghai]

The configuration matches the request after 13:00:00 on 2022-02-26. If it is a request to enter the system before the specified time, an error will appear.

2.2 before route predict

Before route predict can match the time of ZonedDateTime type, which means: match the requests that occur before the specified date and time. The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - Path=/user/**
        - Before=2022-02-26T13:00:00+08:00[Asia/Shanghai]

The configuration matches the request before 13:00:00 on 2022-02-26. If it is a request to enter the system after the specified time, an error will appear.

2.3 between route predict

Between route predict can match the time of ZonedDateTime type. It is composed of two ZonedDateTime parameters. The first parameter is the start time and the second parameter is the end time. It means to match the requests that occur within the specified start time and end time. The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - Path=/user/**
        - Between=2022-02-26T13:00:00+08:00[Asia/Shanghai],2022-02-27T13:00:00+08:00[Asia/Shanghai]

The configuration matches the requests from 2022-02-26 13:00:00 to 2022-02-27 13:00:00. If it is a request to enter the system outside the specified time period, an error will appear.

3,Cookie Route Predicate

The cookie routepredicate consists of two parameters. The first parameter is the Key of the cookie and the second parameter is the Value of the cookie. It represents the request matching the cookie with the specified name and its Value matching the regular expression. The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Path=/user/**
        - Cookie=cokieName, \d+

The Key matching the cookie in the configuration is cookie name, and the value is the regular expression request satisfying \ d +. If cookie name is satisfied but not the request satisfying \ d +, an error will appear.

4,Header Route Predicate

HeaderRoutePredicate consists of two parameters. The first parameter is the name of the Header and the second parameter is the Value value of the Header, which indicates the request of the Header matching the specified name and its Value matching the regular expression. The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Path=/user/**
        - Header=headerName, \d+

In the configuration, the name of the matching Header is headerName, and the value is to meet the regular expression request of \ d +. If the headerName is satisfied but not the request of \ d +, an error will appear.

5,Host Route Predicate

The HostRoutePredicate parameter is the requested Host address. Multiple parameters are separated by commas. The set Host address can use * * to represent wildcards. The configuration is as follows:

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

The matching Host in the configuration can be matched with somehost Org or otherhost The Host address at the end of org. There will be errors in accessing other Host addresses.

6,Method Route Predicate

MethodRoutePredicate consists of one or more HTTP methods, such as POST, PUT, GET and DELETE. The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Path=/user/**
        - Method=GET,POST

The types of HTTP methods matched in the configuration are GET and POST. If it is other types of HTTP methods, an error will occur.

7,Query Route Predicate

QueryRoutePredicate consists of two parameters. The first parameter is the parameter name and the second parameter is the value of the parameter (it can meet the regular expression). It represents the parameterized request that matches the specified name and its value matches the regular expression. The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Path=/user/**
        - Query=name,\d+

The parameter name called name is matched in the configuration, and the value meets the request of \ d +. If \ d + is not met, an error will appear.

8,RemoteAddr Route Predicate

The parameters of RemoteAddrRoutePredicate are composed of CIDR notation (IPv4 or IPv6) strings. The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Path=/user/**
		- RemoteAddr=192.168.1.1/24

In the configuration, the value with IP 192.168.1.100 can be matched. If the IP rule of 192.168.1.1/24 is not met, an error will appear.

9,Weight Route Predicate

Weightaddrroutepredict is composed of group and weight, which means that the same request will be jumped to different uri addresses according to the weight. The name of the group must be the same. The configuration is as follows:

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=groupName, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=groupName, 2

This route will forward about 80% of the traffic to weight Org and forward about 20% of the traffic to weightLow org

Keywords: Java Distribution http gateway

Added by BenS on Mon, 28 Feb 2022 04:06:19 +0200