【7】GateWay gateway component

Spring Cloud Gateway is a new project of Spring Cloud. This project is a gateway developed based on reactive programming and event streaming technologies such as Spring 5.0, Spring Boot 2.0 and Project Reactor. It aims to provide a simple and effective unification for microservice architecture. API routing management method.

Previous article: Feign remote calling component

basic concept

The core functional features of the gateway:
request routing
, permission control
, and current limiting
. Architecture diagram:

Insert image description here
permission control : As a microservice entrance, the gateway needs to verify whether the user is qualified to request, and intercept it if not.
Routing and load balancing : All requests must first go through the gateway, but the gateway does not process business, but forwards the request to a certain microservice according to certain rules. This process is called routing. Of course, when there are multiple target services for routing, load balancing is also required.
Current limiting : When the request traffic is too high, the gateway will release the request at the speed that the downstream microservices can accept to avoid excessive service pressure.

Build service

  1. Create new module m-cloud-gateway-9002

Insert image description here

  1. Import dependencies
   <!--eureka client 客户端依赖引入-->
        <!--spring boot ⽗启动器依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--GateWay ⽹关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!--引⼊webflux-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <!--⽇志依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--lombok⼯具-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!--引⼊Jaxb,开始-->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.2.10-b140310.1920</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!--引⼊Jaxb,结束-->
        <!-- Actuator可以帮助你监控和管理Spring Boot应⽤-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <!--spring cloud依赖版本管理-->
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <!--编译插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <!--打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  1. Startup class
@SpringBootApplication
@EnableDiscoveryClient
public class McloudGateWay9002 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(McloudGateWay9002.class, args);
    }
}
  1. Configuration file application.yml
server:
  port: 9002

spring:
  application:
    name: m-cloud-eureka-server


#注册发现
eureka:
  client:
    service-url:
      defaultZone: http://CloudEurekaServerA:8761/eureka,http://CloudEurekaServerB:8762/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${
    
    spring.cloud.client.ip-address}:${
    
    spring.application.name}:${
    
    server.port}:@project.version@

  1. Add gateWay configuration
spring:
  cloud:
    gateway:
      routes:
        - id: service-autodeliver-router  #路由 ID,保持唯一
          uri: lb://m-service-autodeliver # 目标服务地址  自动投递微服务(部署多实例)动态路由:
          predicates:  # 断言:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果
            - Path=/autodeliver/**
        - id: service-resume-router
          uri: lb://m-service-resume
          predicates:
            - Path=/resume/**
#            filters:
#               - StripPrefix=1
  1. Start accessing http://localhost:9002/autodeliver/checkState/154513 2
    Insert image description here

Core idea.

Spring Cloud GateWay is inherently asynchronous and non-blocking, based on Reactor model
routing (route) : the most basic part of the gateway, and also the basic working unit of the gateway. Routing consists of an ID, a target
URL (the address to which the final route is directed), a series of assertions (matching condition judgment) and filters (refined control)
. If the assertion is true, the route is matched.
Predicates : Referring to the assertion java.util.function.Predicate in Java 8, developers can match
all content in the HTTP request (including request headers, request parameters, etc.) (similar to location matching in nginx ),
routed if the assertion matches the request.
Insert image description here

Filter : A standard Spring webFilter. Using filters, you can execute business
logic before or after the request.

GateWay dynamic routing details

  • Add the registration center client dependency in pom.xml (because to obtain the registration center service list, the eureka client has been introduced)
  • Dynamic routing configuration
    Insert image description here
    When setting dynamic routing, the uri starts with lb: // (lb represents obtaining the service from the registration center), followed by the name of the service that needs to be forwarded
    to

GateWay filter

From the perspective of filter type, Spring Cloud GateWay filters are divided into two types: GateWayFilter and GlobalFilter.

filter type Sphere of influence
GateWayFilter Apply to a single route
GlobalFilter Applies to all routes
For example, Gateway Filter can remove the placeholder in the URL and then forward the route, such as
          predicates:
            - Path=/resume/**
            filters:
               - StripPrefix=1

GateWay High Availability

Multiple GateWay instances can be started to achieve high availability, and load balancing devices such as Nginx are used upstream of GateWay for load forwarding to achieve high availability.

#配置多个GateWay实例
upstream gateway {
    
    
 server 127.0.0.1:9002;
 server 127.0.0.1:9003;
}
location / {
    
    
 proxy_pass http://gateway;
}

Guess you like

Origin blog.csdn.net/u014535922/article/details/129978827