如何使用Sentinel Dashboard动态推把数据同步到Nacos
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章主要讲解了"如何使用Sentinel Dashboard动态推把数据同步到Nacos",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"如何使用Se
千家信息网最后更新 2026年10月31日如何使用Sentinel Dashboard动态推把数据同步到Nacos
这篇文章主要讲解了"如何使用Sentinel Dashboard动态推把数据同步到Nacos",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"如何使用Sentinel Dashboard动态推把数据同步到Nacos"吧!
准备工作:
下载Sentinel Dashboard的release版本。地址:https://github.com/alibaba/Sentinel/releases
一 :修改pom.xml中的sentinel-datasource-nacos的依赖,将注释掉,这样才能在主程序中使用。
|
二:找到resources/app/scripts/directives/sidebar/sidebar.html中的这段代码:
修改为:
这样修改之后就会跳转到FlowControllerV2的接口。
三:再项目com.alibaba.csp.sentinel.dashboard中新建一个nacos包来实现扩展功能。
@Component@ConfigurationProperties(prefix = "nacos.server")public class NacosConfigProperties { private String ip; private String port; private String namespace; private String groupId; public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public String getNamespace() { return namespace; } public void setNamespace(String namespace) { this.namespace = namespace; } public String getGroupId() { return groupId; } public void setGroupId(String groupId) { this.groupId = groupId; } public String getServerAddr() { return this.getIp()+":"+this.getPort(); } @Override public String toString() { return "NacosConfigProperties [ip=" + ip + ", port=" + port + ", namespace=" + namespace + ", groupId=" + groupId + "]"; }}public final class NacosConfigConstant { public static final String FLOW_DATA_ID_POSTFIX = "-sentinel-flow"; public static final String GROUP_ID = "DEFAULT_GROUP";}@Configurationpublic class NacosConfig { @Autowired private NacosConfigProperties nacosConfigProperties; /** * 非常关键 这里将FlowRuleEntity转换成FlowRule才会对客户端生效 * @return FlowRule */ @Bean public Converter, String> flowRuleEntityEncoder() { return rules -> JSON.toJSONString(rules.stream().map(FlowRuleEntity::toRule).collect(Collectors.toList()), true); } @Bean public Converter> flowRuleEntityDecoder() { return s -> JSON.parseArray(s, FlowRuleEntity.class); } @Bean public ConfigService nacosConfigService() throws Exception { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfigProperties.getServerAddr()); properties.put(PropertyKeyConst.NAMESPACE, nacosConfigProperties.getNamespace()); return ConfigFactory.createConfigService(properties); }}
四:编写动态推拉模式的扩展代码
@Component("flowRuleNacosProvider")public class FlowRuleNacosProvider implements DynamicRuleProvider> { private static Logger logger = LoggerFactory.getLogger(FlowRuleNacosProvider.class); @Autowired private NacosConfigProperties nacosConfigProperties; @Autowired private ConfigService configService; @Autowired private Converter> converter; @Override public List getRules(String appName) throws Exception { String rules = configService.getConfig(appName + NacosConfigConstant.FLOW_DATA_ID_POSTFIX, nacosConfigProperties.getGroupId(), 3000); logger.info("从Nacos中拉取到限流规则信息:{}",rules); if (StringUtil.isEmpty(rules)) { return new ArrayList<>(); } return converter.convert(rules); }}
@Component("flowRuleNacosPublisher")public class FlowRuleNacosPublisher implements DynamicRulePublisher> { @Autowired private NacosConfigProperties nacosConfigProperties; @Autowired private ConfigService configService; @Autowired private Converter, String> converter; @Override public void publish(String app, List rules) throws Exception { AssertUtil.notEmpty(app, "app name cannot be empty"); if (rules == null) { return; } configService.publishConfig(app + NacosConfigConstant.FLOW_DATA_ID_POSTFIX, nacosConfigProperties.getGroupId(), converter.convert(rules)); }}
五:然后将FlowControllerV2中的默认DynamicRuleProvider和DynamicRulePublisher修改为:
@Autowired@Qualifier("flowRuleNacosProvider")private DynamicRuleProvider> ruleProvider;@Autowired@Qualifier("flowRuleNacosPublisher")private DynamicRulePublisher> rulePublisher;
private void publishRules(/*@NonNull*/ String app) throws Exception { List rules = repository.findAllByApp(app); rulePublisher.publish(app, rules); logger.info("添加限流规则成功{}", JSON.toJSONString(rules.stream().map(FlowRuleEntity::toRule).collect(Collectors.toList()), true));} 六:application.properties配置文件
#nacosnacos.server.ip=localhostnacos.server.port=8848nacos.server.namespace=nacos.server.group-id=DEFAULT_GROUP
感谢各位的阅读,以上就是"如何使用Sentinel Dashboard动态推把数据同步到Nacos"的内容了,经过本文的学习后,相信大家对如何使用Sentinel Dashboard动态推把数据同步到Nacos这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
动态
数据
同步
规则
学习
代码
内容
成功
主程序
信息
关键
功能
地址
客户
客户端
就是
思路
情况
接口
文件
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网与科技
亿人帮互联网科技有限公司
互联网是第几次科技革命的成果
万易互联网科技
唐信互联网科技
睿民互联网科技
互联网科技论坛
互联网科技公司做什么
中正互联网科技招聘
北京大学互联网科技怎么样
互联网教育科技公司vipkid
南山科技园互联网公司有哪些
华夏互联网科技业
浙南科技城互联网大厦建设工程
用友工业互联网科技发展
互联网科技如何创业
沐思想互联网科技有限公司
互联网与数字科技关系
平安好医生打造互联网科技医疗
互联网企业和网络科技有限公司
互联网科技名片
科技互联网技术
互联网金融公司科技
紫鲸互联网科技
云电互联网科技
伴你行互联网科技有限公司
中国最先进互联网科技公司
互联网科技和信息
科技互联网海报
华人运通互联网科技公司