003-spring Cloud gateway-Overview, Fundamentals, Route predicate Factory

Source: Internet
Author: User
Tags dashed line

I. Overview

Prerequisites: Spring 5, Spring Boot 2 and Project Reactor.

Spring Cloud Gateway is designed to provide a simple and efficient way to route to APIs and provide them with crosscutting concerns such as security, monitoring/metrics, and resiliency.

Route: The basic building block of the routing gateway. It is defined by the ID, target URI, predicate collection, and filter set. If the aggregation predicate is true, the route is matched.

Functions of the Predicate:java 8, Spring FrameworkServerWebExchange允许开发人员匹配HTTP请求中的任何内容,例如标头或参数。

Filter: These are spring Framework Gatewayfilter instances that are built using a specific factory. Here, the request and response can be modified before or after the downstream request is sent.

Second, using 2.1, POM reference
        < Dependency >            < groupId >org.springframework.cloud</groupId>            <  Artifactid>spring-cloud-starter-gateway</artifactid>         </dependency>

Close: Spring.cloud.gateway.enabled=false

Note: Spring Cloud gateway requires the Netty runtime provided by spring boot and spring Webflux. It cannot work in a traditional servlet container or be built as a war.

Three, the principle of work

The client makes a request to the spring Cloud Gateway. If the gateway handler mapping determines that the request matches the route, it is sent to the gateway Web handler. This handler is run through a request-specific filter chain to send the request. The filter is broken down by a dashed line because the filter can execute logic before or after sending the proxy request. Executes all "pre" filter logic and then makes proxy requests. After issuing the proxy request, execute the "POST" filter logic.

URIs that are defined in routes that do not have ports will get the default port settings of 80 and 443, respectively, for HTTP and HTTPS URIs.

Third, route predicate factories "Route predicate factory"

Spring Cloud Gateway matches the route as part of the spring Webflux handlermapping infrastructure. Spring Cloud Gateway contains many built-in route predicate factories. All of these predicates match the different properties of the HTTP request. The multi-route predicate factory can be combined and passed through the logical and.

3.1. After Route predicate Factory

This predicate matches the request that occurred after the current DateTime.

Application.yml

Spring:  Cloud:    Gateway:      routes:      ID: After_route        uri:http://  example.org        predicates:        -after=- -20t17:42 :47.789-£ [america/denver]
3.2. Before Route predicate Factory

This predicate matches a request that occurred prior to the current date time.

Application.yml

Spring:  Cloud:    Gateway:      routes:      ID: Before_route        uri:http://  example.org        predicates:        -before=- -20t17:42 :47.789-£ [america/denver]
3.3. Between Route predicate Factory

This predicate matches the request that occurred after datetime1 and before DateTime2. The datetime2 parameter must be after datetime1.

Application.yml

Spring:  Cloud:    Gateway:      routes:      ID: Between_route        uri:http:/ /example.org        predicates:        -between=- -20t17:  from £47.789-: £º- -21t17:  47.789-: [America/denver]
3.4. Cookie Route predicate Factory

The cookie Route predicate factory has two parameters, a cookie name and a regular expression. This predicate matches the cookie with the given name and the value matches the regular expression.

Application.yml

Spring:  Cloud:    Gateway:      routes:      ID: Cookie_route        uri:http://  example.org        predicates:        -Cookie=chocolate, CH.P

This route matching request has a cookie named chocolate whose value matches the CH.P regular expression.

3.5. Header Route Predicate Factory

Header Route predicate factory has two parameters, title name and regular expression. This predicate matches the header with the given name and the value matches the regular expression.

Application.yml

Spring:  Cloud:    Gateway:      routes:      ID: Header_route        uri:http://  example.org        predicates:        -Header=x-request-id, \d+

If the request has a header named X-request-id, the route matches and its value matches the \ D + Regular expression (a value with one or more numbers).

3.6. Host Route predicate Factory

Host Route predicate factory takes one parameter: hostname mode. This mode is an ant style mode "." As a separator character. This predicate matches the host header that matches the pattern.

Application.yml

Spring:  Cloud:    Gateway:      routes:      ID: Host_route        uri:http://  example.org        predicates:        -host=**.somehost.org

If the requested host header has a value of www.somehost.org or beta.somehost.org, this route will match.

3.7. Method Route predicate Factory

Method Route predicate factory takes one parameter: The HTTP method to match.

Spring:  Cloud:    Gateway:      routes:      ID: method_route        uri:http:  //example.org        predicates:        -Method=get
3.8. Path Route predicate Factory
Spring:  Cloud:    Gateway:      routes:      ID: Host_route        uri:http://  example.org        predicates:        -path=/foo/{segment}

/foo/1or /foo/bar .

3.9. Query Route predicate Factory

Query Route predicate factory has two parameters: a required parameter and an optional regular expression.

Spring:  Cloud:    Gateway:      routes:      ID: Query_route        uri:http://  example.org        predicates:        -Query=baz

If the request contains Baz query parameters, this route will match.

Spring:  Cloud:    Gateway:      routes:      ID: query_route        uri:http:  //example.org        predicates:        -Query=foo, Ba.

If the request contains the foo query parameter whose value matches the BA, the route will match. RegExp, so bar and Baz match.

3.10. remoteaddr Route predicate Factory

The REMOTEADDR Route predicate factory uses a list of CIDR notation (IPV4 or IPV6) strings (The minimum value is 1), for example, 192.168.0.1/16 (where 192.168.0.1 is the IP address and 16 is the subnet mask).

Spring:  Cloud:    Gateway:      routes:      ID: Remoteaddr_route        uri:http: // example.org         predicates:        -remoteaddr=192.168.  1.1/

If the requested remote address is for example 192.168.1.10, then this route will match.

003-spring Cloud gateway-Overview, Fundamentals, Route predicate Factory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.