Predicate of Spring Cloud Gateway

1. Introduction to assertions

The predict in the gateway is inspired by the new feature of Java 8 util. function. Predicate can be understood as: forwarding can only be carried out when the conditions are met. If there are multiple predicates, forwarding can only be carried out when all conditions are met

2. Assertion type

  1. After: matches requests that occur after the specified date and time.
  2. Before: matches requests that occur before the specified date.
  3. Between: you need to specify two date parameters and set a time interval to match the requests within this time interval.
  4. Cookie: you need to specify two parameters, namely name and regexp (regular expression). You can also understand Key and Value to match the cookie with a given name and its Value matches the regular expression.
  5. Header: two parameters are required: header and regexp (regular expression), which can also be understood as Key and Value. The matching request carries information.
  6. Host: matches whether the current request is from the set host.
  7. Method: one or more parameters can be set to match HTTP requests, such as GET and POST
  8. Path: matches the requests under the specified path, which can be multiple, separated by commas
  9. Query: you need to specify one or more parameters, a mandatory parameter and an optional regular expression. Whether the first parameter is included in the matching request. If there are two parameters, whether the value of the first parameter in the matching request conforms to the regular expression.
  10. RemoteAddr: matches the specified IP or IP segment, and forwards if it meets the conditions.
  11. Weight: two parameters, group and weight (int), are required to realize the routing weight function. The route in the same group is selected according to the routing weight

3. demonstration of common assertions

We can't demonstrate the above assertions one by one. We'll pick out some commonly used ones to demonstrate to you. These specific demonstrations are available on the official website. The specific address is: Spring Cloud Gateway

1. After

Indicates that forwarding is not performed until the configured time

spring.cloud.gateway.routes[0].id=nacos-provider
spring.cloud.gateway.routes[0].uri=lb://nacos-provider
# Access path Path=/test/**
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**
# And the request will not be forwarded until After time
spring.cloud.gateway.routes[0].predicates[1]=After=2022-04-16T12:02:30.000+08:00[Asia/Shanghai] 

2. Cookie

Format: Cookie=Cookie Name,Value Regexp

  1. Cookie Name, which is a required option. You need to fill in the name of the cookie
  2. Value Regexp, which is optional and needs to be filled in the regular expression of the value. If it is not filled in, it is judged that there is a Cookie Name and the matching is successful
spring.cloud.gateway.routes[0].id=nacos-provider
spring.cloud.gateway.routes[0].uri=lb://nacos-provider
# Access path Path=/test/**
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**
# And the request will not be forwarded until After time
#spring.cloud.gateway.routes[0].predicates[1]=After=2022-04-16T12:02:30.000+08:00[Asia/Shanghai] 
# And match the key and value of the cookie. Here, it means that if the cookie is username and the value is lowercase, it will match
spring.cloud.gateway.routes[0].predicates[1]=Cookie=username,[a-z]+ 

3. Header

The principle of Header and Cookie is the same, but one acts on Cookie and the other on Header.

spring.cloud.gateway.routes[0].id=nacos-provider
spring.cloud.gateway.routes[0].uri=lb://nacos-provider
# Access path Path=/test/**
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**
# And match the key and value of the Header. Here, it means that there is a match when the cookie is X-Request-Id and the value is a number
spring.cloud.gateway.routes[0].predicates[1]=Header=X-Request-Id,\d+

4. Host

Match whether the current request comes from the set host. * * matching is supported, as shown below

spring.cloud.gateway.routes[0].id=nacos-provider
spring.cloud.gateway.routes[0].uri=lb://nacos-provider
# Access path Path=/test/**
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**
# And match the request header Host, which means that the Host is * * test.com
spring.cloud.gateway.routes[0].predicates[1]=Host=**host:8888

Non * * host:888 cannot be accessed

5. Method

This is even simpler. Matching request methods, such as GET/POST/HEAD, etc

spring.cloud.gateway.routes[0].id=nacos-provider
spring.cloud.gateway.routes[0].uri=lb://nacos-provider
# Access path Path=/test/**
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**
# And the request method is GET
spring.cloud.gateway.routes[0].predicates[1]=Method=GET

Enter the address between browsers to open it, that is, GET, which can be accessed

Jmeter uses POST to access the same address. If it cannot be accessed, it returns 404

6. Query

Matching request parameters, like Head and Cookie, the first parameter is the parameter name, which is a required item, and the second parameter is the regular expression of the matching value, which is an optional item. If there is no second parameter, the request with the query parameter as the configuration name is matched

spring.cloud.gateway.routes[0].id=nacos-provider
spring.cloud.gateway.routes[0].uri=lb://nacos-provider
# Access path Path=/test/**
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**
spring.cloud.gateway.routes[0].predicates[1]=Query=username,[a-z]+

7. Weight

This predicate assertion is used to implement multiple instances of back-end services. Some instances need more or less forwarding traffic to achieve different weights. Therefore, it is meaningful to configure multiple routes. If a route is always 100%, two parameters group and weight (int) are required. The configuration is as follows:

# The first set of configuration, weight 20%
spring.cloud.gateway.routes[0].id=nacos-provider8081
spring.cloud.gateway.routes[0].uri=http://localhost:8081
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**
spring.cloud.gateway.routes[0].predicates[1]=Weight=nacos-provider-weight-group,2
# The first set of configuration, weight 80%
spring.cloud.gateway.routes[1].id=nacos-provider8083
spring.cloud.gateway.routes[1].uri=http://localhost:8083
spring.cloud.gateway.routes[1].predicates[0]=Path=/test/**
spring.cloud.gateway.routes[1].predicates[1]=Weight=nacos-provider-weight-group,8


As can be seen from the GIF diagram, most of the traffic is allocated to port 8083.

4. Summary

Predict is to implement a set of matching rules and let the request come and find the corresponding Route for processing.

Tags: Java Microservices

Posted by paulrichards19 on Sat, 16 Apr 2022 13:27:18 +0930