上一篇咱们介绍了如何经过改造Sentinel Dashboard来实现修改规则以后自动同步到Apollo。下面经过这篇,详细介绍当使用Nacos做为配置中心以后,如何实现Sentinel Dashboard中修改规则同步到Nacos。关于下面改造的原理和分析能够见上一篇《Sentinel Dashboard中修改规则同步到Apollo》的头两节内容,这里不重复介绍了。html
下面直接来看看如何实现的具体改造步骤,这里参考了Sentinel Dashboard
源码中关于Nacos实现的测试用例。可是因为考虑到与Spring Cloud Alibaba的结合使用,略做修改。java
第一步:修改pom.xml
中的sentinel-datasource-nacos的依赖,将<scope>test</scope>
注释掉,这样才能在主程序中使用。git
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <!--<scope>test</scope>--> </dependency>
第二步:找到resources/app/scripts/directives/sidebar/sidebar.html
中的这段代码:github
<li ui-sref-active="active"> <a ui-sref="dashboard.flowV1({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i> 流控规则 </a> </li>
修改成:spring
<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的扩展实现。app
第四步:建立Nacos的配置类,具体代码以下:ide
@Configuration public 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的配置拉取。ui
@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_GROUP spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel
注意:两边的DataId和GroupId必须对应上。spa
第六步:实现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)); } }
第七步:修改com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2
中DynamicRuleProvider
和DynamicRulePublisher
注入的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
项目:
若是您对这些感兴趣,欢迎star、follow、收藏、转发给予支持!