Spring Cloud Gateway Series: Routing Assertion Factory

Article directory

    • 1. After routing assertion factory
        • configuration
        • API style
    • 2. Before routing assertion factory
        • configuration
        • API style
    • 3. Between routing assertion factory
        • configuration
        • API style
    • 4. Cookie routing assertion factory
        • configuration
        • API style
    • 5. Header routing assertion factory
        • configuration
        • API style
    • 6. Host routing assertion factory
        • configuration
        • API style
    • 7. Method routing assertion factory
        • configuration
        • API style
    • 8. Path routing assertion factory
        • configuration
        • API style
    • 9. Query routing assertion factory
        • configuration
        • API style
    • 10. RemoteAddr routing assertion factory
        • configuration
        • API style
    • 11. Weight routing assertion factory
        • configuration
        • API style
    • 12. XForwardedRemoteAddr routing assertion factory
        • configuration
        • API style
    • 13. Priority

Spring Cloud Gateway uses route matching as its most basic function. This function is accomplished through the routing assertion factory. Spring Cloud Gateway contains many built-in routing assertion factories. All of these assertions can match different attributes of HTTP requests, and multiple routing assertion factories can be combined based on logic and status.

1. After routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-after-route-predicate-factory

The argument to this assertion factory is a time in UTC format. It compares the time when the request accesses the Gateway with the parameter time. If the request time is after the parameter time, the match is successful and the assertion is true.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: https://www.baidu.com
          predicates:
            - After=2027-01-20T17:42:47.789-07:00
API style
 @Bean
    public RouteLocator afterRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        //Add 3 days to the current time and set the system's default time zone to the current time zone
        ZonedDateTime dateTime = LocalDateTime.now().plusDays(3).atZone(ZoneId.systemDefault());

        return builder.routes().route("after_route", ps ->
                        ps.after(dateTime).uri("https://www.baidu.com"))
                .build();
    }

2. Before routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-before-route-predicate-factory

The argument to this assertion factory is a time in UTC format. It compares the time when the request accesses the Gateway with the parameter time. If the request time is before the parameter time, the match is successful and the assertion is true.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: https://www.baidu.com
          predicates:
            - After=2027-01-20T17:42:47.789-07:00
API style
 @Bean
    public RouteLocator beforeRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        //Add 3 days to the current time and set the system's default time zone to the current time zone
        ZonedDateTime dateTime = LocalDateTime.now().plusDays(3).atZone(ZoneId.systemDefault());

        return builder.routes().route("before_route", ps ->
                        ps.after(dateTime).uri("https://www.baidu.com"))
                .build();
    }

3. Between routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-between-route-predicate-factory

The arguments to this assertion factory are two times in UTC format. It will compare the time when the request accesses the Gateway with the two parameter times. If the request time is between the two parameter times, the match is successful and the assertion is true.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: between_route
          uri: https://www.baidu.com
          predicates:
            - Between=2021-01-20T17:42:47.789-07:00,2027-01-20T17:42:47.789-07:00
API style
 @Bean
    public RouteLocator betweenRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        ZonedDateTime endTime = LocalDateTime.now().plusDays(3).atZone(ZoneId.systemDefault());
        ZonedDateTime startTime = LocalDateTime.now().minusDays(3).atZone(ZoneId.systemDefault());
        return builder.routes().route("between_route",
                        ps -> ps.between(startTime, endTime)
                                .uri("https://www.baidu.com"))
                .build();
    }

4. Cookie routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-cookie-route-predicate-factory

The assertion factory contains two parameters, which are the key and value of the cookie. When the request carries the cookie with the specified key and value, the match is successful and the assertion is true.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: cookie_route
          uri: https://www.baidu.com
          predicates:
            - Cookie=name,robin
API style
 @Bean
    public RouteLocator cookieRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        return builder.routes().route("cookie_route",
                        ps -> ps.cookie("name", "robin").uri("https://baidu.com"))
                .build();
    }

image

5. Header routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-header-route-predicate-factory

The assertion factory contains two parameters, which are the key and value of the request header. When the request carries the header with the specified key and value, the match is successful and the assertion is true.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: header_route
          uri: https://www.baidu.com
          predicates:
            - Header=X-Request-Id,\d +
API style
 @Bean
    public RouteLocator headerRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        return builder.routes().route("header_route", ps ->
                        ps.header("X-Request-Id", "\d + ")
                                .uri("https://baidu.com"))
                .build();
    }

image

6. Host routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-host-route-predicate-factory

The parameter contained in this assertion factory is the Host attribute in the request header. When the request carries the specified Host attribute value, the match is successful and the assertion is true.

For example

Modify the hosts file in \etc and specify multiple host names for the 127.0.0.1 IP.

Add the following content to the file:

image

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: host_route
          uri: https://www.baidu.com
          predicates:
            - Host=myhost:8000,scootersoftware.com=8000
API style
 @Bean
    public RouteLocator hostRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        return builder.routes().route("host_route", ps ->
                        ps.host("myhost.com:8081","scootersoftware.com:8081")
                                .uri("https://baidu.com"))
                .build();
    }

image

7. Method routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-method-route-predicate-factory

The assertion factory is used to determine whether the request uses the specified request method, whether it is POST, GET, etc. When the specified request method is used in the request, the match is successful and the assertion is true.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: method_route
          uri: https://www.baidu.com
          predicates:
            - Method=GET,POST
API style
 @Bean
    public RouteLocator hostRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        return builder.routes().route("method_route", ps ->
                        ps.method("GET","POST")
                                .uri("https://baidu.com"))
                .build();
    }

image

8. Path routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-path-route-predicate-factory

This assertion factory is used to determine whether the request path contains the specified uri. If included, the match is successful and the assertion is true. At this time, the matched uri will be spliced to the back of the target uri to be redirected to form a unified uri.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: order_route
          uri: https://www.baidu.com
          predicates:
            - Path=/order/**
        - id: product_route
          uri: https://www.jd.com
          predicates:
            - Path=/product/**
API style
 @Bean
    public RouteLocator pathRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        return builder.routes().route("order_route", ps ->
                        ps.path("/order/**")
                                .uri("https://baidu.com"))
                .route("product_route", ps ->
                        ps.path("/product/**")
                                .uri("https://jd.com"))
                .build();
    }

image

image

9. Query routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-query-route-predicate-factory

The relationship between the above two Query assertions is “AND”, that is, only if the request contains both username and password parameters, and the username parameter value must start with ro, and the password is equal to 123456, it can be accessed.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: query_route
          uri: https://www.baidu.com
          predicates:
            - Query=username,ro. +
            - Query=password,123456
API style
 @Bean
    public RouteLocator queryRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        return builder.routes().route("query_route", ps ->
                        ps.query("username", "ro. + ")
                                .and()
                                .query("password", "123456")
                                .uri("https://baidu.com"))
                .build();
    }

image

10. RemoteAddr routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-remoteaddr-route-predicate-factory

This assertion factory is used to determine whether the IP address of the client submitting the request is in the IP range or IP list specified in the assertion. If so, the match is successful and the assertion is true.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: addr_route
          uri: https://www.baidu.com
          predicates:
            - RemoteAddr=192.168.0.1/24
API style
 @Bean
    public RouteLocator remoteAddrRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        return builder.routes().route("addr_route", ps ->
                        ps.remoteAddr("192.168.0.1/24")
                                .uri("https://baidu.com"))
                .build();
    }

Since the host IP where the current browser submits the request is 192.168.0.110, which belongs to the 192.168.0 network segment, it can be accessed.

11. Weight routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-weight-route-predicate-factory

The assertion factory is used to implement load balancing with specified weights for different URIs in the same group. The route contains two parameters, which are used to represent the group and weight. For multiple URI addresses in the same group, the router will forward the request to the corresponding URI in proportion according to the set weight.

The following route is used to specify the access weight to the two e-commerce platforms taobao.com and jd.com as 8:2. The following two routes belong to the web-group group.

Under the premise of a large number of requests, for the same request, the probability of forwarding to tabao accounts for 80%, and the probability of forwarding to jd accounts for 20%. But this is not an absolutely accurate visit ratio, it is roughly the same.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: taobao_route
          uri: https://www.taobao.com
          predicates:
            - Weight=web-group,8
        - id: jd_route
          uri: https://www.jd.com
          predicates:
            - Weight=web-group,2
API style
 @Bean
    public RouteLocator weightRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        return builder.routes().route("taobao_route", ps ->
                        ps.weight("web-group", 8)
                                .uri("https://taobao.com"))
                .route("jd_route", ps ->
                        ps.weight("web-group", 2)
                                .uri("https://jd.com"))
                .build();
    }

image

12. XForwardedRemoteAddr routing assertion factory

Documentation: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-xforwarded-remote-addr-route-predicate-factory

As long as the IP with X-Forwarded-For appended to the current request header appears in the IP list specified by the route, the match is successful and the assertion is true.

Configuration
server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
        - id: addr_route
          uri: https://www.baidu.com
          predicates:
            - XForwardedRemoteAddr=192.168.0.1/24
API style
 @Bean
    public RouteLocator xForwardedRemoteAddrRouteLocator(RouteLocatorBuilder builder) {<!-- -->
        return builder.routes().route("addr_route", ps ->
                        ps.xForwardedRemoteAddr("192.168.0.1/24")
                                .uri("https://baidu.com"))
                .build();
    }

13. Priority

When the same route assertion factory is set in an application through configuration and API respectively, and the set values are different, there are two relationships between them:

  1. or relationship: for example, Header routing assertion factory, Query routing assertion factory

  2. Priority relationship: For example, After, Before, Between routing assertion factory. At this time, the priority of the configuration type is higher than that of the API type.