[Spring Cloud Alibaba] routing Gateway gateway

1. Introduction

Spring Cloud gateway is a new project of Spring Cloud. The project is a gateway developed based on Spring 5.0, Spring Boot 2.0 and Project Reactor. It aims to provide a simple and effective unified API routing management method for microservice architecture.

As a gateway in the Spring Cloud ecosystem, the goal of Spring Cloud gateway is to replace Zuul. In Spring Cloud versions above 2.0, the latest performance versions above Zuul 2.0 are not integrated, and the old version of non Reactor mode before Zuul 2.0 is still used. In order to improve the performance of the gateway, the Spring Cloud gateway is implemented based on the WebFlux framework, and the underlying WebFlux framework uses the high-performance Reactor mode communication framework Netty.

The goal of Spring Cloud Gateway is not only to provide a unified routing method, but also to provide the basic functions of the gateway based on the Filter chain, such as security, monitoring / indicators, and current limiting.

2. Features

The official of spring cloud introduces the features of spring cloud gateway as follows:

  • Based on Spring Framework 5, Project Reactor and Spring Boot 2.0
  • Integrated Hystrix circuit breaker
  • Integrating Spring Cloud DiscoveryClient
  • Predictions and Filters are used for specific routes. They are easy to write
  • It has some advanced functions of gateway: dynamic routing, current limiting and path rewriting

From the above characteristics, it is not different from Zuul's characteristics. The main difference between spring cloud gateway and Zuul lies in the underlying communication framework.

Briefly explain the three terms above:

  • Filter:

    Similar to Zuul's filter in concept, it can be used to intercept and modify requests and secondary process upstream responses. The filter is an instance of the org.springframework.cloud.gateway.filter.GatewayFilter class.

  • Route:

    The basic composition module of gateway configuration is similar to Zuul's routing configuration module. A Route module is defined by an ID, a target URI, a set of assertions, and a set of filters. If the assertion is true, the Route matches and the destination URI is accessed.

  • Predicate:

    This is a Java 8 Predicate that can be used to match anything from an HTTP request, such as headers or parameters. The input type of the assertion is a ServerWebExchange.

3. Processing flow

The client sends a request to the Spring Cloud Gateway. Then find the route matching the request in the Gateway Handler Mapping and send it to the Gateway Web Handler. The Handler sends the request to our actual service through the specified filter chain, executes the business logic, and then returns. The filters are separated by dashed lines because the filter may execute business logic before ("pre") or after ("post") sending the proxy request.

4. Routing configuration

Create a new project to configure routing.

5. pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>net.javatv</groupId>
        <artifactId>hsca-dependencies</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../hsca-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>hsca-gateway</artifactId>
    <packaging>jar</packaging>

    <name>hsca-gateway</name>
    <url>https://javatv.net</url>
    <inceptionYear>2021-Now</inceptionYear>

    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- SpringCloud Loadbalancer -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <!-- Spring Cloud End -->

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>net.javatv.GatewayApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

6. Startup class

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

7. application.yml

spring:
  application:
    # apply name
    name: spring-gateway
  cloud:
    # Registering discovery using Naoos as a service
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    # Use Sentinel as fuse
    sentinel:
      transport:
        port: 8720
        dashboard: localhost:8080
    # Routing gateway configuration
    gateway:
      # Setting is combined with the service registration discovery component, so that the routing policy of service name can be adopted
      discovery:
        locator:
          enabled: true
      # Configure routing rules
      routes:
        # Adopt custom routing ID(There are fixed usage, different id It has different functions. See the following for details: https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters)
        - id: NACOS-CONSUMER
          # The request is made in LoadBalanceClient mode, starting with lb: / /, followed by the service name registered on Nacos
          uri: lb://nacos-consumer
          # Predicate translates to "predicate". It must be. Its main function is to match the user's request. There are many uses
          predicates:
            # Method method predicate, which matches GET and POST requests
            - Method=GET,POST

server:
  port: 9000

feign:
  sentinel:
    enabled: true

management:
  endpoints:
    web:
      exposure:
        include: "*"

# Configure the log level for debugging
logging:
  level:
    org.springframework.cloud.gateway: debug

Start the Nacos service, service provider, service consumer and gateway service in turn.

visit: http://localhost:9000/nacos-consumer/echo/test

Similarly, consumer services are not accessed through the gateway: http://localhost:9090/echo/test

Further instructions: https://www.cnblogs.com/crazymakercircle/p/11704077.html

Keywords: Spring Cloud

Added by Lashiec on Sun, 21 Nov 2021 10:54:05 +0200