当前位置:网站首页>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
边栏推荐
- C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
- CISSP certified daily knowledge points (April 12, 2022)
- 【数学建模】—— 层次分析法(AHP)
- Resolves the interface method that allows annotation requests to be written in postman
- K210 serial communication
- Tangle
- 【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))
- 22年字节跳动飞书人力套件三面面经
- In win10 system, all programs run as administrator by default
- Query the logistics update quantity according to the express order number
猜你喜欢
随机推荐
Daily CISSP certification common mistakes (April 11, 2022)
Log4j2 cross thread print traceid
CISSP certified daily knowledge points (April 14, 2022)
ESP32 LVGL8. 1 - BTN button (BTN 15)
数据库上机实验四(数据完整性与存储过程)
Imx6 debugging LVDS screen technical notes
With the use of qchart, the final UI interface can be realized. The control of qweight can be added and promoted to a user-defined class. Only the class needs to be promoted to realize the coordinate
回路-通路
深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
Robocode tutorial 7 - Radar locking
Golang 语言实现TCP UDP通信
实战业务优化方案总结---主目录---持续更新
If condition judgment in shell language
Quantexa CDI(场景决策智能)Syneo平台介绍
C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
多功能工具箱微信小程序源码
特征选择feature_selection--SelectKBest
22 year flying Book manpower Kit
Using transmittablethreadlocal to realize parameter cross thread transmission
ctfshow-web362(SSTI)









