天天看點

SpringBoot 2.0 + Nacos + Sentinel 流控規則集中存儲

前言

Sentinel 原生版本的規則管理通過API 将規則推送至用戶端并直接更新到記憶體中,并不能直接用于生産環境。不過官方也提供了一種 Push模式,擴充讀資料源

ReadableDataSource

,規則中心統一推送,用戶端通過注冊監聽器的方式時刻監聽變化,比如使用 Nacos、Zookeeper 等配置中心。這種方式有更好的實時性和一緻性保證。這裡我們通過配置 Nacos 來實作流控規則的統一存儲配置。

架構

SpringBoot 2.0 + Nacos + Sentinel 流控規則集中存儲

控制台推送規則至配置中心,用戶端通過監聽事件從配置中心擷取流控規則。

用戶端配置

pom.xml 引入:

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

配置檔案:

# nacos的通路位址,配置參考 https://blog.52itstyle.vip/archives/4174/ 
spring.cloud.sentinel.datasource.ds.nacos.server-addr=47.104.187.19:8848
#nacos中存儲規則的dataId,對于dataId使用了${spring.application.name}變量,這樣可以根據應用名來區分不同的規則配置
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-flow-rules
#nacos中存儲規則的groupId
spring.cloud.sentinel.datasource.ds.nacos.groupId=SENTINEL_GROUP
#定義存儲的規則類型
spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow           

控制台配置

修改 pom.xml,原來的

<scope>test</scope>

去掉:

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

src/test

下面的包

com.alibaba.csp.sentinel.dashboard.rule.nacos

拷貝到

src/main/java

下面。

修改 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 配置引入 Nacos:

# nacos的通路位址,配置參考 https://blog.52itstyle.vip/archives/4174/ 
nacos.address=47.104.197.19:8848           

FlowControllerV2

指定對應的 Bean 開啟 Nacos 适配。

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

修改

sidebar.html

頁面, 流控規則路由從

dashboard.flowV1

改成

dashboard.flow

<-- nacos 動态規則配置-->
<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;流控規則</a>
</li>           

如圖所示,界面會多了一個回到單機頁面的按鈕,這裡我們新增一個流控規則。

SpringBoot 2.0 + Nacos + Sentinel 流控規則集中存儲

登入 Nacos 背景,配置管理->配置清單:

SpringBoot 2.0 + Nacos + Sentinel 流控規則集中存儲

點選進入配置詳情,配置内容如下:

[{
    "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
}]           

小結

生産環境下,推送規則正确做法應該是 配置中心控制台/Sentinel 控制台 → 配置中心 → Sentinel 資料源 → Sentinel。

案例

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

參考

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