前言
Sentinel 原生版本的規(guī)則管理通過API 將規(guī)則推送至客戶端并直接更新到內(nèi)存中,并不能直接用于生產(chǎn)環(huán)境。不過官方也提供了一種 Push模式,擴(kuò)展讀數(shù)據(jù)源
ReadableDataSource,規(guī)則中心統(tǒng)一推送,客戶端通過注冊(cè)監(jiān)聽器的方式時(shí)刻監(jiān)聽變化,比如使用 Nacos、Zookeeper
等配置中心。這種方式有更好的實(shí)時(shí)性和一致性保證。這里我們通過配置 Nacos 來實(shí)現(xiàn)流控規(guī)則的統(tǒng)一存儲(chǔ)配置。
架構(gòu)
控制臺(tái)推送規(guī)則至配置中心,客戶端通過監(jiān)聽事件從配置中心獲取流控規(guī)則。
客戶端配置
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中存儲(chǔ)規(guī)則的dataId,對(duì)于dataId使用了${spring.application.name}變量,這樣可以根據(jù)應(yīng)用名來區(qū)分不同的規(guī)則配置
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-flow-rules
#nacos中存儲(chǔ)規(guī)則的groupId
spring.cloud.sentinel.datasource.ds.nacos.groupId=SENTINEL_GROUP #定義存儲(chǔ)的規(guī)則類型
spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow
控制臺(tái)配置
修改 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 指定對(duì)應(yīng)的 Bean 開啟 Nacos 適配。
@Autowired @Qualifier("flowRuleNacosProvider") private
DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; @Autowired
@Qualifier("flowRuleNacosPublisher") private
DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
修改sidebar.html頁面, 流控規(guī)則路由從 dashboard.flowV1 改成 dashboard.flow
<-- nacos 動(dòng)態(tài)規(guī)則配置--> <li ui-sref-active="active" ng-if="!entry.isGateway"> <a
ui-sref="dashboard.flow({app: entry.app})"> <i class="glyphicon
glyphicon-filter"></i> 流控規(guī)則</a> </li>
如圖所示,界面會(huì)多了一個(gè)回到單機(jī)頁面的按鈕,這里我們新增一個(gè)流控規(guī)則。
登錄 Nacos 后臺(tái),配置管理->配置列表:
點(diǎn)擊進(jìn)入配置詳情,配置內(nèi)容如下:
[{ "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 }]
小結(jié)
生產(chǎn)環(huán)境下,推送規(guī)則正確做法應(yīng)該是 配置中心控制臺(tái)/Sentinel 控制臺(tái) → 配置中心 → Sentinel 數(shù)據(jù)源 → Sentinel。
案例
https://gitee.com/52itstyle/spring-boot-blog
<https://gitee.com/52itstyle/spring-boot-blog>
參考
https://github.com/alibaba/Sentinel
https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard
熱門工具 換一換