SpringBoot 2.0 + Nacos + Sentinel Flow Control Rule Centralized Storage

Keywords: Java Spring xml JSON github

Preface

Sentinel's native version of Rule Management pushes rules to the client through the API and updates them directly to memory, not directly for production environments.Officially, however, there is also a Push mode that extends the Readable DataSource for reading data sources, unifies the Rule Center push, and allows clients to monitor changes by registering listeners, such as using configuration centers such as Nacos and Zookeeper.This approach provides better real-time and consistency assurance.Here we implement a unified storage configuration of flow control rules by configuring Nacos.

Framework

Console pushes rules to the configuration center, and client obtains flow control rules from the configuration center by listening for events.

Client Configuration

Introduction of pom.xml:

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

Profile:

# Access address for nacos, configuration reference https://blog.52itstyle.vip/archives/4174/ 
spring.cloud.sentinel.datasource.ds.nacos.server-addr=47.104.187.19:8848
#The dataId in nacos that stores the rules, using the ${spring.application.name} variable for the dataId, allows you to distinguish between different rule configurations based on the application name
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-flow-rules
#groupId for storing rules in nacos
spring.cloud.sentinel.datasource.ds.nacos.groupId=SENTINEL_GROUP
#Define stored rule types
spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow

Console Configuration

Modify pom.xml to remove the original <scope>test</scope>

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

Copy the package com.alibaba.csp.sentinel.dashboard.rule.nacos under src/test to src/main/java.

Modify NacosConfig:

/**
 * @author Eric Zhao
 * @since 1.4.0
 */
@Configuration
public class NacosConfig {

    @Value("${nacos.address}")
    private String address;

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

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

    @Bean
    public ConfigService nacosConfigService() throws Exception {
        Properties properties = new Properties();
        properties.put("serverAddr",address);
        return ConfigFactory.createConfigService(properties);
    }
}

application.properties configuration introduces Nacos:

# Access address for nacos, configuration reference https://blog.52itstyle.vip/archives/4174/ 
nacos.address=47.104.197.19:8848

FlowController V2 specifies that the corresponding Bean opens the Nacos adapter.

@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

Modify sidebar.html page to change flow control rule routing from dashboard.flowV1 to dashboard.flow

<-- nacos Dynamic Rule Configuration-->
<li ui-sref-active="active" ng-if="!entry.isGateway">
      <a ui-sref="dashboard.flow({app: entry.app})">
      <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;Flow Control Rules</a>
</li>

As shown in the diagram, there will be an additional button to return to the single machine page, here we add a new flow control rule.

Log in to the Nacos background, Configuration Management - > Configuration List:

Click to enter the configuration details as follows:

[{
    "app": "blog",
    "clusterConfig": {
        "fallbackToLocalWhenFail": true,
        "sampleCount": 10,
        "strategy": 0,
        "thresholdType": 0,
        "windowIntervalMs": 1000
    },
    "clusterMode": false,
    "controlBehavior": 0,
    "count": 2.0,
    "gmtCreate": 1568617671812,
    "gmtModified": 1568622253889,
    "grade": 1,
    "id": 6,
    "ip": "10.136.168.88",
    "limitApp": "default",
    "port": 8720,
    "resource": "blogView",
    "strategy": 0
}]

Summary

In production environments, the correct practice for push rules should be to configure the central console/Sentinel console_Configuration center_Sentinel data source_Sentinel.

case

https://gitee.com/52itstyle/spring-boot-blog

Reference resources

https://github.com/alibaba/Sentinel

https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard

Posted by able on Thu, 19 Sep 2019 17:56:05 -0700