[In-depth analysis of spring cloud gateway] 01 Basic concepts

1. Introduction

Spring Cloud Gateway is a brand new project of Spring Cloud. This project is a gateway developed based on technologies such as 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, Spring Cloud Gateway aims to replace Netflix Zuul. It not only provides a unified routing method, but also provides basic gateway functions based on the Filter chain, such as: security, monitoring/indicators, and current limiting. .

2. Concept

  • Route: Route is the basic element of the gateway, consisting of ID, target URI, assertion, and filter. When the request reaches the gateway, the GatewayHandler Mapping performs route matching through assertions. When the assertion is true, the route is matched.
  • Predicate: Predicate is a function provided in Java 8. Allows developers to match requests from HTTP, such as request headers or request parameters. Simply put it is a matching condition.
  • Filter: Filter is a filter in Gateway that can perform some business processing before and after the request is issued.

3. Working principle

When the client request reaches Spring Cloud Gateway, Gateway Handler Mapping will intercept it and determine which route the request matches based on predicates. If the match is successful, the request is sent to the Gateway web handler. The Gateway web handler processes the request through a series of "pre" type filters and then performs the proxy request. After execution, it goes through a series of "post" type filters and is finally returned to the client.
principle

4. Quick start

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>gateway-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2021.0.8</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Configure a routing and forwarding rule. When http://localhost:8080/hello is entered, go to the specified website.

spring:
  application:
    name: gateway-demo
  cloud:
    gateway:
      routes:
        - id: first
          uri: https://example.org
          predicates:
            - Path=/hello/**
          filters:
            - StripPrefix=1
            

start the main method

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

5. Official website link

Official website document

Guess you like

Origin blog.csdn.net/suyuaidan/article/details/132532189