Sentinel rule persistence

Sentinel rule persistence

It is very troublesome to modify the source code a lot, and only a small part of it has been modified below.
Ali has a paid sentinel cloud service. The open source sentinel did not implement the best version. Maybe it is to make way for the paid version.

1. Modify the order-service service

Modify OrderService to listen to the sentinel rule configuration in Nacos.

Specific steps are as follows:

1. Introduce dependencies

Introduce sentinel to monitor nacos dependencies in order-service:

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

2. Configure nacos address

Configure the nacos address and monitor configuration information in the application.yml file in order-service:

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

Copy and paste directly from datasource

insert image description here


The second step of modification is very troublesome, you can skip it, and use the packaged one directly

Link: https://pan.baidu.com/s/1mffAD62BZt3IDp59NxO7OQ
Extraction code: hzan


2. Modify the source code of sentinel-dashboard

SentinelDashboard does not support nacos persistence by default, and the source code needs to be modified.

1. Unzip

Unzip the sentinel source package in the pre-class materials:

insert image description here

Then open the project with IDEA, the structure is as follows:

insert image description here

2. Modify nacos dependencies

In the pom file of the sentinel-dashboard source code, nacos depends on the default scope of test, which can only be used during testing. Here it needs to be removed:

insert image description here

insert image description here

Remove the scope that sentinel-datasource-nacos depends on:

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

3. Add nacos support

Under the test package of sentinel-dashboard, support for nacos has been written, and we need to copy it to main.

insert image description here

4. Modify nacos address

Then, you also need to modify the NacosConfig class in the test code:
the copied
insert image description here

Modify the nacos address and let it read the configuration in application.properties:

insert image description here

/**
 * @author Eric Zhao
 * @since 1.4.0
 */
@Configuration
@ConfigurationProperties(prefix = "nacos")
public class NacosConfig {
    
    

    // nacos地址
    private String addr;

    @Bean
    public ConfigService nacosConfigService() throws Exception {
    
    
        return ConfigFactory.createConfigService(addr);
    }

    public String getAddr() {
    
    
        return addr;
    }

    public void setAddr(String addr) {
    
    
        this.addr = addr;
    }

    @Bean
    public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
    
    
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
    
    
        return s -> JSON.parseArray(s, FlowRuleEntity.class);
    }


}

Add nacos address configuration in application.properties of sentinel-dashboard:

nacos.addr=localhost:8848

5. Configure nacos data source

In addition, you need to modify the FlowControllerV2 class under the com.alibaba.csp.sentinel.dashboard.controller.v2 package:

insert image description here

Let the Nacos data source we added take effect:

insert image description here

6. Modify the front-end page

Next, modify the front-end page and add a menu that supports nacos.

Modify the sidebar.html file in the src/main/webapp/resources/app/scripts/directives/sidebar/ directory:

insert image description here

Turn on this part of the comment:

insert image description here

Modify the text in it:

insert image description here

<li ui-sref-active="active" ng-if="entry.appType==0">
  <a ui-sref="dashboard.flow({app: entry.app})">
    <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则-NACOS</a>
</li>

7. Recompile and package the project

Run the maven plug-in in IDEA, compile and package the modified Sentinel-Dashboard:

insert image description here

If you report an error, clean first, then package

insert image description here

The packaged files are as follows, you can use them directly, don’t change them yourself, it’s really troublesome

Link: https://pan.baidu.com/s/1YpHITSBC3dUSAj8zQF_niQ
Extraction code: hzan

Copy it to a directory and rename it to distinguish

insert image description here

8. start

The startup method is the same as the official one:

java -jar sentinel-dashboard.jar

If you want to modify the nacos address, you need to add parameters: (you can dynamically specify the address of nacos, the address of external dependencies, of course, you must dynamically specify it)

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

9. Browser-side testing

First log in to nacos normally, then access a resource and trigger a cluster point link

Then maybe the address has not been changed. The previous official nacos front end has a cache, which needs to be cleared. The operation of clearing the specified web page cache is as shown in the figure below.
insert image description here

Then there will be our modified flow control rules based on nacos persistence
insert image description here

Then 流控规则-nacosthe newly created rules under the menu can be persisted
. Other menus still can’t, and they have to be modified one by one, so the amount of modification will be very large, and this is only a small part of the modification.

add try
insert image description here

Look under nacos: there is an extra configuration item: orderservice-flow-rules
insert image description here

Visit: http://localhost:10010/order/101?authorization=admin

indeed restricted
insert image description here

Restart the OrderApplication microservice, it will be lost before, but it will not be lost now
(create a new rule under the other menu, then restart the OrderApplication microservice to check that it is indeed lost)

10. The modified sentinel rule can persist the file

The packaged files are as follows, you can use them directly, don’t change them yourself, it’s really troublesome

Link: https://pan.baidu.com/s/1YpHITSBC3dUSAj8zQF_niQ
Extraction code: hzan

Guess you like

Origin blog.csdn.net/hza419763578/article/details/132306492