Sentinel Dashboard 中修改规则同步到 Nacos

点击蓝色“程序猿DD”关注我哟

加个“星标”,不忘签到哦


640?wx_fmt=jpeg




上一篇我们介绍了如何通过改造Sentinel Dashboard来实现修改规则之后自动同步到Apollo。下面通过这篇,详细介绍当使用Nacos作为配置中心之后,如何实现Sentinel Dashboard中修改规则同步到Nacos。关于下面改造的原理和分析可以见上一篇《Sentinel Dashboard中修改规则同步到Apollo》的头两节内容,本篇就不重复介绍了。

代码实现

下面直接来看看如何实现的具体改造步骤,这里参考了 SentinelDashboard源码中关于Nacos实现的测试用例。但是由于考虑到与Spring Cloud Alibaba的结合使用,略作修改。

第一步:修改 pom.xml中的sentinel-datasource-nacos的依赖,将 <scope>test</scope>注释掉,这样才能在主程序中使用。

 
 
  1. <dependency>

  2. <groupId>com.alibaba.csp</groupId>

  3. <artifactId>sentinel-datasource-nacos</artifactId>

  4. <!--<scope>test</scope>-->

  5. </dependency>

第二步:找到 resources/app/scripts/directives/sidebar/sidebar.html中的这段代码:

 
 
  1. <li ui-sref-active="active">

  2. <a ui-sref="dashboard.flowV1({app: entry.app})">

  3. <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则

  4. </a>

  5. </li>

修改为:

 
 
  1. <li ui-sref-active="active">

  2. <a ui-sref="dashboard.flow({app: entry.app})">

  3. <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则

  4. </a>

  5. </li>

第三步:在 com.alibaba.csp.sentinel.dashboard.rule包下新建一个nacos包,用来编写针对Nacos的扩展实现。

第四步:创建Nacos的配置类,具体代码如下:

 
 
  1. @Configuration

  2. public class NacosConfig {


  3. @Bean

  4. public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {

  5. return JSON::toJSONString;

  6. }


  7. @Bean

  8. public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {

  9. return s -> JSON.parseArray(s, FlowRuleEntity.class);

  10. }


  11. @Bean

  12. public ConfigService nacosConfigService() throws Exception {

  13. Properties properties = new Properties();

  14. properties.put(PropertyKeyConst.SERVER_ADDR, "localhost");

  15. return ConfigFactory.createConfigService(properties);

  16. }

  17. }

如果用到了namespace隔离环境,可以在 nacosConfigService方法中再加入配置,比如: properties.put(PropertyKeyConst.NAMESPACE,"130e71fa-97fe-467d-ad77-967456f2c16d");

第五步:实现Nacos的配置拉取。

 
 
  1. @Component("flowRuleNacosProvider")

  2. public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {


  3. @Autowired

  4. private ConfigService configService;

  5. @Autowired

  6. private Converter<String, List<FlowRuleEntity>> converter;


  7. public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";

  8. public static final String GROUP_ID = "DEFAULT_GROUP";


  9. @Override

  10. public List<FlowRuleEntity> getRules(String appName) throws Exception {

  11. String rules = configService.getConfig(appName + FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000);

  12. if (StringUtil.isEmpty(rules)) {

  13. return new ArrayList<>();

  14. }

  15. return converter.convert(rules);

  16. }

  17. }

  • getRules方法中的 appName参数是Sentinel中的服务名称。

  • configService.getConfig方法是从Nacos中获取配置信息的具体操作。其中,DataId和GroupId分别对应客户端使用时候的对应配置。比如这里的例子对应了之前我们在《Sentinel使用Nacos存储规则》一文中的配置,具体如下:

 
 
  1. spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP

  2. spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel

注意:两边的DataId和GroupId必须对应上。

第六步:实现Nacos的配置推送。

 
 
  1. @Component("flowRuleNacosPublisher")

  2. public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {


  3. @Autowired

  4. private ConfigService configService;

  5. @Autowired

  6. private Converter<List<FlowRuleEntity>, String> converter;


  7. public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";

  8. public static final String GROUP_ID = "DEFAULT_GROUP";


  9. @Override

  10. public void publish(String app, List<FlowRuleEntity> rules) throws Exception {

  11. AssertUtil.notEmpty(app, "app name cannot be empty");

  12. if (rules == null) {

  13. return;

  14. }

  15. configService.publishConfig(app + FLOW_DATA_ID_POSTFIX, GROUP_ID, converter.convert(rules));

  16. }

  17. }

  • 这里的大部分内容与上一步中的实现一致。主要就是Nacos中存储配置的DataId和GroupId不要弄错。

第七步:修改 com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2DynamicRuleProviderDynamicRulePublisher注入的Bean,改为上面我们编写的针对Apollo的实现:

 
 
  1. @Autowired

  2. @Qualifier("flowRuleNacosProvider")

  3. private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;

  4. @Autowired

  5. @Qualifier("flowRuleNacosPublisher")

  6. 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、收藏、转发给予支持!

系列回顾


推荐阅读


号外:最近整理了之前编写的一系列内容做成了PDF,关注我并回复相应口令获取:

001 :领取《Spring Boot基础教程》

002 :领取《Spring Cloud基础教程》



活动介绍自律到极致-人生才精致:第6期

活动奖励:《Service Mesh实战》* 10

扫描下面二维码签到参与

640?wx_fmt=png

关注我,加个星标,不忘签到哦~


2019

与大家聊聊技术人的斜杠生活

640?wx_fmt=png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值