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

  1. When the gateway is started,Dynamic routingHow is the data loaded in
  2. Static routingwithDynamic routingPs:Static routingThis refers to the route configuration that is written to death in the configuration file
  3. Listening to theDynamic routingData source changes of
  4. What happens when the data changesNotify the gatewayThe refresh routing

 

Third, concrete implementation

The following classes are responsible for loading routing information in the Spring Cloud Gateway

  1. PropertiesRouteDefinitionLocator: read from a configuration file into the routing information (e.g., YML, Properties, etc.)
  2. RouteDefinitionRepository: read from the storage routing information (such as memory, allocation center, Redis, MySQL, etc.)
  3. 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.NacosThe 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!