当前位置:网站首页>Sentinel 实现 pull 模式规则持久化
Sentinel 实现 pull 模式规则持久化
2022-07-24 10:24:00 【流楚丶格念】
文章目录
导言
sentinel的所有规则默认都是内存存储,重启后所有规则都会丢失。在生产环境下,我们必须确保这些规则的持久化,避免丢失。
规则管理模式
规则是否能持久化,取决于规则管理模式,sentinel支持三种规则管理模式:
- 原始模式:Sentinel的默认模式,将规则保存在内存,重启服务会丢失。
- pull模式
- push模式
pull模式
pull模式:控制台将配置的规则推送到Sentinel客户端,而客户端会将配置规则保存在本地文件或数据库中。以后会定时去本地文件或数据库中查询,更新本地规则。

push模式
push模式:控制台将配置规则推送到远程配置中心,例如Nacos。Sentinel客户端监听Nacos,获取配置变更的推送消息,完成本地配置更新。

Sentinel pull模式规则持久化
这里用的是 1.8.1 ,需要自己改源码,不知道后续版本支持自动配置了没
一、修改服务
修改你要引入的微服务项目,让其监听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的持久化,需要修改源码。
从官网把源码载下来:
用IDEA打开项目:
项目目录如下所示,下载依赖包(可能时间会很长,我用阿里云给我下了十多分钟)

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

将sentinel-datasource-nacos依赖的scope去掉:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
3. 添加nacos支持
在sentinel-dashboard的test包下,已经编写了对nacos的支持,我们需要将其拷贝到main下。

4. 修改nacos地址
然后,还需要修改测试代码中的NacosConfig类:

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

在sentinel-dashboard的application.properties中添加nacos地址配置:
nacos.addr=localhost:8848
5. 配置nacos数据源
另外,还需要修改com.alibaba.csp.sentinel.dashboard.controller.v2包下的FlowControllerV2类:

让我们添加的Nacos数据源生效:
按照下面修改

@Autowired
@Qualifier("flowRuleDefaultProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleDefaultPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
6. 修改前端页面
接下来,还要修改前端页面,添加一个支持nacos的菜单。
修改src/main/webapp/resources/app/scripts/directives/sidebar/目录下的sidebar.html文件:

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

修改其中的文本:

7. 重新编译、打包项目
运行IDEA中的maven插件,编译和打包修改好的Sentinel-Dashboard:

8.启动
启动方式跟官方一样:
java -jar sentinel-dashboard.jar
如果要修改nacos地址,需要添加参数:
java -jar -Dnacos.addr=localhost:8848 sentinel-dashboard.jar
边栏推荐
- Android uses JDBC to connect to a remote database
- Tree array-
- 【LeeCode】获取2个字符串的最长公共子串
- zoj 2770 差分约束系统---2--2022年5月20日
- Arduino serial port information reading and output
- cannot unpack non-iterable NoneType object
- Figure model 2-2022-5-13
- Is CITIC Securities a safe and reliable securities firm? How to open an account?
- [STM32 learning] (7) use of serial port 2 (usart2)
- Deployment and analysis of coredns
猜你喜欢
![[STM32 learning] (6) use of serial port 1 (usart1)](/img/b1/430d3501a99e46958c066f7fd7eee9.png)
[STM32 learning] (6) use of serial port 1 (usart1)

Curse of knowledge

给你的网站加一个爱发电角标

WEB安全基础 - - -文件上传(文件上传绕过)

高精尖中心论文入选国际顶会ACL 2022,进一步拓展长安链隐私计算能力

Segment tree--

Dynamic programming -- a collection of stock problems

ZOJ 2770 differential restraint system -- 2 -- May 20, 2022

Scan line, weight segment tree

CRC Coding in C language
随机推荐
fatal: unable to commit credential store: Device or resource busy
cannot unpack non-iterable NoneType object
What is NFT? How to develop NFT system?
Mysql database JDBC programming
The role of glpushmatrix and glpopmatrix
Intranet remote control tool under Windows
Query about operating system security patch information
PostgreSQL rounding
Home raiding II (leetcode-213)
SMTP automatic mail sending function code
Constant pointer, pointer constant
In the envy of LETV, there is a "meaning crisis" of contemporary workers
zoj1137+作业1--2022年5月28日
ZOJ 2770 differential restraint system -- 2 -- May 20, 2022
关联规则--2022年7月10日
[STM32 learning] (14) two 74HC595 controls four nixie tube displays
Trie tree template 2
Curse of knowledge
Implementation and traversal of binary tree and binary tree sorting tree
MySQL 数据库 JDBC编程