当前位置:网站首页>Sentinel规则持久化进Nacos
Sentinel规则持久化进Nacos
2022-04-23 18:36:00 【华为云】
@toc
源码地址:gitee代码仓库地址
1、为什么要将Sentienl规则持久化?
一旦我们重启应用,sentinel规则将消失,生产环境需要将配置规则进行持久化
2、持久化的思路
我们现在将限流配置规则持久化进Nacos保存,只要刷新8401某个rest地址,sentinel控制台的流控规则就能看到,只要Nacos里面的配置不删除,针对8401上sentinel上的流控规则持续有效。

3、操作步骤
3.1 项目中引入依赖
<!--SpringCloud ailibaba sentinel-datasource-nacos --><dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId></dependency>

3.2 添加Nacos数据源配置
application.yml
server: port: 8401spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: #Nacos服务注册中心地址 server-addr: localhost:8848 sentinel: transport: #配置Sentinel dashboard地址 dashboard: localhost:8080 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口 port: 8719 datasource: ds1: #添加nacos数据源的配置,将sentinel配置持久化进nacos里面 nacos: server-addr: localhost:8848 dataId: cloudalibaba-sentinel-service groupId: DEFAULT_GROUP data-type: json rule-type: flowmanagement: endpoints: web: exposure: include: '*'

3.3 添加Nacos业务规则配置

[ { "resource": "/rateLimit/byUrl", "limitApp": "default", "grade": 1, "count": 1, "strategy": 0, "controlBehavior": 0, "clusterMode": false }]
resource:资源名称;
limitApp:来源应用;
grade:阈值类型,0表示线程数,1表示QPS;
count:单机阈值;
strategy:流控模式,0表示直接,1表示关联,2表示链路;
controlBehavior:流控效果,0表示快速失败,1表示Warm Up,2表示排队等待;
clusterMode:是否集群。
3.4 启动sentinel

3.5 此时启动8401微服务并刷新Sentinel
发现业务规则有了。
nacos上面:

sentinel上面:

3.6 测试
我们上面配置的意思是,该接口每秒只能处理1个请求,那我们频繁刷新请求,看看限流规则是否生效。
先正常访问:http://localhost:8401/rateLimit/byUrl

频繁访问:http://localhost:8401/rateLimit/byUrl

可以看到,触发了限流规则。这里我们程序中并没有指定blockHandler,所以显示的是默认限流之后的信息。
3.7 停止8401微服务查看

停止8401发现流控规则没有了。
3.8 重启8401微服务再看sentinel

还是没有,我们访问一次http://localhost:8401/rateLimit/byUrl,再刷新Sentinel

配置出现了,持久化验证通过。
版权声明
本文为[华为云]所创,转载请带上原文链接,感谢
https://bbs.huaweicloud.com/blogs/349221
边栏推荐
- Query the logistics update quantity according to the express order number
- SQL database syntax learning notes
- 昇腾 AI 开发者创享日全国巡回首站在西安成功举行
- 深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
- 硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
- os_authent_prefix
- 机器学习理论之(8):模型集成 Ensemble Learning
- Configure iptables
- Mysqldump backup database
- K210 serial communication
猜你喜欢

ctfshow-web362(SSTI)

The vivado project corresponding to the board is generated by TCL script

Teach you to quickly rename folder names in a few simple steps

From introduction to mastery of MATLAB (2)

Custom prompt box MessageBox in QT

Tangle

Machine learning practice - naive Bayes

Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash

Machine learning theory (8): model integration ensemble learning

【ACM】509. Fibonacci number (DP Trilogy)
随机推荐
Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
Ionic instruction set order from creation to packaging
【ACM】455. Distribute Biscuits (1. Give priority to big biscuits to big appetite; 2. Traverse two arrays with only one for loop (use subscript index -- to traverse another array))
SQL中函数 decode()与 replace()的用法
kettle庖丁解牛第17篇之文本文件输出
The vivado project corresponding to the board is generated by TCL script
Promote QT default control to custom control
【ACM】70. climb stairs
Mysqldump backup database
多功能工具箱微信小程序源码
使用晨曦记账本,分析某个时间段每个账户收支结余
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
Chondroitin sulfate in vitreous
Using transmittablethreadlocal to realize parameter cross thread transmission
Const keyword, variable and function are decorated with const
Tangle
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
【ACM】376. Swing sequence
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
22年字节跳动飞书人力套件三面面经