完成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的限流也实时生效了 。

相关推荐
9523618 小时前
Sentinel
java·后端·spring·sentinel·springcloud
渣渣盟2 天前
当 Redis 集群发生主从切换(Failover)时,Flink 任务会崩溃吗?如何利用 Sentinel 实现高可用?
redis·flink·sentinel
xiaoxiangsiyan8 天前
LNMP + Redis Sentinel 高可用架构部署手册(续)
运维·网络·数据库·redis·缓存·架构·sentinel
敲代码的嘎仔8 天前
从零搭建微服务:Spring Cloud Alibaba 全家桶踩坑实录(Nacos + OpenFeign + Gateway + Sentinel)
java·spring boot·spring·spring cloud·微服务·gateway·sentinel
小罗水10 天前
第14章 Sentinel 限流与 AI 降级
人工智能·sentinel
he___H11 天前
Sentinel使用实操
java·开发语言·sentinel
随风M记忆s12 天前
GEE&Python-demo:利用Sentinel-监测北京奥林匹克森林公园年NDVI变化(附Python版)
开发语言·python·sentinel
X-⃢_⃢-X12 天前
六、Sentinel
网络·数据库·sentinel
YOU OU14 天前
Redis哨兵 & 集群
数据库·redis·sentinel
遥感知识服务25 天前
Sentinel-1 + DEM + FwDET + 随机森林:从快速水深初估到多因子误差修正
算法·随机森林·sentinel