Spring Cloud microservice Gateway component

What is Spring Cloud Gateway

As the entrance of traffic, the common functions of gateway include routing and forwarding, permission verification, flow restriction, etc.
Spring Cloud Gateway is the second-generation gateway framework officially launched by Spring Cloud, which is positioned to replace Netflix Zuul1.0. Compared with Zuul, Spring Cloud Gateway provides better performance and more powerful functions.
Spring Cloud Gateway is a responsive API gateway implemented by WebFlux + Netty + Reactor. It doesn't work in a traditional servlet container, nor does it build a war package.
Spring Cloud Gateway aims to provide a simple and effective API routing management method for microservice architecture, and provide the basic functions of the gateway based on Filter, such as security authentication, monitoring, current limiting, etc.

1. Other gateway components:
In the spring cloud microservice system, a very important component is the gateway. Zuul gateway is adopted in version 1.x; However, in version 2.x, zuul's upgrade has been skipped. Spring cloud finally developed a gateway to replace zuul, that is, spring cloud gateway
Many places on the Internet say that Zuul is blocked and Gateway is non blocking. This is not rigorous. To be exact, Zuul.x is blocked. In the 2.x version, Zuul is also based on Netty and non blocking. If you have to say performance, there is really no big gap.

On the official website, there was a test project and a benchmark test project was created: spring cloud gateway benchmark, which compared:

Strong performance: 1.6 times that of Zuul, the first generation gateway

Official website documents: https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories

Spring Cloud Gateway features

  1. Based on Spring Framework 5, Project Reactor and Spring Boot2.0;
  2. Dynamic routing: it can match any request attribute;
  3. Support path rewriting;
  4. Integrate Spring Cloud service discovery function (Nacos, Eruka);
  5. Integrated flow control degradation function (Sentinel, Hystrix);
  6. You can specify easy to write predicate and filter for routes;

Core concept

  1. Route
    Routing is the most basic part of the gateway. The routing information consists of an ID, a destination URL, a group of assertion factories and a group of filters. If the assertion is true, the requested URL matches the configured route.
  2. Assertions
    The assertion function in Java 8 and the assertion function type in spring cloud gateway are ServerWebExchange in spring 5.0 framework. The assertion function allows developers to define and match any information in Http request, such as request header and parameters.
  3. Filter
    Filters in spring cloud gateway are divided into Gateway Filter and Global Filter. Filter can process requests and responses.

working principle

The working principle of Spring Cloud Gateway is similar to that of Zuul. The biggest difference is that there are only pre and post filters in the Gateway.

The client sends a request to the Spring Cloud Gateway. If the request matches the route defined by the gateway program, the request will be sent to the gateway Web handler. At this time, the handler runs a specific request filter chain.
The reason why the filters are separated by dashed lines is that the filters may execute logic before and after sending proxy requests. All pre filter logic is executed first, and then the proxy request is executed; After the proxy request is completed, the post filter logic is executed.

Spring Cloud Gateway quick start

1. Environmental construction
1> Introduce dependency

<!--gateway Dependence of spring cloud development-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

Note: it will conflict with the dependency of spring webmvc, so it is necessary to exclude spring webmvc

2> Writing yml configuration files

server:
  port: 8088
# Application name (nacos will take this name as the service name)
spring:
  application:
    name: api-gateway
  cloud:
    # gateway configuration
    gateway:
      # Routing rules
      routes:
        - id: order_route  # Unique identification of the route to order
          uri: http://localhost:8020 # address to be forwarded
          # Assertion rules are used to match routing rules
          predicates:
            - Path=/order-serv/**
              # http://localhost:8020/order-serv/order/add
          filters:
            - StripPrefix=1  # Remove the layer 1 path before forwarding
              # http://localhost:8020/order/add
        #- id: stock_route

3> Testing

gateway integrates nacos

Now the address of the forwarding path is written in the configuration file. We have analyzed the problems caused by the dead address in person. Next, we get this address from the registry.
1. Introduce dependency

<!--nacos Service registration discovery-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!--gateway Dependence of spring cloud development-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

2. Write yml configuration file

server:
  port: 8088
# Application name (nacos will take this name as the service name)
spring:
  application:
    name: api-gateway
  cloud:
    # gateway configuration
    gateway:
      # Routing rules
      routes:
        - id: order_route  # Unique identification of the route to order
          uri: lb://Order service # address to be forwarded lb: use the local load balancing policy in nacos
          # Assertion rules are used to match routing rules
          predicates:
            - Path=/order-serv/**
              # http://localhost:8020/order-serv/order/add
          filters:
            - StripPrefix=1  # Remove the layer 1 path before forwarding
              # http://localhost:8020/order/add
        #- id: stock_route
    # Configuring nacos
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public

3. Test

4. Abbreviation: remove the routing configuration and automatically find services

server:
  port: 8020
# Application name (nacos will take this name as the service name)
spring:
  application:
    name: order-service
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public

5. Test

Keywords: Load Balance Spring Cloud Microservices ribbon

Added by [/Darthus] on Tue, 30 Nov 2021 20:27:23 +0200