完成Sentinel-Dashboard控制台数据的持久化-同步到Nacos

本次案例采用的是Sentinel1.8.8版本

一、Sentinel源码环境搭建

1、下载Sentinel源码工程

bash 复制代码
git clone https://github.com/alibaba/Sentinel.git

2、导入到idea

这里可以先运行DashboardApplication.java试一下是否运行成功,若成功,源码环境搭建完毕,若失败,则请看以下解决方案。

3、Java版本Bug解决(若无此问题可跳过)

先把项目的java版本给指定,调成你对应的版本,最好是1.8

对应的moudle也修改成java 8

接下来再次运行 DashboardApplication.java试一下是否运行成功,若报以下错:

说明对应sentinel-core这个模块的java版本也需要改

再次运行,可能还有其他模块的报错,根据报错信息,只要java 13版本出错的,都可以找到报错信息中的moudle,然后进行版本修改。

如果出现java 17版本的错误,那么还需要改一个地方。

这里也是,哪个模块报错就修改哪个模块

至此,Sentinel-Dashboard应该能正常启动,源码环境搭建完毕。

二、源码改造

后端代码改造

首先将下面这行代码注释,因为要将配置推送到nacos,所以才将以下作用域注释掉

接下来将test包下的相关代码复制到src相关的包下

修改NacosConfig

java 复制代码
    @Bean
    public ConfigService nacosConfigService() throws Exception {
        Properties properties = new Properties();
        properties.put("serverAddr","127.0.0.1:8848");
        properties.put("namespace","f1ce1184-d664-46a0-947d-a6cfc0aea7a8");
        return ConfigFactory.createConfigService(properties);
    }

修改以下代码,换成Nacos的provider和publisher

前端代码改造

修改前端代码:

html 复制代码
          <li ui-sref-active="active" ng-if="!entry.isGateway">
            <a ui-sref="dashboard.flow({app: entry.app})">
              <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则-V2</a>
          </li>

添加运行参数:

bash 复制代码
-Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -Dcsp.sentinel.api.port=8720

最后的-Dcsp.sentinel.api.port=8720 这里的8720不要和你的SpringBoot 中transport端口一样,会造成冲突

接着运行 DashboardApplication,能正常运行代表改造成功。

本地SpringBoot应用改造

打开sentinel的网页进行流量控制的编辑,就可以实现自动推送到SpringBoot应用以及 Nacos中了

可以发现自动写入nacos了,而且SpringBoot的限流也实时生效了 。

相关推荐
接着奏乐接着舞1 天前
springcloud Sentinel
spring·spring cloud·sentinel
超梦dasgg2 天前
Sentinel生产环境实战全解
java·微服务·sentinel
Ting-yu2 天前
SpringCloud快速入门(11)---- Sentinel(异常处理)
java·spring boot·后端·spring·spring cloud·sentinel
Ting-yu2 天前
SpringCloud快速入门(10)---- Sentinel(应用场景&控制台安装)
spring·spring cloud·sentinel
heimeiyingwang4 天前
【架构实战】服务熔断与限流Sentinel:高可用服务的守护神
架构·sentinel
庞轩px5 天前
第八篇:Spring与微服务——从SpringBoot到SpringCloud的演进
spring boot·spring·微服务·nacos·gateway·sentinel
卷毛的技术笔记8 天前
双十一零点扛过10倍流量洪峰:Sentinel与Redis+Lua的分布式限流深度避坑指南
java·redis·分布式·后端·系统架构·sentinel·lua
chushiyunen9 天前
sentinel限流工具使用
sentinel
Irissgwe9 天前
redis之哨兵(Sentinel)
数据库·redis·sentinel·主从复制·哨兵
phltxy11 天前
微服务高可用实战:Sentinel 熔断与限流从入门到精通
微服务·架构·sentinel