Sentinel 规则持久化

Sentinel 规则持久化

一、修改order-service服务

修改OrderService,让其监听Nacos中的sentinel规则配置。

具体步骤如下:

1.引入依赖

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

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

2.配置nacos地址

在order-service中的application.yml文件配置nacos地址及监听的配置信息:

spring:
  cloud:
    sentinel:
      datasource:
        flow:
          nacos:
            server-addr: localhost:8848 # nacos地址
            dataId: orderservice-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: flow # 还可以是:degrade、authority、param-flow

二、修改sentinel-dashboard源码

SentinelDashboard默认不支持nacos的持久化,需要修改源码。

1. 解压

解压sentinel源码包:

Sentinel 规则持久化

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

Sentinel 规则持久化

2. 修改nacos依赖

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

Sentinel 规则持久化

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

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

3. 添加nacos支持

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

Sentinel 规则持久化

4. 修改nacos地址

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

Sentinel 规则持久化

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

Sentinel 规则持久化

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

nacos.addr=localhost:8848

5. 配置nacos数据源

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

Sentinel 规则持久化

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

Sentinel 规则持久化

6. 修改前端页面

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

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

Sentinel 规则持久化

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

Sentinel 规则持久化

修改其中的文本:

Sentinel 规则持久化

7. 重新编译、打包项目

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

Sentinel 规则持久化

8.启动

启动方式跟官方一样:

java  -jar  sentinel-dashboard.jar

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

java  -jar  -Dnacos.addr=localhost:8848  sentinel-dashboard.jar
本作品采用《CC 协议》,转载必须注明作者和本文链接
写这些文章的初衷只是记录一下自己的学习过程,避免自己忘记
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
文章
110
粉丝
6
喜欢
39
收藏
77
排名:812
访问:8435
私信
所有博文
社区赞助商