There are a few things you need to clear up before you can formally learn about the gateway
- What is a service gateway
- Why is a service gateway needed
- Third, SpringCloud Gateway
What is a gateway?
The API gateway is a server that is a unique entry point to the system. From an object-oriented design perspective, it is similar to the appearance pattern. The API gateway encapsulates the internal architecture of the system and provides a custom API for each client. It may also have other responsibilities, such as authentication, monitoring, load balancing, caching, request sharding and management, and static response processing. The core point of the API gateway approach is that all clients and consumers access microservices through a unified gateway and handle all non-business functions at the gateway layer. Typically, gateways also provide REST/HTTP access apis.
Why do you need a service gateway?
The service gateway has the following functions:
- Performance: API high availability, load balancing, fault tolerance.
- Security: permission authentication, desensitization, traffic cleaning, back-end signature (to ensure that the whole link trusted calls), blacklist (illegal calls limit).
- Log: Log records (SPainID,traceid) are required for full link tracing.
- Cache: Data cache.
- Monitoring: record request response data, API time analysis, performance monitoring.
- Current limiting: flow control, off-peak flow control, can define a variety of current limiting rules.
- Gray scale: Online gray scale deployment can reduce risks.
- Routing: Dynamic routing rules.
A simple microservice architecture is shown below:
The overall process
- The client sends the request
- Nginx load balancing to the API gateway
- The SpringCloud Gateway loads to the service, which does the processing
SpringCloud Gateway
How to learn something new quickly? Go to the official website, of course! The official website is linked to spring-cloud-gateway
Routing – the 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.
Configuration file mode
server:
port: 8080
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: service1 ## id
uri: lb://service1 Lb: indicates load balancing
predicates:
-Path=/service1/api ## Use the implementation class PathPredicate for GatewayPredicate
filter:
-StripPerfix=1 # # use GatewayFilter StripPerfixGatewayFilterFactory implementation class
Copy the code
Code injection
@Bean
public RouteLocator service1Locator(RouteLocatorBuilder builder) {
return builder.routes()
.route("service1", r -> r.path("/service1/api")
.filters(f->f.stripPrefix(1))
.uri("lb://service1"))
.build();
}
Copy the code
As for other assertions or filters, I won’t write more here, but you can refer to the big guy’s article on the most complete SpringCloud Gateway ever