Now, sentinel
all the rules are stored in memory, and all rules will be lost after restarting. In a production environment, we must ensure the persistence of these rules to avoid loss.
1. Rule management mode
Whether the rules can be persisted depends on the rule management mode, and sentinel
three rule management modes are supported:
- Original mode:
Sentinel
the default mode, the rules are saved in memory, and the service will be lost after restarting the service. pull
modelpush
model
1.1. pull
Mode
pull
Mode: The console pushes the configured rules to Sentinel
the client, and the client saves the configured rules in a local file or database. In the future, we will regularly query local files or databases to update local rules.
1.2. push
Mode
push
Mode: The console pushes configuration rules to a remote configuration center, eg Nacos
. Sentinel
The client monitors Nacos
, obtains push messages of configuration changes, and completes local configuration updates.
2. Realization push
mode
2.1. Modify order-service
service
Modify OrderService
and let it listen to the rule configuration Nacos
in sentinel
. Specific steps are as follows:
2.1.1. Introducing dependencies
order-service
Introduce sentinel
the listening dependency in nacos
:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
2.1.2. Configure nacos
address
The file configuration order-service
address and monitoring configuration information in :application.yml
nacos
spring:
cloud:
sentinel:
datasource:
flow:
nacos:
server-addr: localhost:8848 # nacos地址
dataId: orderservice-flow-rules
groupId: SENTINEL_GROUP
rule-type: flow # 还可以是:degrade、authority、param-flow
2.2. Modify sentinel-dashboard
the source code
SentinelDashboard
nacos
Persistence is not supported by default , and the source code needs to be modified.
2.2.1. Download and decompress the source package
Source packages downloaded from the official website sentinel
:
Then IDEA
open the project with the following structure:
2.2.2. Modify nacos
dependencies
In sentinel-dashboard
the source code pom
file, nacos
the default dependency scope
is test
that it can only be used during testing, and it needs to be removed here:
Remove sentinel-datasource-nacos
dependencies scope
:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
2.2.3. Add nacos
support
Under sentinel-dashboard
the test
package, the support for has been written nacos
, we need to copy it under main
.
2.2.4. Modify nacos
address
Then, you also need to modify the class in the test code , modify the address NacosConfig
in it , and let it read the configuration in:nacos
application.properties
Add sentinel-dashboard
address configuration in :application.properties
nacos
nacos.addr=localhost:8848
2.2.5. Configure nacos
data source
In addition, you need to modify the classes com.alibaba.csp.sentinel.dashboard.controller.v2
under the package FlowControllerV2
to make the data source we added Nacos
take effect:
2.2.6. Modify the front-end page
Next, modify the front-end page and add a supporting nacos
menu. Modify the files src/main/webapp/resources/app/scripts/directives/sidebar/
in the directory sidebar.html
and open this part of the comment:
Modify the text in it:
2.2.7. Recompile and package the project
IDEA
The running plugin maven
, compiled and packaged modified Sentinel-Dashboard
:
2.2.8. Startup
The startup method is the same as the official one:
java -jar sentinel-dashboard.jar
If you want to modify nacos
the address, you need to add parameters:
java -jar -Dnacos.addr=localhost:8848 sentinel-dashboard.jar