javascript
Spring Cloud Alibaba基础教程:Sentinel Dashboard中修改规则同步到Nacos
上一篇我們介紹了如何通過改造Sentinel Dashboard來實現修改規則之后自動同步到Apollo。下面通過這篇,詳細介紹當使用Nacos作為配置中心之后,如何實現Sentinel Dashboard中修改規則同步到Nacos。關于下面改造的原理和分析可以見上一篇《Sentinel Dashboard中修改規則同步到Apollo》的頭兩節內容,這里不重復介紹了。
代碼實現
下面直接來看看如何實現的具體改造步驟,這里參考了Sentinel Dashboard源碼中關于Nacos實現的測試用例。但是由于考慮到與Spring Cloud Alibaba的結合使用,略作修改。
第一步:修改pom.xml中的sentinel-datasource-nacos的依賴,將<scope>test</scope>注釋掉,這樣才能在主程序中使用。
| <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <!--<scope>test</scope>--> </dependency> |
第二步:找到resources/app/scripts/directives/sidebar/sidebar.html中的這段代碼:
| <li ui-sref-active="active"> <a ui-sref="dashboard.flowV1({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i> 流控規則 </a> </li> |
修改為:
| <li ui-sref-active="active"> <a ui-sref="dashboard.flow({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i> 流控規則 </a> </li> |
第三步:在com.alibaba.csp.sentinel.dashboard.rule包下新建一個nacos包,用來編寫針對Nacos的擴展實現。
第四步:創建Nacos的配置類,具體代碼如下:
public class NacosConfig { public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() { return JSON::toJSONString; } public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() { return s -> JSON.parseArray(s, FlowRuleEntity.class); } public ConfigService nacosConfigService() throws Exception { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "localhost"); return ConfigFactory.createConfigService(properties); } } |
如果用到了namespace隔離環境,可以在nacosConfigService方法中再加入配置,比如:properties.put(PropertyKeyConst.NAMESPACE, "130e71fa-97fe-467d-ad77-967456f2c16d");
第五步:實現Nacos的配置拉取。
| ("flowRuleNacosProvider") public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> { private ConfigService configService; private Converter<String, List<FlowRuleEntity>> converter; public static final String FLOW_DATA_ID_POSTFIX = "-sentinel"; public static final String GROUP_ID = "DEFAULT_GROUP"; public List<FlowRuleEntity> getRules(String appName) throws Exception { String rules = configService.getConfig(appName + FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000); if (StringUtil.isEmpty(rules)) { return new ArrayList<>(); } return converter.convert(rules); } } |
- getRules方法中的appName參數是Sentinel中的服務名稱。
- configService.getConfig方法是從Nacos中獲取配置信息的具體操作。其中,DataId和GroupId分別對應客戶端使用時候的對應配置。比如這里的例子對應了之前我們在《Sentinel使用Nacos存儲規則》一文中的配置,具體如下:
| spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel |
注意:兩邊的DataId和GroupId必須對應上。
第六步:實現Nacos的配置推送。
| ("flowRuleNacosPublisher") public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> { private ConfigService configService; private Converter<List<FlowRuleEntity>, String> converter; public static final String FLOW_DATA_ID_POSTFIX = "-sentinel"; public static final String GROUP_ID = "DEFAULT_GROUP"; public void publish(String app, List<FlowRuleEntity> rules) throws Exception { AssertUtil.notEmpty(app, "app name cannot be empty"); if (rules == null) { return; } configService.publishConfig(app + FLOW_DATA_ID_POSTFIX, GROUP_ID, converter.convert(rules)); } } |
- 這里的大部分內容與上一步中的實現一致。主要就是Nacos中存儲配置的DataId和GroupId不要弄錯。
第七步:修改com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2中DynamicRuleProvider和DynamicRulePublisher注入的Bean,改為上面我們編寫的針對Apollo的實現:
("flowRuleNacosProvider") private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; ("flowRuleNacosPublisher") private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher; |
最后,讀者可以使用本文改造后的sentinel-dashboard聯合之前《Sentinel使用Nacos存儲規則》一文的例子來驗證本文內容。
代碼示例
本文介紹內容的客戶端代碼,示例讀者可以通過查看下面倉庫中的alibaba-sentinel-dashboard-nacos項目:
- Github:https://github.com/dyc87112/SpringCloud-Learning/
- Gitee:https://gitee.com/didispace/SpringCloud-Learning/
如果您對這些感興趣,歡迎star、follow、收藏、轉發給予支持!
系列回顧
- 《Spring Cloud Alibaba基礎教程:使用Nacos實現服務注冊與發現》
- 《Spring Cloud Alibaba基礎教程:支持的幾種服務消費方式》
- 《Spring Cloud Alibaba基礎教程:使用Nacos作為配置中心》
- 《Spring Cloud Alibaba基礎教程:Nacos配置的加載規則詳解》
- 《Spring Cloud Alibaba基礎教程:Nacos配置的多環境管理》
- 《Spring Cloud Alibaba基礎教程:Nacos配置的多文件加載與共享配置》
- 《Spring Cloud Alibaba基礎教程:Nacos的數據持久化》
- 《Spring Cloud Alibaba基礎教程:Nacos的集群部署》
- 《Spring Cloud Alibaba基礎教程:使用Sentinel實現接口限流》
- 《Spring Cloud Alibaba基礎教程:Sentinel使用Nacos存儲規則》
- 《Spring Cloud Alibaba基礎教程:Sentinel使用Apollo存儲規則》
- 《Spring Cloud Alibaba基礎教程:Sentinel Dashboard中修改規則同步到Apollo》
專題推薦
- Spring Boot基礎教程
- Spring Cloud基礎教程
總結
以上是生活随笔為你收集整理的Spring Cloud Alibaba基础教程:Sentinel Dashboard中修改规则同步到Nacos的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一般化机器学习与神经网络
- 下一篇: 直通BAT必考题系列:7种JVM垃圾收集