5. Spring Cloud Gateway-Gateway

Gateway Introduction

SpringCloud Gateway is a brand new project of Spring Cloud. The 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.

Gateway, as a gateway in the Spring Cloud ecosystem, aims to replace Zuul. In order to improve the performance of the gateway, the Gateway is implemented based on the WebFlux framework, and the bottom layer of the WebFlux framework uses the high-performance communication framework Netty.

1. Gateway Features

  • Limiting
  • path rewriting
  • dynamic routing
  • Integrate Spring Cloud Discovery Client
  • Integrated Hystrix circuit breaker

2. Core concepts

  • Filter

Similar in concept to Zuul's filter, it can be used to intercept and modify requests, and perform secondary processing on upstream responses. Filters are instances of the GatewayFilter class.

  • Route

    The basic component module of gateway configuration is similar to Zuul's routing configuration module.一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。

  • Predicate

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

3. The difference between Gateway and Zuul

  • Zuul is an open source product of Netflix, and Spring Cloud Gateway is a product in the Spring family, which can be better integrated with other components in the Spring family.
  • Zuul1 does not support long connection, such as websocket.
  • Spring Cloud Gateway supports throttling.
  • Spring Cloud Gateway is developed based on Netty , which implements asynchronous and non-blocking, takes up less resources, and has better performance than Zuul .

Gateway basic use

Spring Cloud Gateway supports two different usages:

  • Coded
  • yml configuration

Coded configuration example

1. Create a Spring Boot project and add Spring Cloud Gateway dependencies:

<!-- 网关 gateway 依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

2. After the project is successfully created, directly configure a RouteLocator bean to implement request forwarding.

@Bean
RouteLocator routeLocator(RouteLocatorBuilder builder) {
    
    
    return builder.routes()
    		// 设置路由规则
            .route("test_route", r -> r.path("/get").uri("http://httpbin.org"))
            .build();
}

3. Start the project, visit: http://localhost:8080/get, the request will be forwarded to http://httpbin.org

yml configuration example

# gateway路由规则配置
spring:
  cloud:
    gateway:
      # 路由
      routes:
      	# - 表示数组
        - id: test_route	# id
          uri: http://httpbin.org	# 目标URI
          predicates:	# 断言
            - Path=/get	# 表示根据路径匹配
      discovery:
        locator:
          enabled: true # 开启自动代理
  application:
    name: gateway
# 将gateway服务注册到 eureka上
eureka:
  client:
    service-url: 
      defaultZone: http://localhost:1111/eureka、
# 日志级别
logging:
  level: 
    org.springframework.cloud.gateway: debug

Start the project, access the interface, http://localhost:8080/get, the request will be forwarded to http://httpbin.org

Route

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 target URI is accessed.

# gateway路由规则配置
spring:
  cloud:
    gateway:
      # 路由
      routes:
      	# - 表示数组
        - id: test_route	# id
          uri: http://httpbin.org	# 目标URI
          predicates:	# 一组断言
            - Path=/get		# 表示根据路径匹配
          filters:		# 一组过滤器
            - AddRequestParameter=name,zhangsan

Predicate

1. Match by time

  • After: Indicates request forwarding after a certain point in time
  • Before: Indicates that the request is forwarded before a certain point in time
  • Between: Indicates that the request is forwarded between two time points, and the two time points are separated by .
spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: http://httpbin.org
          predicates:
            - After=2022-03-22T01:01:01+08:00[Asia/Shanghai]

The above configuration indicates that the request time will be routed after the time of 2022-03-22T01:01:01+08:00[Asia/Shanghai].

2. Match by request

spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: http://httpbin.org
          predicates:
            - Method=GET

The above configuration means that only GET requests are routed.

3. By request path matching

spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: http://www.javaboy.org
          predicates:
            - Path=/2019/0612/{
    
    segment}

The above configuration indicates that the path that meets the rule /2019/0612/** will be routed.

4. Matching by parameters

spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: http://httpbin.org
          predicates:
            - Query=name

The above configuration indicates that there must be a name parameter in the request to be routed, otherwise no route will be performed.

Parameters and parameter values ​​can also be specified:

spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: http://httpbin.org
          predicates:
            - Query=name,java.*

The above configuration indicates that there must be a name parameter in the request, and the value value must start with java before it can be routed.

5. Combination of multiple matching methods

spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: http://httpbin.org
          predicates:
            - Query=name,java.*
            - Method=GET
            - After=2022-03-22T01:01:01+08:00[Asia/Shanghai]

Filter

Filters in Spring Cloud Gateway fall into two categories:

  • GatewayFilter
  • GlobalFilter

Example of filter usage:

spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: lb://provider	# 这里表示路由到provider服务
          filters:
            - AddRequestParameter=name,javaboy
          predicates:
            - Method=GET

The AddRequestParameter filter is to automatically add additional parameters when requesting forwarding routes.

Supongo que te gusta

Origin blog.csdn.net/weixin_45698637/article/details/123673237
Recomendado
Clasificación