6-cloudalibaba sentinel (rule persistence) learning notes 2020.10.25

6-cloudalibaba sentinel (rule persistence) learning notes 2020.10.25

Preface:( Official website ,Dynamic rules)

Previously, the rules we learned to configure were temporary. As long as the micro service application is restarted, the rules disappear and need to be reconfigured.

In this way, there will be more and more troublesome things in the follow-up after there are more services. Next, configure the Push mode rule persistence to solve the temporary problem.

Rule management and push

Generally speaking, there are three modes for pushing rules:

Push mode explain advantage shortcoming
Original mode The rule is written directly to the memory source and pushed to the client( WritableDataSource) Simple without any dependency Consistency is not guaranteed; The rule is saved in memory and disappears after restart. Seriously not recommended for production environments
Pull mode Extended write data source( WritableDataSource ), the client actively polls and pulls rules from a rule management center, which can be RDBMS, files, etc Simple without any dependence; Rule persistence Consistency is not guaranteed; The real-time performance is not guaranteed. If you pull too frequently, there may be performance problems.
Push mode Extended read data source( ReadableDataSource ), the rule center pushes uniformly, and the client monitors changes at any time by registering a listener, such as using Nacos, Zookeeper and other configuration centers. This method has better real-time and consistency guarantee. push mode data source is generally used in production environment. Rule persistence; uniformity; fast Introducing third-party dependencies

1.0 configure rule persistence into Nacos configuration center

1.1 introducing dependencies

 <!--SpringCloud ailibaba sentinel-datasource-nacos  sentinel Rule persistence-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

1.2 application. Configure the nacos data source configuration in the YML configuration file

server:
  port: 8080
spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: 119.xx.xx.xxx:8848 # Nacos service registry address
    sentinel:
      transport:
        dashboard: localhost:8090 # Configure Sentinel dashboard address
        port: 8719
      datasource:
        # The name is optional (but it is best to see the name)
        flow:
          nacos:
            server-addr: 119.xx.xx.xx:8868
            dataId: ${spring.application.name}-flow-rules #Default Nacos adapted dataId Convention
            groupId: SENTINEL_GROUP #groupId Convention for default Nacos adaptation
            ## Rule type, see: org springframework. cloud. alibaba. sentinel. datasource. RuleType
            rule-type: flow
        degrade:
          nacos:
            server-addr: 119.xx.xx.xx:8868
            dataId: ${spring.application.name}-degrade-rules
            groupId: SENTINEL_GROUP
            rule-type: degrade
        param-flow:
          nacos:
            server-addr: 119.xx.xx.xx:8868
            dataId: ${spring.application.name}-param-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: param-flow

1.3 console transformation( Official website)

Console transformation is mainly for rule implementation. You need to modify the source code from https://github.com/alibaba/Sentinel/releases Download the source code from the address and enter the sentinel dashboard directory for modification

Starting from Sentinel 1.4.0, we extracted the interface to push and pull rules to the remote configuration center:

  • Dynamicreuleprovider: pull rule
  • DynamicRulePublisher: push rule

1.3.1 modify POM XML file [under sentinel dashboard module]

C:\Users\Administrator\Desktop\Sentinel-1.8.0\Sentinel-1.8.0\sentinel-dashboard

POM in XML file, remove the scope < scope > test < / scope >

		<!-- for Nacos rule publisher sample -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <scope>test</scope>
        </dependency>

1.3.2 extract relevant classes

Find the sentinel dashboard / SRC / test / Java / COM / Alibaba / CSP / sentinel / Dashboard / rule / Nacos directory and copy the entire directory to sentinel dashboard / SRC / main / Java / COM / Alibaba / CSP / sentinel / Dashboard / rule / Nacos

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-xxezbtke-1603695589612)( https://s1.ax1x.com/2020/10/26/BnYqBQ.jpg )]

1.3.3 you need to specify the corresponding bean in FlowControllerV2 to enable Nacos adaptation

Then, just specify the corresponding bean in FlowControllerV2 to enable the Nacos adaptation. The front-end page needs to be switched manually or the front-end routing configuration needs to be modified (sidebar.html flow control rule routing can be changed from dashboard.flowV1 to dashboard.flow. Note that the dialog box of cluster point link page needs to be modified by itself)

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-io2x3syo-1603695589616)( https://s1.ax1x.com/2020/10/26/Bntb26.jpg )]

The dataId and groupId conventions of the default Nacos adapter are as follows:

  • groupId: SENTINEL_GROUP
  • Flow control rule dataId: {appName} - flow rules. For example, if the application name is appA, the dataId is appA flow rules

Users can modify the corresponding groupId and dataId postfix in NacosConfigUtil. You can configure the corresponding Converter in NacosConfig. By default, the decoder and encoder of FlowRuleEntity are provided.

1.3.4 modify front-end routing configuration (sidebar.html)

Modification: the file path is located in sentinel dashboard / SRC / main / webapp / resources / APP / scripts / directives / sidebar / sidebar html

sidebar.html flow control rule routing from dashboard Change flowv1 to dashboard Flow. Note that the dialog box of cluster point link page needs to be modified by itself

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG heromymw-1603695589619)( https://s1.ax1x.com/2020/10/26/Bndede.jpg )]

Since then, the flow control rule has been changed to Push persistence, and other rules will be modified accordingly

1.3.5 modify cluster link page dialog box

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-4xg7z7mx-1603695589623)( https://s1.ax1x.com/2020/10/26/Bn0wrV.jpg )]

1.3.6 application. Add the nacos address parameter to the YML configuration file and read it in the background

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zcoufe50-1603695589625)( https://s1.ax1x.com/2020/10/26/BnBaJH.jpg )]

1.3.7 recompile and package

Go back to the project C:\Users\Administrator\Desktop\Sentinel-1.8.0\Sentinel-1.8.0 home directory (with pom.xml), and execute MVN clean package - dmaven test. skip=true

Locate sentinel dashboard. In the target directory of the project Jar, execute Java - dserver port=8090 -Dcsp. sentinel. dashboard. server=localhost:8090 -Dproject. name=sentinel-dashboard -jar sentinel-dashboard.jar

Refer to: https://www.cnblogs.com/fx-blog/p/11726189.html

1.4 restart the micro service application test

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-bzcb1lql-1603695589627)( https://s1.ax1x.com/2020/10/26/Bn5UzV.jpg )]

After the above configuration is completed, the restart application rule still exists and will take effect.

However, only current limited persistence is configured.

Extension:

1.0 increase degradation persistence

In the modified console source code package, copy DegradeController and configure a DegradeController v2

1.1 rewrite a DegradeControllerV2

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-deydlei2-1603695589630)( https://s1.ax1x.com/2020/10/26/BuVlC9.jpg )]

1.2 add DegradeRuleNacosProvider provider and publisher DegradeRuleNacosPublisher

@Component("degradeRuleNacosProvider")
public class DegradeRuleNacosProvider implements DynamicRuleProvider<List<DegradeRuleEntity>> {

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<String, List<DegradeRuleEntity>> converter;

    @Override
    public List<DegradeRuleEntity> getRules(String appName) throws Exception {
        								Modify the file name of the build configuration center
        String rules = configService.getConfig(appName + NacosConfigUtil.DEGRADE_DATA_ID_POSTFIX,
            NacosConfigUtil.GROUP_ID, 3000);
        if (StringUtil.isEmpty(rules)) {
            return new ArrayList<>();
        }
        return converter.convert(rules);
    }
}
 //------------------------------------------------------------------------------------
@Component("degradeRuleNacosPublisher")
public class DegradeRuleNacosPublisher implements DynamicRulePublisher<List<DegradeRuleEntity>> {

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<List<DegradeRuleEntity>, String> converter;

    @Override
    public void publish(String app, List<DegradeRuleEntity> rules) throws Exception {
        AssertUtil.notEmpty(app, "app name cannot be empty");
        if (rules == null) {
            return;
        }
        										//Modify the file name of the build configuration center
        configService.publishConfig(app + NacosConfigUtil.DEGRADE_DATA_ID_POSTFIX,
            NacosConfigUtil.GROUP_ID, converter.convert(rules));
    }
}

1.4 add converter < list < degraderuleentity >, string > converter

@Configuration
public class NacosConfig {
	
	
	@Value("${spring.cloud.config.nacos.server-addr}")
    private String serviceAddr;
	

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

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

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

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

    @Bean
    public ConfigService nacosConfigService() throws Exception {
      //return ConfigFactory.createConfigService("localhost");
        return ConfigFactory.createConfigService(serviceAddr);
    }
}

1.5 adding constant name to nacosconfigutil

public static final String DEGRADE_DATA_ID_POSTFIX = "-degrade-rules";

1.6 start the console for testing

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-pz8bqtat-1603695589632)( https://s1.ax1x.com/2020/10/26/Bue5n0.jpg )]

The test rule still exists after restarting the application.

demo of personal learning configuration

1

You can also use the online hosting Sentinel console provided by Alibaba cloud: AHAS

Tags: Java Spring Cloud alibaba

Posted by hagen00 on Tue, 10 May 2022 09:00:26 +0300