Springcloud gateway uses Gateway Routing

Time:2022-6-18

Route forwarding and filtering of gateway

Configure in a gateway project (configured with Eureka and other components)

server:
  port: 9006
spring:
  application:
    name: zhao-service-gateway
  cloud:
    gateway:
      routes:
       - id: service-autodeliver-router
         #uri: http://127.0.0.1:8091
         uri: lb://zhao-service-autodeliver
         predicates:
          - Path= /autodeliver/**
       - id: service-resume-router
         #uri: http://127.0.0.1:8081
         uri: lb://zhao-service-resume
         predicates:
           - Path=/resume/**
         filters:
           - StripPrefix=1

Through the configuration of the first service, Hao service autodelivery, you can normally access the service through the gateway project by using the fixed IP and service name. However, the method of fixed IP is not very flexible. Lb://zhao-service-autodelivery can realize random load balancing, and you do not need to fill in the fixed IP to avoid unnecessary trouble

Springcloud gateway uses Gateway Routing

In the second service configuration, filters:- stripprefix=1 will filter out the first path configuration. Therefore, in addition to the first filtered configuration, we also need to add the original configuration in the final access. The access form is as follows

Springcloud gateway uses Gateway Routing

Gateway assertion

The above configuration for paths is the configuration of assertion predictions, and the gateway also has the following built-in assertions

Springcloud gateway uses Gateway Routing

Basically, the above assertions are filtered based on the information carried by the request. In the actual operation process, these information can be comprehensively used to achieve the desired operation

Gateway custom global filter

//fhadmin. cn

The filter intercepts the requests in the blacklist (in practice, this operation can be realized with the help of MySQL or redis and other data stores) to achieve the effect

Springcloud gateway uses Gateway Routing

High availability of gateway

As a component of the “normal core”, if the connection fails, all requests may be routed. Therefore, we need to do the “passable” of the gateway. Gateway scalability is very simple: you can start multiple gateway instances to achieve scalability, and enable load balancing devices such as nginx to perform load forwarding upstream of gateway to achieve scalability. Start multiple gateway instances (say two, ⼀ end ⼝ 9002, ⼀ end ⼝ 9003), and the rest is to make ⽤ nginx and others complete the load agent.

This work adoptsCC agreement, reprint must indicate the author and the link to this article