This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money

Predicate

Predicate is a functional interface provided by Java 8 that accepts a parameter and returns a Boolean value. Predicate can be used for conditional filtering and cas validation.

Specify the time rule to match the route

  • Corresponding BeforeRoutePredicateFactory request before the specified date
  • Request after the specified date Corresponding AfterRoutePredicateFactory
  • Request in the designated corresponding BetweenRoutePredicateFactory between two dates
spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before = 2017-01-20 T17:42:47. 789 - from America/Denver
Copy the code

Cookie Matching routing

Corresponding CookieRoutePredicateFactory

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, xpp
Copy the code

The current request must carry a name=chocolate value XPP to route to example.org

Header Matching route

Corresponding HeaderRoutePredicateFactory, judge the Header in the request message corresponding to the name and the value and the configuration of the Predicate values match, the value is also a regular matching form

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+
Copy the code

Header name= x-request-id value matches \d+ according to the regular expression, that is, matches more than one number.

Host matching route

HTTP request will carry a Host field, this field represents the request of the server address, and the corresponding HostRoutePredicateFactory

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org
Copy the code

Host can be configured with a list of elements separated by commas. The host in the current request is routed to example.org only when the value of **.somehost.org or **.anotherhost.org is specified

The request method matches the route

Corresponding MethodRoutePredicateFactory according to the HTTP request Method attributes to match in order to realize the routing

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST
Copy the code

If the request is a GET or POST request, it is routed to example.org

The request path matches the route

Corresponding PathRoutePredicateFactory

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}
Copy the code

{segment} is a special placeholder. /* indicates single-layer path matching and /** indicates multi-layer path matching. The matching request is forwarded to example.org only when the URI is /red/* or /blue/*

To summarize, this article has focused on the predicate part of gateway, which is the functional interface that Java8 enters and provides assertion capabilities. Can match anything in an HTTP request. If the Predicate set determines true, it means that the request will be forwarded by the current Router.

And then the type of predicate is sort of match the route by the specified time rule, match the route by Cookie, match the route by Header, match the route by Host, match the route by request method and wait.

We use appropriate verb in the right places routing rules, simplify our code development, to really play the role of routing forwarding gateway gateway, the gateway gateway is the nature of the request routing forwarding as well as to the request of the front and rear filtering, in the next article we will to filter the content of this part of the explanation, If this article is helpful to you, don’t forget to give me a thumbs up message oh, hey hey, your thumbs up and comments are great support to me, we learn gateway together, so that we can flexibly use gateway in our work, let gateway really play its role.

Reference: docs. Spring. IO/spring – clou…