通用搭建网站教程,外贸公司做网站该去哪里找,手机页面模板,阿里云虚拟主机怎么做网站转载自 Spring Cloud Alibaba基础教程#xff1a;Sentinel Dashboard中修改规则同步到Nacos
上一篇我们介绍了如何通过改造Sentinel Dashboard来实现修改规则之后自动同步到Apollo。下面通过这篇#xff0c;详细介绍当使用Nacos作为配置中心之后#xff0c;如何实现Sentin…转载自 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的依赖将scopetest/scope注释掉这样才能在主程序中使用。
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactId!--scopetest/scope--
/dependency第二步找到resources/app/scripts/directives/sidebar/sidebar.html中的这段代码
li ui-sref-activeactivea ui-srefdashboard.flowV1({app: entry.app})i classglyphicon glyphicon-filter/inbsp;nbsp;流控规则/a
/li修改为
li ui-sref-activeactivea ui-srefdashboard.flow({app: entry.app})i classglyphicon glyphicon-filter/inbsp;nbsp;流控规则/a
/li第三步在com.alibaba.csp.sentinel.dashboard.rule包下新建一个nacos包用来编写针对Nacos的扩展实现。
第四步创建Nacos的配置类具体代码如下
Configuration
public class NacosConfig {Beanpublic ConverterListFlowRuleEntity, String flowRuleEntityEncoder() {return JSON::toJSONString;}Beanpublic ConverterString, ListFlowRuleEntity flowRuleEntityDecoder() {return s - JSON.parseArray(s, FlowRuleEntity.class);}Beanpublic 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 DynamicRuleProviderListFlowRuleEntity {Autowiredprivate ConfigService configService;Autowiredprivate ConverterString, ListFlowRuleEntity converter;public static final String FLOW_DATA_ID_POSTFIX -sentinel;public static final String GROUP_ID DEFAULT_GROUP;Overridepublic ListFlowRuleEntity 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.groupIdDEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.dataId${spring.application.name}-sentinel注意两边的DataId和GroupId必须对应上。
第六步实现Nacos的配置推送。
Component(flowRuleNacosPublisher)
public class FlowRuleNacosPublisher implements DynamicRulePublisherListFlowRuleEntity {Autowiredprivate ConfigService configService;Autowiredprivate ConverterListFlowRuleEntity, String converter;public static final String FLOW_DATA_ID_POSTFIX -sentinel;public static final String GROUP_ID DEFAULT_GROUP;Overridepublic void publish(String app, ListFlowRuleEntity 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的实现
Autowired
Qualifier(flowRuleNacosProvider)
private DynamicRuleProviderListFlowRuleEntity ruleProvider;
Autowired
Qualifier(flowRuleNacosPublisher)
private DynamicRulePublisherListFlowRuleEntity rulePublisher;最后读者可以使用本文改造后的sentinel-dashboard联合之前《Sentinel使用Nacos存储规则》一文的例子来验证本文内容。
代码示例
本文介绍内容的客户端代码示例读者可以通过查看下面仓库中的alibaba-sentinel-dashboard-nacos项目
Githubhttps://github.com/dyc87112/SpringCloud-Learning/Giteehttps://gitee.com/didispace/SpringCloud-Learning/