One 、 Why choose SpringCloud Gateway instead of Zuul?
Gateway and Zuul The same responsibilities as , It's all about request distribution , similar Nginx Distribute to back end servers .
1.SpingCloud Gateway and SpringCloud Zuul comparative analysis
(1) The same thing
- The bottom is servlet
- Both are web gateway , Dealing with http request
(2) Difference
a. Internal implementation
gateway contrast zuul More dependence on spring-webflux, stay spring With the support of , More powerful , Internal current limiting is realized 、 Load balancing, etc. , And it's more scalable , But at the same time, it's also limited to be suitable for Spring Cloud Kit ;
zuul It can be extended to other microservice frameworks , There is no current limiting inside 、 Load balancing, etc. .
b. Is asynchrony supported
zuul Only synchronization... Is supported ;
gateway Asynchronous Support ( Theoretically gateway It is more suitable for improving system throughput ( But not necessarily better performance ), The final performance also needs to be determined by a rigorous pressure test ).
c. The angle of frame design
gateway With better scalability , And it's been released 2.0.0 Of RELESE edition , Stability is also very good .
d. performance
Zuul and Gateway Which is better , A friend did a special test and wrote an article :
Micro service gateway selection :spring cloud gateway、zuul 1 Performance comparison test
e. Current limiting
Zuul2: This can be done through a configuration file or filter Realization ;
Gateway: But for IP、 user 、 Cluster to limit current , And provide extension interface .
f. authentication
Zuul2:filter Middle code implementation ;
Gateway: General authentication 、auth2.0.
g. monitor
Zuul2:filter Middle code implementation ;
Gateway:Gateway Metrics Filter Realization .
h. Ease of use
Zuul2: There are few references ;
Gateway: Simple and easy to use .
(3) Architecture diagram
a.Zuul2 Internal architecture
b.Gateway Internal architecture
2. It's time to choose Gateway still Zuul?
My view is to combine the business scenario with the actual situation . For example , If it's a new project, you can use Gateway, If it's a redevelopment project , And that project gateway uses Zuul, Suggest not to change , Keep the status quo , Until you really understand the project and the underlying framework , You can try to change ( In the end, whether or not to change depends on how high the cost is , If it's too high , Still don't change ).
Two 、SpringCloud Integrate Gateway
1. Import Maven rely on
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
2. The configuration file
server:
port: 8080
spring:
cloud:
gateway:
discovery:
locator:
lowerCaseServiceId: true
enabled: true
routes:
# authentication center
- id: blog-api
uri: lb://blog-api
predicates:
- Path=/api/**
filters:
- StripPrefix=1
application:
name: blog-gateway-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3. Start and test
It's very simple to integrate , The key is two :
First of all ,SpringCloud and SpringBoot The version should be compatible with ;
second , The configuration file should be for , Otherwise, there will be such a problem , Can start normally , However, the following microservices cannot be accessed through the gateway .
If a friend is right Zuul Interested in , Please refer to my article :
SpringCloud And Zuul
The references are as follows :
Microservice gateway Zuul and Gateway The difference between
SpringCloud Gateway New gateway and zuul The comparative selection of
Micro service gateway selection :spring cloud gateway、zuul 1 Performance comparison test
Zuul and Gateway contrast