Introduction to the

  • Current limiting
  • The path to rewrite
  • Dynamic routing

Compared with Zuul

  • Zuul is a Netflix product and GateWay is a Spring family product
  • Zuul1 does not support long connections, such as Websockets
  • GateWay Supports traffic limiting
  • GateWay is developed based on Netty, which realizes asynchronous and non-blocking, occupies less resources and has stronger performance

Microservice usage

GateWay can be used in two ways

  • The encoding type
  • Yml type

Create a new project, add a dependency, be careful not to add a WebMVC dependency

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        <version>2.2.4. RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>2.2.4. RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Copy the code

The encoding type

@SpringBootApplication
public class GatewayApplication {

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

    @Bean
    RouteLocator routeLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                // Customize the route
                .route("southyin_route",r->r.path("hello1").uri("http://localhost:8888")) .build(); }}Copy the code

Yml type

Application configuration

spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168189.101.: 8848
    gateway:
      discovery:
        locator:
          enabled: true
logging:
  level:
    org.southyin.gateway: debug

server:
  port: 2020
Copy the code

Start the project, access to test, here can access directly through http://localhost:2020/prover/hello1 application name to access, and automatic load balancing support

You can also define routes

spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168189.101.: 8848
    gateway:
      routes:
        - id: southyin_route # Route name, custom
          uri: http://localhost:8888 # forwarding address
          predicates: # define interface, is an array
            - Path=/hello1
      discovery:
        locator:
          enabled: true
logging:
  level:
    org.southyin.gateway: debug

server:
  port: 2020
Copy the code

Visit the test as shown below

Predicate

This is one of the reasons Spring Cloud is so powerful, for example

1. Yml used Path to match

spring:
  cloud:
    gateway:
      routes:
        - id: southyin_route
          uri: http://localhost:8888
          predicates:
            - Path=/hello1
      discovery:
        locator:
          enabled: true
Copy the code

2. The configured routes can also be matched by time

spring:
  cloud:
    gateway:
      routes:
        - id: southyin_route
          uri: http://localhost:8888
          predicates:
            - After=2021-09-05T01:01:01+08:00[Asia/Shanghai]
      discovery:
        locator:
          enabled: true
Copy the code

Indicates that the request will be routed at 2021-09-05 09:01:01, and there are two keywords

  • Before: Before a certain point in time
  • “Between” : Two time points are separated by commas (,)

3. It can also be matched by request method

spring:
  cloud:
    gateway:
      routes:
        - id: southyin_route
          uri: http://localhost:8888
          predicates:
            - Method=GET
      discovery:
        locator:
          enabled: true
Copy the code

Indicates that only GET requests will be routed

4. Request path matching can also be used

spring:
  cloud:
    gateway:
      routes:
        - id: southyin_route
          uri: http://localhost:8888
          predicates:
            - Path=/test/{segment}
      discovery:
        locator:
          enabled: true
Copy the code

Indicates that the test prefix can be forwarded

5. Match parameters

spring:
  cloud:
    gateway:
      routes:
        - id: southyin_route
          uri: http://localhost:8888
          predicates:
            - Query=name
      discovery:
        locator:
          enabled: true
Copy the code

Indicates that the request must have the name parameter to forward

spring:
  cloud:
    gateway:
      routes:
        - id: southyin_route
          uri: http://localhost:8888
          predicates:
            - Query=name,test.*
      discovery:
        locator:
          enabled: true
Copy the code

The value representing the name parameter must start with test

The use of the Filter

There are two main categories of filters in Gateway:

  • GatewayFilter
  • GlobalFilter
spring:
  cloud:
    gateway:
      routes:
        - id: southyin_route
          uri: lb://provider # lb,loadBalance stands for automatic load balancing
          filters:
            - AddRequestParameter=name,southyin
          predicates:
            - Method=GET
      discovery:
        locator:
          enabled: true
Copy the code

This filter indicates that the name parameter, value southyin, is automatically added when forwarding