上一篇我们介绍了如何通过改造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>&nbsp;&nbsp;流控规则    </a></li>

修改为:

<li ui-sref-active="active">    <a ui-sref="dashboard.flow({app: entry.app})">        <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则    </a></li>

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

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

@Configurationpublic class NacosConfig {

    @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(PropertyKeyConst.SERVER_ADDR, "localhost");        return ConfigFactory.createConfigService(properties);    }}

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

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

@Component("flowRuleNacosProvider")public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {

    @Autowired    private ConfigService configService;    @Autowired    private Converter<String, List<FlowRuleEntity>> converter;

    public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";    public static final String GROUP_ID = "DEFAULT_GROUP";

    @Override    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_GROUPspring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel

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

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

@Component("flowRuleNacosPublisher")public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {

    @Autowired    private ConfigService configService;    @Autowired    private Converter<List<FlowRuleEntity>, String> converter;

    public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";    public static final String GROUP_ID = "DEFAULT_GROUP";

    @Override    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.FlowControllerV2DynamicRuleProviderDynamicRulePublisher注入的Bean,改为上面我们编写的针对Apollo的实现:

@Autowired@Qualifier("flowRuleNacosProvider")private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;@Autowired@Qualifier("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相关推荐

  1. Sentinel Dashboard 中修改规则同步到 Nacos

    点击蓝色"程序猿DD"关注我哟 加个"星标",不忘签到哦 上一篇我们介绍了如何通过改造Sentinel Dashboard来实现修改规则之后自动同步到Apoll ...

  2. Sentinel(二十六)之Sentinel Dashboard中修改规则同步到Nacos

    转载自  Spring Cloud Alibaba基础教程:Sentinel Dashboard中修改规则同步到Nacos 上一篇我们介绍了如何通过改造Sentinel Dashboard来实现修改规 ...

  3. Spring Cloud Alibaba基础教程:Sentinel Dashboard中修改规则同步到Apollo

    在之前的两篇教程中我们分别介绍了如何将Sentinel的限流规则存储到Nacos和Apollo中.同时,在文末的思考中,我都指出了这两套整合方案都存在一个不足之处:不论采用什么配置中心,限流规则都只能 ...

  4. Sentinel(二十四)之Sentinel Dashboard中修改规则同步到ZooKeeper

    转载自  Springboot使用Sentinel限流,集成zookeeper完成规则的持久化 上一篇简单介绍了sentinel限流的基本配置和使用,这一篇我们来稍微深入一点,看看如何将zookeep ...

  5. Spring Cloud Alibaba基础教程:Nacos的集群部署

    点击蓝色"程序猿DD"关注我哟 <Spring Cloud Alibaba基础教程>连载中,关注我一起学习!前情回顾: <使用Nacos实现服务注册与发现> ...

  6. Spring Cloud Alibaba基础教程:Nacos的数据持久化

    <Spring Cloud Alibaba基础教程>连载中,关注我一起学习!前情回顾: <使用Nacos实现服务注册与发现> <支持的几种服务消费方式> <使 ...

  7. Spring Cloud Alibaba基础教程:Nacos配置的多文件加载与共享配置

    <Spring Cloud Alibaba基础教程>连载中,关注我一起学期!前情回顾: <使用Nacos实现服务注册与发现> <支持的几种服务消费方式> <使 ...

  8. Spring Cloud Alibaba基础教程:Nacos配置的多环境管理

    <Spring Cloud Alibaba基础教程>连载中,关注我一起学期!前情回顾: <使用Nacos实现服务注册与发现> <支持的几种服务消费方式> <使 ...

  9. Spring Cloud Alibaba基础教程:Nacos配置的加载规则详解

    <Spring Cloud Alibaba基础教程>连载中,关注我一起学期!前情回顾: <使用Nacos实现服务注册与发现> <支持的几种服务消费方式> <使 ...

最新文章

  1. crontab 最小间隔_今天我间隔了:如何找到不在数组中的最小数字
  2. 剑指Offer #12 数值的整数次方(快速幂)
  3. idea 中javax.servlet.http.HttpServlet包导不进来
  4. golang grpc demo
  5. java葵花宝典_JAVA程序员想入职跳槽,这些基本功一定要做好,你给自己打几分?...
  6. 31.CSS3变形效果【下】
  7. .NET之模型绑定和验证
  8. 以前看过一个压缩过的.exe,运行会播放长达半小时的动画,却只有60KB,个人认为其中的原理...
  9. python中的pygame模块使用方法_Pygame的基本使用
  10. 十八、数据容器、数据访问宽度、端口(计算机对数据处理方式:读取、写入、运算;数据可存放三个地方:CPU内部、内存、端口)
  11. VPP命令行:启动配置,HTTP服务,DPDK配置
  12. 汽车维保反欺诈系统的设计和算法应用
  13. 前端协商缓存强缓存如何使用_强制缓存(200)和协商缓存(304)
  14. php支付宝发卡源码,个人发卡系统支付宝即时到帐大气源码
  15. Java跨年祝福语代码_[商业跨年祝福语]跨年祝福语贺词大全
  16. 解放生产力!20 个必知必会 VSCode 小技巧
  17. ps中扩展画布的时候,不能选择扩展画布部分的颜色解决方法
  18. progress中的数据库访问
  19. 储罐液位开关c语言编程,危化品企业罐区液位计和紧急切断阀的设置及联锁要求规范合集(1)...
  20. 微信撤回的消息找不到?你OUT了,看看python程序怎么找回!

热门文章

  1. MYSQL性能优化(转)
  2. static关键字 void和void指针 函数指针
  3. typename的作用
  4. 去广州见了我大学老师标哥
  5. java时间戳版本号_maven 自动编译版本号 buildnumber-maven-plugin 1.4
  6. python图像下采样_[Python图像处理]十二.图像向下取样和向上取样
  7. AttributeError: ‘set‘ object has no attribute ‘items‘
  8. RuntimeError: Can‘t call numpy() on Variable that requires grad. Use var.detach().numpy()
  9. 下载Pytorch的自带数据集时报错=urllib.error.URLError: urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]
  10. Mongoose aggregate 多表关联查询