

Spring Cloud Alibaba基础教程:Sentinel Dashboard中修改规则同步到Nacos
source link: https://www.tuicool.com/articles/E7fA7fY
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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的依赖,将 <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> 流控规则 </a> </li>
修改为:
<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的扩展实现。
第四步:创建Nacos的配置类,具体代码如下:
@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的配置拉取。
@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必须对应上。
第六步:实现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.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
项目:
- Github: https://github.com/dyc87112/SpringCloud-Learning/
- Gitee: https://gitee.com/didispace/SpringCloud-Learning/
如果您对这些感兴趣,欢迎star、follow、收藏、转发给予支持!
Recommend
-
47
最近管点闲事浪费了不少时间,感谢网友 libinwalan 的留言提醒。及时纠正路线,继续跟大家一起学习Spring Cloud Alibaba。 Nacos作为注册中心和配置中心的基础教程,到这里先告一段落,后续与其他结合的内容等讲...
-
42
通过上一篇 《使用Sentinel实现接口限流》 的介绍,相信大家对Sentinel已经有了初步的认识。在Spring Cloud Alibaba的整合封装之下,接口限流这件事情可以非...
-
24
-
47
-
28
-
38
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。 Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。 Sentinel 具有以下特征: 丰富的应用场景: Sentinel...
-
51
Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常方便的,下面来介绍下如何对 Feign 的支持以及实现原理。 集成 Feign 使用 s...
-
23
在之前的练习中,只要应用重启,就需要重新配置,这样在我们实际的项目是非常不实用的,那么有没有办法把我们配置的规则保存下来呢?答案是YES,那么接下来,给大家来介绍如何将Sentinel规则持久化。Document:传送门FileDatasource(文件存储)Pull模式Push模式Nacos...
-
43
这一节我们通过一个简单的实例,学习Sentinel的基本应用。 一、Sentinel 限流核心概念 在学习Sentinel的具体应用之前,我们先来了解一下Sentinel中两个核心的概念,资源和规则。
-
39
前文中我们提到 Netflix 中多项开源产品已进入维护阶段,不再开发新的版本,就目前来看是没有什么问题的。但是从长远角度出发,...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK