Spring Cloud Gateway-Route Predicate Factories (Route Predicate Factories)

Spring Cloud Gateway-Route Predicate Factories (Route Predicate Factories)

Click on "IT Ranch" above and select "Set as Star" to deliver technical dry goods daily!

TIPS This article is based on Spring Cloud Greenwich SR2 and is compatible with Spring Cloud Finchley and higher versions.

This section discusses the Spring Cloud Gateway 路由谓词工厂(Route Predicate Factories) in detail . The role of the route predicate factory is to use the configuration of the route if the predicate condition is met, otherwise it will be ignored. As long as you master this sentence, it will be easier to master the routing predicate factory.

TIPS Predicate is a functional programming interface provided by Java 8.

This article explores the built-in predicate factory in Spring Cloud Gateway, including:

Two forms of routing configuration

Let's first discuss the two postures of Spring Cloud Gateway routing configuration:

Route to the specified URL

Example 1: Wildcard

spring:
  cloud:
    gateway:
      routes:
      -id: {Unique ID}
        uri: http://www.itmuch.com

Represents the access GATEWAY_URL/**will be forwarded to thehttp://www.itmuch.com/**

This section of TIPS configuration cannot be used directly, it needs to be used in conjunction with the following Predicate.

Example 2: Exact match

spring:
  cloud:
    gateway:
      routes:
      -id: {Unique ID}
        uri: http://www.itmuch.com/spring-cloud/spring-cloud-stream-pan-ta/

Represents the access GATEWAY_URL/spring-cloud/spring-cloud-stream-pan-ta/will be forwarded to thehttp://www.itmuch.com/spring-cloud/spring-cloud-stream-pan-ta/

This section of TIPS configuration cannot be used directly, it needs to be used in conjunction with the following Predicate.

Route to the microservice on the service discovery component

Example 1: Wildcard

spring:
  cloud:
    gateway:
      routes:
      -id: {Unique ID}
        uri: lb://user-center

Represents the access GATEWAY_URL/**will be forwarded to the user-centermicro services/**

This section of TIPS configuration cannot be used directly, it needs to be used in conjunction with the following Predicate.

Example 2: Exact match

spring:
  cloud:
    gateway:
      routes:
      -id: {Unique ID}
        uri: lb://user-center/shares/1

Represents the access GATEWAY_URL/shares/1will be forwarded to the user-centermicro services/shares/1

This section of TIPS configuration cannot be used directly, it needs to be used in conjunction with the following Predicate.

Detailed explanation of predicate factory

The following formally discusses the routing predicate factory. Spring Cloud Gateway provides ten kinds of routing predicate factories. It provides a cornerstone for the gateway to realize flexible forwarding.

After

Example:

spring:
  cloud:
    gateway:
      routes:
        -id: after_route
          uri: lb://user-center
          predicates:
            # If and only when the time of the request is configured after the time, it will be forwarded to the user microservice
            # The current configuration will not enter the routing configuration, so it returns 404
            # Change the time to the time of <now, then visit localhost:8040/** -> user-center/**
            # eg. Visit http://localhost:8040/users/1 -> user-center/users/1
            -After=2030-01-20T17:42:47.789-07:00[America/Denver]

TIPS • Skills: The time can be used System.out.println(ZonedDateTime.now());to print, and then you can see the time zone. For example: 2019-08-10T16:50:42.579+08:00[Asia/Shanghai]• Relevant logic of time format: • Default time format: org.springframework.format.support.DefaultFormattingConversionService#addDefaultFormatters•Time format registration: org.springframework.format.datetime.standard.DateTimeFormatterRegistrar#registerFormatters

Before

Example:

spring:
  cloud:
    gateway:
      routes:
        -id: before_route
          uri: lb://user-center
          predicates:
            # If and only if the time of the request is the time configured by Before, it will be forwarded to the user microservice
            # The current configuration will not enter the routing configuration, so it returns 404
            # Change the time to> now, then visit localhost:8040/** -> user-center/**
            # eg. Visit http://localhost:8040/users/1 -> user-center/users/1
            -Before=2018-01-20T17:42:47.789-07:00[America/Denver]

Between

Example:

spring:
  cloud:
    gateway:
      routes:
        -id: between_route
          uri: lb://user-center
          predicates:
            # If and only when the time of the request is between the configured time, it will be forwarded to the user microservice
            # Therefore, visit localhost:8040/** -> user-center/**
            # eg. Visit http://localhost:8040/users/1 -> user-center/users/1
            -Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2027-01-21T17:42:47.789-07:00[America/Denver]

Example:

spring:
  cloud:
    gateway:
      routes:
        -id: cookie_route
          uri: lb://user-center
          predicates:
            # If and only if there is a cookie named somecookie, and the value conforms to the regular ch.p, it will be forwarded to the user microservice
            # If the cookie meets the conditions, visit http://localhost:8040/** -> user-center/**
            # eg. Visit http://localhost:8040/users/1 -> user-center/users/1
            -Cookie=somecookie, ch.p
spring:
  cloud:
    gateway:
      routes:
        -id: header_route
          uri: lb://user-center
          predicates:
            # If and only if there is a Header named X-Request-Id and the value conforms to the regular/d+, it will be forwarded to the user microservice
            # If the Header meets the conditions, visit http://localhost:8040/** -> user-center/**
            # eg. Visit http://localhost:8040/users/1 -> user-center/users/1
            -Header=X-Request-Id,/d+

Host

spring:
  cloud:
    gateway:
      routes:
        -id: host_route
          uri: lb://user-center
          predicates:
            # If and only if the Header named Host matches **.somehost.org or **.anotherhost.org, the user microservice will be forwarded
            # If Host meets the conditions, visit http://localhost:8040/** -> user-center/**
            # eg. Visit http://localhost:8040/users/1 -> user-center/users/1
            -Host=**.somehost.org,**.anotherhost.org

Method

spring:
  cloud:
    gateway:
      routes:
        -id: method_route
          uri: lb://user-center
          predicates:
            # If and only if the HTTP request method is GET, the user microservice will be forwarded
            # If the request method meets the conditions, visit http://localhost:8040/** -> user-center/**
            # eg. Visit http://localhost:8040/users/1 -> user-center/users/1
            -Method=GET

Path

spring:
  cloud:
    gateway:
      routes:
        -id: path_route
          uri: lb://user-center
          predicates:
            # If and only if the access path is/users/* or/some-path/**, the user microservice will be forwarded
            # segment is a special placeholder, single-level path matching
            # eg. Visit http://localhost:8040/users/1 -> user-center/users/1
            -Path=/users/{segment},/some-path/**

TIPS

I suggest you read this part of the official document, which has a segment programming skills. Relatively simple, leave an impression.

https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_path_route_predicate_factory

Query

Example 1:

spring:
  cloud:
    gateway:
      routes:
        -id: query_route
          uri: lb://user-center
          predicates:
            # If and only if the request contains baz parameters, it will be forwarded to the user microservice
            # eg. Visit http://localhost:8040/users/1?baz=xx -> user-center/users/1
            -Query=baz

Example 2:

spring:
  cloud:
    gateway:
      routes:
        -id: query_route
          uri: lb://user-center
          predicates:
            # If and only if the request has a parameter named foo, and the parameter value conforms to the regular ba., it will be forwarded to the user microservice
            # eg. Visit http://localhost:8040/users/1?baz=baz -> user-center/users/1?baz=baz
            -Query=foo, ba.

RemoteAddr

Example:

spring:
  cloud:
    gateway:
      routes:
        -id: remoteaddr_route
          uri: lb://user-center
          predicates:
            # If and only if the requested IP is the 192.168.1.1/24 network segment, such as 192.168.1.10, it will be forwarded to the user microservice
            # eg. Visit http://localhost:8040/users/1 -> user-center/users/1
            -RemoteAddr=192.168.1.1/24

TIPS

I suggest you read this part of the official document, there is a small programming skills. Relatively simple, leave an impression.

https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_remoteaddr_route_predicate_factory

Related code

Dry goods sharing

Recently, I organize personal study notes into a book and share them in PDF. Follow me, reply to the following code, you can get the Baidu disk address, no routine to receive! •001: "Java Concurrency and High Concurrency Solutions" study notes; •002: "Deep JVM Kernel-Principles, Diagnosis and Optimization" study notes; •003: "Java Interview Collection" • 004: "Docker Open Source Book" • 005: "Kubernetes Open Source Book" • 006: "DDD Crash (Domain Driven Design Crash)" • 007: All • 008: Add technical discussion group

Recent hot articles

Detailed description of Java mainstream log tool librarySpring Cloud Stream knowledge point inventoryAmazon practice field-driven design approachSeveral strategies in the process of caching use summary and combination analysis of advantages and disadvantagesUnderstand QPS, TPS, PV, UV, GMV in seconds , IP, RPS! The unified login of multiple accounts realizes the whole process

Reference: https://cloud.tencent.com/developer/article/1486311 Spring Cloud Gateway-Route Predicate Factories-Cloud + Community-Tencent Cloud