Use of the Spring Cloud Gateway

oneSpringbootThe correspondingSpringcloudThe version is as follows:

IO /projects/sp…

Dalston, Edgware, Finchley, Greenwich are no longer supported

Use two.

pomfile

<properties>
    <spring.cloud-version>Hoxton.SR8</spring.cloud-version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring.cloud-version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

yamlfile

spring:
  cloud:
    gateway:
      routes:
        - id: weight_high
          uri: http://localhost:8080/
          predicates:
            - Path=/gateway/a
          filters:
/gateway/a = /a
            - StripPrefix=1
Reset the routing address
            - SetPath=/debug
Copy the code

Of three.

The official documentation

Route

The basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates, and a collection of filters. A route is matched if the aggregate predicate is true.

Corresponding to this class (see yamL file above) :

Predicate

This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This lets you match on anything from the HTTP request, such as headers or parameters.

The request is intercepted when a match is made

Filter

These are instances of Spring Framework GatewayFilter that have been constructed with a specific factory. Here, you can modify requests and responses before or after sending the downstream request.

You can configure more filters ~

Four. Working principle

Since the Gateway package uses WEBFLUX and cannot coexist with this WEBMVC package, the Spring-boot-starter-Web package is removed

How it works: Just a bunch of filters

Route Predicate (★★★★)

Route Predicate Factories: doc.spring. IO /spring-clou…

You can see that there are 11 matching patterns ~

Match pattern complete (key!)

The match here satisfies the regular expression

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
# match after this time
            - After = 2017-01-20 T17:42:47. 789-07:00 [America/Denver]
# match after this time
            - Before = 2017-01-20 T17:42:47. 789 - from America/Denver
# match the middle of the time
            - Between = 2017-01-20 T17:42:47. 789 - from America/Denver, The 2017-01-21 T17:42:47. 789-07:00[America/Denver]
# block the request if the cookie is chocolate=ch.p
            - Cookie=chocolate, ch.p
If there is an X-request-id header in the header and its value matches the re
            - Header=X-Request-Id, \d+
# match the host address ~
            - Host=**.somehost.org,**.anotherhost.org
# match the request method
            - Method=GET,POST
# Match the path
            - Path=/gateway/a
# Match the request parameter ~ if the request parameter is red and the value starts with GREE
            - Query=red, gree.
The matching url is 192.168.1.1-192.168.1.254
            - RemoteAddr = 192.168.1.1/24
Copy the code

Match by weight

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2
Copy the code

Gateway Filter Factories(★★★★★)

‘Gateway Filter Factories: doc.spring. IO /spring-clou…

The official website has as many as 30 filters for you to use

I’ll probably write about it in another blog post, but here’s a quick reference to the configuration of 4YE in the yamL file above

Seven. Global Filters (u u u u u)

Global Filters: docs. Spring. IO/spring – clou…

Global filter ~😄

Custom filter

@Component
public class CustomGlobalFilter implements GlobalFilter.Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("custom global filter");
        return chain.filter(exchange);
    }

    @Override
    public int getOrder(a) {
        return -1; }}Copy the code

Public articles address: mp.weixin.qq.com/s/cKrnr2yyN… (Surprise ~)

Java4ye A little white blogger focused on productivity ~(increasing groping time), sharing learning resources, technological awareness, little things about programmer life let us groping together ~(● world twentyconsciousness) Java4ye here to prepare a series of learning resources for you, as well as a variety of plug-ins, software oh welcome message! Thanks for your support! O (≧ ヾ del ≦ *)