Spring Cloud Gateway practice 4: summary of built-in predicate

Welcome to my GitHub

https://github.com/zq2599/blog_demos

Content: classification and summary of all original articles and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc;

Overview of this article

  • This article is the fourth in the Spring Cloud Gateway practice series. Let's summarize the types of existing predicate s. In today's content, in addition to the simplified configuration officially recommended, we also give the JSON format configuration of the assertion during dynamic routing;

After

  • < font color = "blue" > after < / font > indicates that the route takes effect after the specified time
  • In the configuration file, pay attention to the format of the time string, < font color = "blue" > + 08:00 < / font > indicates the East eighth District:
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: http://127.0.0.1:8082
        predicates:
        - After=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
  • For JSON format of dynamic routing, note that < font color = "blue" > args < / font > parameters should use < font color = "red" > datetime < / font >:
[
    {
        "id": "after_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "After",
                "args": {
                    "datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]"
                }
            }
        ]
    }
]

Before

  • < font color = "blue" > before < / font > indicates that the route takes effect before the specified time
  • Profile:
spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: http://127.0.0.1:8082
        predicates:
        - Before=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
  • For JSON format of dynamic routing, note that < font color = "blue" > args < / font > parameters should use < font color = "red" > datetime < / font >:
[
    {
        "id": "before_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Before",
                "args": {
                    "datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]"
                }
            }
        ]
    }
]

Between

  • < font color = "blue" > between < / font > indicates that the route takes effect within the specified time period. Since the time period is two parameters, pay attention to their writing
  • Profile:
spring:
  application:
    name: hello-gateway
  cloud:
    gateway:
      routes:
        - id: between_route
          uri: http://127.0.0.1:8082
          predicates:
            - Between=2021-08-16T07:36:00.000+08:00[Asia/Shanghai], 2021-08-16T08:15:00.000+08:00[Asia/Shanghai]
  • JSON format of dynamic routing. Note the < font color = "blue" > args < / font > parameter. The start time is < font color = "red" > datetime1 < / font >, and the end time is < font color = "red" > datetime2 < / font >:
[
    {
        "id": "path_route_addr",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Between",
                "args": {
                    "datetime1": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]",
                    "datetime2": "2021-08-16T08:18:00.000+08:00[Asia/Shanghai]"
                }
            }
        ]
    }
]

Cookie

  • < font color = "blue" > cookie < / font > indicates that the cookie has the specified name and the corresponding value conforms to the specified regular expression
  • Profile:

    spring:
    cloud:
      gateway:
        routes:
        - id: cookie_route
          uri: https://example.org
          predicates:
          - Cookie=chocolate, ch.p
  • JSON format of dynamic routing. Note the < font color = "blue" > args < / font > parameters:
[
    {
        "id": "cookie_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Cookie",
                "args": {
                    "name": "chocolate",
                    "regexp": "ch.p"
                }
            }
        ]
    }
]

Header

  • < font color = "blue" > header < / font > indicates that the header has the specified name and the corresponding value conforms to the specified regular expression
  • The following example requires that < font color = "blue" > x-request-id < / font > must exist in the header, and the value must be a number
  • Profile:
spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+
  • For JSON format of dynamic routing, note that the < font color = "blue" > args < / font > parameters are < font color = "red" > header < / font > and < font color = "red" > regexp < / font >, and also note that there are two backslashes (escape problem) in the value of regexp:
[
    {
        "id": "header_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Header",
                "args": {
                    "header": "X-Request-Id",
                    "regexp": "\\d+"
                }
            }
        ]
    }
]
  • The parameters and results of postman test are as follows:

Host

  • < font color = "blue" > host < / font > indicates that the requested host must match the specified string and the corresponding value conforms to the specified regular expression. You can specify multiple host matching expressions at the same time. The following examples are given to two, of which the first specifies the port:
  • Profile:
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://127.0.0.1:8082
        predicates:
        - Host=test.com:8081,**.anotherhost.org
  • For the JSON format of dynamic routing, pay attention to the < font color = "blue" > args < / font > parameter. In addition, through the actual measurement, it is found that the value of regex here is a regular expression. Therefore, multiple host s in the above configuration file should be realized here through the writing of regular expression (the writing of JSON array always has exceptions during deserialization and cannot be parsed successfully):
[
    {
        "id": "header_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Host",
                "args": {
                    "regex": "test.com:8086"
                }
            }
        ]
    }
]

Method

  • < font color = "blue" > method < / font > is very easy to understand and matches the specified method type (there can be multiple)
  • Profile:
spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://127.0.0.1:8082
        predicates:
        - Method=GET,POST
  • The JSON format of dynamic routing. Similarly, due to personal level problems, only the JSON writing method of specifying a single method is practiced for the time being. If you know how to specify a method, please let me know. Thank you:
[
    {
        "id": "method_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Method",
                "args": { 
                    "methods": "GET"
                }
            }
        ]
    }
]

Path

  • < font color = "blue" > Path < / font > is commonly used to match the specified method type (there can be multiple)
  • For the configuration file, note < font color = "blue" > {segment} < / font >, which means that the real value of the location can be extracted and used in the filter. This will be explained in subsequent filter articles:
spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: http://127.0.0.1:8082
        predicates:
        - Path=/hello/{segment},/lbtest/{segment}
  • The JSON format of dynamic routing. Similarly, due to personal level problems, only the JSON writing method of specifying a single method is practiced for the time being. If you know how to specify a method, please let me know. Thank you:
[
    {
        "id": "path_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Path",
                "args": { 
                    "pattern": "/hello/{segment}"
                }
            }
        ]
    }
]

Query

  • < font color = "blue" > query < / font > matches whether the request contains a specified parameter. It can also be matched only when the parameter is equal to the specified value (regular expression)
  • The configuration file is matched as long as it has a request parameter named < font color = "blue" > name < / font >
spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://127.0.0.1:8082
        predicates:
        - Query=name
  • As shown below, you can also specify that the value of the name parameter must be < font color = "blue" > AAA. < / font >, and this decimal point indicates that a character is matched, such as < font color = "blue" > name = aaa1 < / font > or < font color = "blue" > name = AAA2 < / font >:
spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://127.0.0.1:8082
        predicates:
        - Query=name,aaa.
  • For the JSON format of dynamic routing, note that the parameter name and parameter value are set with < font color = "blue" > param < / font > and < font color = "blue" > regexp < / font > respectively:
[
    {
        "id": "query_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Query",
                "args": { 
                    "param": "name",
                    "regexp": "aaa."
                }
            }
        ]
    }
]
  • The tests are as follows:

RemoteAddr

  • < font color = "blue" > remoteaddr < / font > it's easy to understand and match the request from the specified source
  • Profile:
spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: http://127.0.0.1:8082
        predicates:
        - RemoteAddr=192.168.50.134/24
  • For the JSON format of dynamic routing, note that the parameter name and parameter value are set with < font color = "blue" > param < / font > and < font color = "blue" > regexp < / font > respectively:
[
    {
        "id": "remoteaddr_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "RemoteAddr",
                "args": { 
                    "sources": "192.168.50.134/24"
                }
            }
        ]
    }
]
  • The test is as follows. During the test, the host address should not be < font color = "blue" > localhost < / font > and < font color = "blue" > 127.0.0.1 < / font >, which will cause the network card address obtained by the server when judging the source to be 0.0.0.0:

Weight

  • < font color = "blue" > weight < / font > as the name suggests, requests are distributed to different locations according to weight
  • Profile:
spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: http://192.168.50.134:8082
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: http://192.168.50.135:8082
        predicates:
        - Weight=group1, 2
  • The above are the commonly used assertion types. It can be seen that the function is very powerful. I hope I can give you some reference

You're not alone. Xinchen's original accompanies you all the way

  1. Java series
  2. Spring collection
  3. Docker series
  4. kubernetes series
  5. Database + middleware series
  6. DevOps series

Welcome to the official account: programmer Xin Chen

Wechat search "programmer Xinchen", I'm Xinchen, looking forward to traveling with you in the Java World
https://github.com/zq2599/blog_demos

Tags: cloud computing

Posted on Tue, 16 Nov 2021 18:44:21 -0500 by abo28