Gateway route predicate factory

Predicate Factory Classification

Gateway filtering. Remove the first

in the path

1. Post moment – predicate factory

A time point can be set, and the request will be routed only when the system time is greater than the set time point.

– After=time, the date can only be accessed after the set time

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            - After=2023-08-14T15:39:21.993+08:00[Asia/Shanghai]
          filters:
            - StripPrefix=1

Test Results (current test date is 2023-07-14)

Set the date to 2023-08-14, but the date is not after the set time Result:

Set the date to 2023-06-14, and the date is after the set time. The result:

2. Pre-moment-predicate factory

A time point can be set, and the request will be routed only when the system time is greater than the set time point.

– Before=time, when the date is before the set time, it can be accessed

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            - Before=2023-06-14T15:39:21.993 + 08:00[Asia/Shanghai]
          filters:
            - StripPrefix=1

Test Results (current test date is 2023-07-14)

Set the date to 2023-08-14, and the date is before the set time. The result:

Set date For 2023-06-14, the date is not before the set time Result:

3. Time period – predicate factory

A time interval can be set, and the request will be routed only when the system time is within the set time interval.

– Between=moment, moment. Access is only possible when the date is within the set time

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            - Before=2023-06-14T15:39:21.993 + 08:00[Asia/Shanghai]
          filters:
            - StripPrefix=1

Test Results (current test date is 2023-07-14)

Set the date to 2023-06-14~2023-08-14, and the date is within the set time. The result:

4.cookie-predicate factory

Routing occurs when our request contains a cookie

– Cookie=cookie name, value (value supports regular expressions)

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            -Cookie=username,hff
          filters:
            - StripPrefix=1

Test Results

Only when the request carries a cookie and the usernme value of the cookie is hff can access be allowed

Test results without cookies

5. Request header – predicate factory

Routing will only be triggered when the HTTP request must carry the defined request header

– Header=name,value

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            -Header=token,hff
          filters:
            - StripPrefix=1

Test Results

carry request header

Does not carry request header

6. Host-verb factory

That is, we need to carry the set host information in our request header before our request can be routed, and multiple host name lists can be set.

– Host=host1,host2,host3…

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            - Host=**.zs.org,**.hff.org #** supports fuzzy matching
          filters:
            - StripPrefix=1

Test Results

carry the correct hostname

carry wrong hostname

7. Method – predicate factory

The so-called method predicate factory is to route according to the HTTP request type matching, such as GET, POST, PUT, DELETE, etc., and multiple request methods can be configured.

– Method=GET

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            -Method=GET,POST
          filters:
            - StripPrefix=1

Test Results

Test using GET or POST requests

Test without GET or POST requests

8. Path-predicate factory

-Path=path

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            -Path=/aaa/**
          filters:
            - StripPrefix=1

Test Results

Paths that meet the requirements

non-compliant path

9. Query parameter – predicate factory

That is, when our request contains a certain request parameter, routing will be performed. This setting only requires that the request parameter must carry the parameter name: [parameter name]

– Query=[parameter name]

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            - Query=hff
          filters:
            - StripPrefix=1

Test Results

set parameter request

Parameter request not set

10. RemoteAddr/network segment-routing predicate factory

That is, it only accepts requests from a certain network segment that is set, and when the request comes from a certain network segment, routing will be triggered

–RemoteAddr=192.168.200.1/24

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: lb://consumer-service
          predicates:
            - RemoteAddr=192.168.21.1/24
          filters:
            - StripPrefix=1

Test Results

Routing will only be triggered when the IP of the requester is between 192.168.21.1-192.168.21.254.

It is not the request result of the specified ip range interval

Request results in the specified ip range

11. Weight-predicate factory

The weight is supported when routing predicates, and the request can be quantified and forwarded according to the weight ratio we configured. It needs to set two parameters, one is group and the other is weight, that is, weight grouping and weight ratio.

– Weight=group, weight ratio

server:
  port: 10010
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: http://localhost:8082
          predicates:
            - Weight=testWeight, 1
        - id: consumer2
          uri: http://localhost:9090
          predicates:
            - Weight=testWeight, 2

Test Results

first

the second time

the third time

The overall probability is twice 9090 port once 8082 port