Microservice Gateway -Gateway
preface
In the recent project, every time a new interface is added, WE need to submit JIRA work order and apply for the interface permission on the company’s gateway platform. So what is a gateway? Why do we use gateways… A series of questions came to mind.
The body of the
Introduction to the Gateway The gateway is the only external access of the system. It is the middle layer between the client and server. It processes non-service functions and provides routing requests, authentication, monitoring, caching, and traffic limiting functions.
Different microservices generally have different network addresses, and the client may need to invoke multiple service interfaces to complete a business requirement. If the client is allowed to directly communicate with each microservice, the following problems will occur:
- The client may request different microservices multiple times, increasing the complexity of the client
- There are cross-domain requests, which are relatively complex to handle
- Authentication is complex and each service requires independent authentication
- Difficult to refactor, multiple services may merge into one or split into multiple
The microservice gateway is in the middle layer between the server and the client. All external service requests will first pass through the microservice gateway. Customers can only interact with the microservice gateway without invoking specific microservice interfaces, which simplifies development
There are two main gateways used by microservices: Zuul and Gateway. Here is a brief comparison:
- In terms of performance, the performance of the two products is similar under the scenario of low traffic.
- In high concurrency scenarios
Gateway
Performance is much better. In terms of development,Zuul1
The programming model is simple and easy to expand; Gateway
The programming model is a bit more difficult, the code is much more difficult to read than Zuul, and the extensions are a bit more complex.
GateWay is in a sense a replacement for Zuul.
So let’s focus on thatSpring Cloud Gateway
The project provides a library for building API gateways on top of Spring MVC.Spring Cloud Gateway
Designed to provide a simple and efficient way to route apis and provide them with cross-domain concerns such as: security, monitoring/metrics, and resiliency.
The Spring Cloud Gateway implements the following functions:
- Based on the
Spring Framework 5
.Project Reactor
andSpring Boot 2.0
On top of - Ability to match routes on any request attribute. Predicates and filters are specific to routes
Hystrix
Circuit breaker integrationSpring Cloud DiscoveryClient
integration- Easy to write predicates and filters
- Request rate limiting, or traffic limiting
- The path to rewrite
Spring Cloud Gateway Quick start
For Nacos setup, see my blog :Nacos Service Governance Center and Configuration Center
The project structure
spring-gateway-demo
- Spring-gateway-route: indicates the gateway module of the project
- Spring-gatewat-user: User test module for the project
spring-gateway-route
dependent
<dependencies> <! -- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.01.</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.12..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.12..RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.12..RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Copy the code
application.properties
server.port=9000
spring.application.name=spring-gateway-route
spring.main.allow-bean-definition-overriding=true
spring.cloud.nacos.discovery.server-addr=127.0. 01.:8848The gateway enables service registration and discovery, and spring Cloud Gateway automatically creates a router for each service based on service discovery. This router forwards the request path starting with the service name to the corresponding service. spring.cloud.gateway.discovery.locator.enabled=trueThe service name on the request path is set to lowercase (because the service name was capitalized when registering with the registry), rather than the request path with /service-hi/ being routed to the service named service-hi. spring.cloud.gateway.discovery.locator.lower-case-service-id=true
spring.cloud.gateway.routes[0].id=spring-gateway-user
spring.cloud.gateway.routes[0].uri=lb://spring-gateway-user
spring.cloud.gateway.routes[0].predicates[0]=Path=/user/ * *Copy the code
spring-gatewat-user
dependent
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.12..RELEASE</version>
</dependency>
</dependencies>
Copy the code
application.properties
server.port=8080
spring.application.name=spring-gateway-user
spring.main.allow-bean-definition-overriding=true
spring.cloud.nacos.discovery.server-addr=127.0. 01.:8848
Copy the code
test
Start both projects separately, as can be seen in the Nacos admin console
The test interface
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping ("/hello")
public String hello(String input){
return "Hello World"+input; }}Copy the code
Access test interface
- The Gateway forwards the request to the specified server
- localhost:9000/user/hello? Input = The imagination of Xiyuan