A, description,
The core concept of gateway is routing configuration and routing rules. As the entrance of all request traffic, it is necessary to avoid restarts in the actual production environment to ensure high reliability and availability. Therefore, dynamic routing is very necessary. This paper mainly introduces the implementation of Spring Cloud Gateway, and Nacos as the data source to explain
For more information about dynamic routing in Spring Cloud Zuul, see the article “How to Do Dynamic Routing in Spring Cloud Zuul?
2. Key points of implementation
To implement dynamic routing, focus on the following four points
- When the gateway is started,
Dynamic routing
How is the data loaded in Static routing
withDynamic routing
Ps:Static routing
This refers to the route configuration that is written to death in the configuration file- Listening to the
Dynamic routing
Data source changes of - What happens when the data changes
Notify the gateway
The refresh routing
Third, concrete implementation
The following classes are responsible for loading routing information in the Spring Cloud Gateway
- PropertiesRouteDefinitionLocator: read from a configuration file into the routing information (e.g., YML, Properties, etc.)
- RouteDefinitionRepository: read from the storage routing information (such as memory, allocation center, Redis, MySQL, etc.)
- DiscoveryClientRouteDefinitionLocator: read from the registry of routing information (such as Nacos, Eurka, Zookeeper, etc.)
We can customize RouteDefinitionRepository implementation class to achieve the purpose of dynamic routing
3.1. Realize data loading of dynamic routing
Create a Nacos RouteDefinitionRepository implementation class
NacosRouteDefinitionRepository class to view: NacosRouteDefinitionRepository. Java
Rewrite getRouteDefinitions to read routing information
Configure Nacos listeners to listen for route configuration changes
Routing change only need to ApplicationEventPublisher push a RefreshRoutesEvent event immediately, the gateway will automatically monitor the event and call getRouteDefinitions method to update the routing information
3.2. Create a configuration class
DynamicRouteConfig class: DynamicRouteConfig
Add 3.3.Nacos
The routing configuration
- Data Id: SCG – routes
- Group: SCG_GATEWAY
- Configuration contents:
[{"id": "csdn"."predicates": [{
"name": "Path"."args": {
"pattern": "/csdn/**"}}]."uri": "https://www.csdn.net/"."filters": []}, {"id": "github"."predicates": [{
"name": "Path"."args": {
"pattern": "/github/**"}}]."uri": "http://github.com/"."filters": []}]Copy the code
Add two routes
Four, test,
Start the gateway and use /actuator/gateway/routes endpoints to view the current routing information
You can see the two routes configured in Nacos
See the complete Spring Cloud Gateway code
Gitee.com/zlt2000/mic…
Recommended reading
- Log troubleshooting difficulty? Distributed log link tracing to help you
- Zuul integrates Sentinel’s latest gateway flow control components
- How to do Spring Cloud Zuul dynamic routing? Integrating the Nacos implementation is simple
- How can Spring Cloud developers resolve service conflicts and instance hopping?
- How to do distributed transactions in Spring Cloud synchronization scenarios? Try Seata
- How to do distributed transactions in Spring Cloud asynchronous scenario? Try RocketMQ
Scan code attention has surprise!