SpringCloud: Rule Persistence for Microservice Protection

Now, sentinelall 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 sentinelthree rule management modes are supported:

  • Original mode: Sentinelthe default mode, the rules are saved in memory, and the service will be lost after restarting the service.
  • pullmodel
  • pushmodel

1.1. pullMode

pullMode: The console pushes the configured rules to Sentinelthe 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.

insert image description here

1.2. pushMode

pushMode: The console pushes configuration rules to a remote configuration center, eg Nacos. SentinelThe client monitors Nacos, obtains push messages of configuration changes, and completes local configuration updates.

insert image description here

2. Realization pushmode

2.1. Modify order-serviceservice

Modify OrderServiceand let it listen to the rule configuration Nacosin sentinel. Specific steps are as follows:

2.1.1. Introducing dependencies

order-serviceIntroduce sentinelthe 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 nacosaddress

The file configuration order-serviceaddress and monitoring configuration information in :application.ymlnacos

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-dashboardthe source code

SentinelDashboardnacosPersistence 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:

insert image description here

Then IDEAopen the project with the following structure:

insert image description here

2.2.2. Modify nacosdependencies

In sentinel-dashboardthe source code pomfile, nacosthe default dependency scopeis testthat it can only be used during testing, and it needs to be removed here:

insert image description here

Remove sentinel-datasource-nacosdependencies scope:

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

2.2.3. Add nacossupport

Under sentinel-dashboardthe testpackage, the support for has been written nacos, we need to copy it under main.

insert image description here

2.2.4. Modify nacosaddress

Then, you also need to modify the class in the test code , modify the address NacosConfigin it , and let it read the configuration in:nacosapplication.properties

insert image description here

Add sentinel-dashboardaddress configuration in :application.propertiesnacos

nacos.addr=localhost:8848

2.2.5. Configure nacosdata source

In addition, you need to modify the classes com.alibaba.csp.sentinel.dashboard.controller.v2under the package FlowControllerV2to make the data source we added Nacostake effect:

insert image description here

2.2.6. Modify the front-end page

Next, modify the front-end page and add a supporting nacosmenu. Modify the files src/main/webapp/resources/app/scripts/directives/sidebar/in the directory sidebar.htmland open this part of the comment:

insert image description here

Modify the text in it:

insert image description here

2.2.7. Recompile and package the project

IDEAThe running plugin maven, compiled and packaged modified Sentinel-Dashboard:

insert image description here

2.2.8. Startup

The startup method is the same as the official one:

java -jar sentinel-dashboard.jar

If you want to modify nacosthe address, you need to add parameters:

java -jar -Dnacos.addr=localhost:8848 sentinel-dashboard.jar

Guess you like

Origin blog.csdn.net/qq_37726813/article/details/130674609