How dynamic routing Spring Cloud Gateway do? Nacos very simple to achieve integration

file

I. Description

The core concept is the gateway routing configuration and routing rules, and as the entrance all requests flow in the actual production environment in order to ensure high reliability and availability, is to try to avoid the restart, so the dynamic routing is very necessary; this article introduces the Spring Cloud Gatewayideas to achieve, and to Nacosa data source to explain

PS : About Spring Cloud Zuuldynamic routing See the article " Dynamic Routing Spring Cloud Zuul's how to do it? Integrated Nacos very simple to achieve . "

 

Second, to achieve points

To implement dynamic routing can focus on the following four points

  1. When the gateway starts, 动态路由how the data is loaded to come
  2. 静态路由And 动态路由with that subject, ps: 静态路由it refers to the configuration file-coded routing configuration
  3. Monitor 动态路由the data source changes
  4. What happens when there is a change data 通知gatewayrefresh routing

 

Third, the specific implementation

Spring Cloud Gateway Load the routing information are responsible for the following categories

  1. PropertiesRouteDefinitionLocator : read the routing information from the configuration file (e.g., YML, Properties, etc.)
  2. RouteDefinitionRepository : read the routing information (such as memory, central configuration, Redis, MySQL, etc.) from the memory
  3. DiscoveryClientRouteDefinitionLocator : reading the routing information from the registry (e.g. Nacos, Eurka, Zookeeper, etc.)

 
We can customize RouteDefinitionRepositoryto achieve the purpose of dynamic routing implementation class

 

3.1. Data loading dynamic routing

Create a Nacosthe RouteDefinitionRepositoryimplementation class

NacosRouteDefinitionRepository like to see: NacosRouteDefinitionRepository.java
file

Rewriting getRouteDefinitionsmethod of routing information reading

 
Configuring Nacos listener listens routing configuration changes
file

Routing changes only need to ApplicationEventPublisherpush a RefreshRoutesEventevent instantly, gateway will automatically monitor the event and call the getRouteDefinitionsmethod update routing information

 

3.2. Create a configuration class

DynamicRouteConfig like to see: DynamicRouteConfig.java
file

 

3.3. Adding Nacosrouting configuration

file
New configuration items:

  • Data Id:scg-routes
  • Group:SCG_GATEWAY
  • Configuring Content:
[
    {
        "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": []
    }
]

Add two routing data

 

Fourth, the test

Start the gateway through /actuator/gateway/routesto view the current routing information endpoint

file

You can see Nacosthe two routes in the configuration information

 
Complete Spring Cloud Gateway Please see
https://gitee.com/zlt2000/microservices-platform/tree/master/zlt-gateway/sc-gateway

 

Recommended Reading

 
Scan code concern surprise!

file

Guess you like

Origin www.cnblogs.com/zlt2000/p/11712943.html