前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Sentinel Dashboard 中修改规则同步到 Nacos

Sentinel Dashboard 中修改规则同步到 Nacos

作者头像
程序猿DD
发布2019-05-24 20:40:01
1.3K0
发布2019-05-24 20:40:01
举报
文章被收录于专栏:程序猿DD程序猿DD

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

代码实现

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

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

代码语言:javascript
复制
<dependency>    <groupId>com.alibaba.csp</groupId>    <artifactId>sentinel-datasource-nacos</artifactId>    <!--<scope>test</scope>--></dependency>

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

代码语言:javascript
复制
<li ui-sref-active="active">    <a ui-sref="dashboard.flowV1({app: entry.app})">        <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则    </a></li>

修改为:

代码语言:javascript
复制
<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的配置类,具体代码如下:

代码语言:javascript
复制
@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的配置拉取。

代码语言:javascript
复制
@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存储规则》一文中的配置,具体如下:
代码语言:javascript
复制
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUPspring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel

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

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

代码语言:javascript
复制
@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的实现:

代码语言:javascript
复制
@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/
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序猿DD 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码实现
  • 代码示例
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档