Sentinel持久化实现push模式

push模式实现

alibaba没有提供sentinel持久化最佳方案的实现(要付费..),所以需要手动实现,包括两部分:Sentinel Dashboard向远程配置中心推送规则;Sentinel客户端监听配置中心规则变化。

资料来源于黑马

步骤:

一、修改order-service服务

1.引入依赖

在order-service引入sentinel监听nacos的依赖

 <!--实现持久化,引入sentinel监听nacos的依赖-->
 <dependency>
     <groupId>com.alibaba.csp</groupId>
     <artifactId>sentinel-datasource-nacos</artifactId>
 </dependency>

2.配置nacos地址以及要监听的地址信息

 spring:
  cloud:
    sentinel:
      datasource:
        flow: #配置flow限流规则
          nacos:
            server-addr: localhost:8848
            data-id: order-service-flow-rules
            group-id: SENTINEL_GROUP
            rule-type: flow # degrade/authority/param-flow

二、修改sentinel-dashboard源码

1.解压

下载并解压sentinel源码,github地址

image-20230720141801601

然后并用IDEA打开这个项目,结构如下:

image-20210618201412878

2.修改nacos依赖

在sentinel-dashboard源码的pom文件中,nacos的依赖默认的scope是test,只能在测试时使用,这里要去除:

image-20210618201607831

将sentinel-datasource-nacos依赖的scope去掉:

 <dependency>
     <groupId>com.alibaba.csp</groupId>
     <artifactId>sentinel-datasource-nacos</artifactId>
 </dependency>

3.添加nacos支持

在sentinel-dashboard的test包下,已经编写了对nacos的支持,我们需要将其拷贝到main下。

image-20210618201726280

4.修改nacos地址

然后,还需要修改测试代码中的NacosConfig类:

image-20210618201912078

修改其中的nacos地址,让其读取application.properties中的配置:

image-20210618202047575

在sentinel-dashboard的application.properties中添加nacos地址配置:

 nacos.addr=localhost:8848

5.配置nacos数据源

另外,还需要修改com.alibaba.csp.sentinel.dashboard.controller.v2包下的FlowControllerV2类:

image-20210618202322301

让我们添加的Nacos数据源生效:

image-20210618202334536

6.修改前端页面

接下来,还要修改前端页面,添加一个支持nacos的菜单。

修改src/main/webapp/resources/app/scripts/directives/sidebar/目录下的sidebar.html文件:

image-20210618202433356

将其中的这部分注释打开:

image-20210618202449881

修改其中的文本:

image-20210618202501928

7.重新编译、打包项目

运行IDEA中的maven插件,编译和打包修改好的Sentinel-Dashboard:

image-20210618202701492

8.启动

启动方式跟官方一样:

 java -jar sentinel-dashboard.jar

如果要修改nacos地址,需要添加参数:

 java -jar -Dnacos.addr=localhost:8848 sentinel-dashboard.jar

One thought on “Sentinel持久化实现push模式

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注