I. Introduction to GateWay
Spring Cloud GateWay (which is just one of many GateWay solutions).
Spring Cloud GateWay is a new project of Spring Cloud that aims to replace Netflix Zuul, It is developed based on Spring5.0+SpringBoot2.0+WebFlux (Reactor model based on high performance responsive communication framework Netty, asynchronous non-blocking model), and its performance is higher than Zuul. In official tests, GateWay is 1.6 times that of Zuul. It aims to provide a simple and effective unified API route management method for microservices architecture.
Spring Cloud GateWay not only provides a unified routing mode (reverse proxy), but also provides basic GateWay functions based on the Filter chain, such as authentication, traffic control, fusing, path rewriting, log monitoring, etc.
Gateway location in the architecture:
2. GateWay core concept
The Netty Spring Cloud GateWay is inherently asynchronous and non-blocking, based on the Reactor model
A request – > the gateway matches according to certain conditions – then forwards the request to the specified service address; In this path, we can do some more specific controls (limiting traffic, logging, whitelist)
- Route: The most basic part of a gateway and the basic unit of work of the gateway. A route consists of an ID, a destination URL (the final route to the address), a set of assertions (matching criteria), and a Filter Filter (refinement control). If the assertion is true, the route is matched.
- Predicates: Reference the Java8 assertions in Java. Util. The function. The Predicate, the developer can match all the content in the Http request (including the request headers, request parameters, etc.) (similar to the location of nginx matching), if the assertion match the request routing.
- Filter: A standard Spring webFilter that uses filters to perform business logic before or after a request.
An image from the official website
Among them, Predicates predicate is our matching condition, and Filter can be understood as an omnipotent interceptor. With these two elements, combined with the target URL, a specific route forwarding can be realized.
How It Works
Filter The Pre Filter performs parameter verification, permission verification, traffic monitoring, log output, and protocol conversion. The POST Filter performs response content modification, response header modification, log output, and traffic monitoring.
GateWay core logic: route forwarding + execution filter chain
4. GateWay application
- To import dependencies, the GateWay does not need to use the Web module. It introduces WebFlux (similar to SpringMVC). Note: Do not introduce the starter-Web module
- Application. yml Part of the configuration file
server:
port: 9002
eureka:
client:
serviceUrl: Path to eureka Server
defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/ Eureka server can synchronize the registry with each eureka server
instance:
Use the IP address to register, otherwise the host name will be registered.
prefer-ip-address: true
spring:
application:
name: gateway-9002
cloud:
ConfigServer communicates with the ConfigServer and tells the ConfigServer in which file it wants to obtain the configuration information
config:
discovery:
enabled: true
service-id: config-bus-9006
gateway:
routes: There can be multiple routes
- id: lagou-service-user # our custom route ID, keep unique
uri: lb://lagou-service-user
predicates:
- Path=/api/user/**
- id: lagou-service-code # our custom route ID, keep unique
uri: lb://lagou-service-code
predicates:
- Path=/api/code/**
Copy the code
GateWay Routing rules
Implementing dynamic Routing
GateWay supports automatic retrieval and access of a list of services from a registry, known as dynamic routing
- Add registry client dependencies to pom.xml (since the Eureka client has been introduced to get a list of registry services)
- Dynamic Routing Configuration
Note: When setting up a dynamic route, the URI starts with lb: // (lb stands for service from the registry) followed by the name of the service to be forwarded
GateWay filter
- In terms of filter life cycle (influencing timing points), there are two main pre and Post:
- From the perspective of filter types, Spring Cloud GateWay filters are divided into GateWayFilter and GlobalFilter
- Use as follows:
GateWay is highly available
The high availability of GateWay is simple: multiple GateWay instances can be started to achieve high availability, and load balancing devices such as Nginx can be used upstream of GateWay for load forwarding to achieve high availability.