A preface.
Micro service practice series is based on the open source micro service project Youlai-Mall version upgrade as the background to carry out, this is about the API Gateway to replace Zuul, interested friends can go into a star, thank you very much.
What is a microservices gateway?
Microservices gateways are layers that precede services or applications to secure, enhance, and control access to microservices.
Its common functions are:
- Authentication verification: Verifies whether authentication and authorization are performed
- Unified entrance: provides the entry point of all micro-services, plays an isolation role, and guarantees the security of services
- Current-limiting fuse
- Routing forwarding
- Load balancing
- Link to track
Iii. How to select gateway?
Why use Gateway instead of Zuul?
SpringCloud ecosystem provides two KINDS of API Gateway products, namely Netflix’s open source Zuu1 and Spring’s own Developed SpringCloud Gateway. SpringCloud takes Finchely version as the dividing line. Previous versions used Zuul as the API Gateway, and Gateway is more recommended.
Netflix has been open sourcing Zuul2 since 2018, but SpringCloud has launched Gateway and has no plans to integrate Zuul2 on Github.
Comparison and technology selection between SpringCloud Gateway and Zuul?
Iv. Project information
Youlai-mall complete project structure drawing
This article covers project modules
Project name | port | describe |
---|---|---|
nacos-server | 8848 | Registry and configuration center |
youlai-gateway | 9999 | API gateway |
youlai-admin | 8080 | Management platform |
Versions.
Nacos Server: 1.3.2 SpringBoot: 2.3.0.release SpringCloud: Hoxton.sr5 SpringCloud Alibaba: 2.2.1.releaseCopy the code
5. Actual combat of the project
1. Add the SpringCloud Gateway dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
Copy the code
2. Bootstrap. yml configuration information
server: port: 9999 spring: application: name: youlai-gateway cloud: gateway: discovery: locator: enabled: Lower-case-service-id: true # Set routes: -id: youlai-auth uri: // predicates: -path =/youlai-auth/** filters: -stripprefix =1 lb://youlai-admin predicates: - Path=/youlai-admin/** filters: - StripPrefix=1Copy the code
3. Microservice interface
Youlai-admin adds an interface method to test gateway forwarding capability
4. Gateway test
Start projects nacos-server, Youlai-admin, and Youlai-gateway in sequence
You can see when we request gateway service path http://localhost:9999/youlai-admin/users, routing according to the matching rules
The request path prefixed with /youlai-admin was forwarded to the service Youlai-admin instance.
6. Conclusion
At this point, SpringCloud has successfully integrated Gateaway, of course, only verifying the routing and forwarding function of the API gateway. Later, an article will be written about SpringCloud Gateaway integrating Oauth2 to realize the network authentication function.
Download the complete code for this article