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 Gateway
ideas to achieve, and to Nacos
a data source to explain
PS : About Spring Cloud Zuul
dynamic 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
- When the gateway starts,
动态路由
how the data is loaded to come 静态路由
And动态路由
with that subject, ps:静态路由
it refers to the configuration file-coded routing configuration- Monitor
动态路由
the data source changes - What happens when there is a change data
通知gateway
refresh routing
Third, the specific implementation
Spring Cloud Gateway
Load the routing information are responsible for the following categories
- PropertiesRouteDefinitionLocator : read the routing information from the configuration file (e.g., YML, Properties, etc.)
- RouteDefinitionRepository : read the routing information (such as memory, central configuration, Redis, MySQL, etc.) from the memory
- DiscoveryClientRouteDefinitionLocator : reading the routing information from the registry (e.g. Nacos, Eurka, Zookeeper, etc.)
We can customize RouteDefinitionRepository
to achieve the purpose of dynamic routing implementation class
3.1. Data loading dynamic routing
Create a Nacos
the RouteDefinitionRepository
implementation class
NacosRouteDefinitionRepository like to see: NacosRouteDefinitionRepository.java
Rewriting
getRouteDefinitions
method of routing information reading
Configuring Nacos listener listens routing configuration changes
Routing changes only need to
ApplicationEventPublisher
push aRefreshRoutesEvent
event instantly, gateway will automatically monitor the event and call thegetRouteDefinitions
method update routing information
3.2. Create a configuration class
DynamicRouteConfig like to see: DynamicRouteConfig.java
3.3. Adding Nacos
routing configuration
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/routes
to view the current routing information endpoint
You can see
Nacos
the 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
- Logs to troubleshoot difficult problems? Distributed link tracking log to help you
- zuul latest Sentinel integrated gateway flow control assembly
- How dynamic routing Spring Cloud Zuul do? Nacos very simple to achieve integration
- How Spring Cloud service developers to resolve conflicts and instances scurrying?
- How Spring Cloud synchronization scenario distributed transactions do? Try Seata
- How Spring Cloud scene asynchronous distributed transactions do? Try RocketMQ
Scan code concern surprise!