Sentinel rule configuration, once we restart the service, all the rules will disappear. These rule configurations can be stored in configuration centers such as Zookeeper, Applo, and Nacos so that configuration data can be shared when services are restarted or multiple nodes are started.
In the previous project, WE used Nacos as the service registry and service configuration center. We can also use Nacos as the Sentinel configuration data storage warehouse to provide us with the persistence of Sentinel configuration data. As shown in the following figure
Added Nacos storage dependency
The following dependencies need to be introduced independently to state that sentinel’s rule data is stored in NACOS.
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
Copy the code
Add the YML configuration
If we use Sentinel’s stand-alone data source capability, we need to declare the data source configuration information in the YAML file.
spring:
application:
name: stock-service
profiles:
active: dev
cloud:
sentinel:
transport:
port: 8719
dashboard: localhost:8080
enabled: true
datasource:
ds:
nacos:
dataId: ${spring.application.name}-flow-rules
groupId: DEFAULT_GROUP
data‐type: json
rule‐type: flow
Copy the code
Nacos Added configuration
In the Nacas configuration center, we need to add configuration information, here we need to pay attention to the dataId and groupId must not be wrong, otherwise the data configuration will not be found, as shown in the following figure:
Data templates
[{"resource": "ABC ", "controlBehavior": 0, "count": 20.0, "grade": 1, "limitApp": "default", "strategy": 0}]Copy the code
Sentinel Configuration
Finally we start the service and Sentinel console, and you can see the flow control information that we defined in the configuration center under the flow Control Information menu.
conclusion
At this point, we are done with the persistence of Sentinel rules to Nacos. There is a small problem that we can not directly synchronize to Nacos for persistence after Sentinel changes the rules. If we need to persist to Nacos, we can modify the source code of Sentinel console to synchronize the configuration information to Nacos through “push mode”.
Sentinel provides sample implementations of push and pull rules for Nacos, ZooKeeper, and Apollo (located under the Test directory). Take Nacos as an example. If you want to use Nacos as the dynamic rule configuration center, you can extract the relevant classes and specify the corresponding bean in FlowControllerV2 to enable Nacos adaptation. You need to manually switch the front-end page or modify the front-end route configuration (the sidebar. HTML flow control rule route can be changed from dashboard.flowV1 to dashboard.flow. Note that the link dialog box of cluster points needs to be modified).
reference
Github.com/alibaba/Sen…