Springcloud—B站学习总结—Springcloud Alibaba—sentinel规则持久化
sentinel规则持久化是什么
sentinel规则持久化怎么用
通过修改8401查看sentinel规则持久化
步骤:
1.改pom文件:
添加依赖 Sentinel编写好的流控规则写进我们的的Nacos中,做持久化用到
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
2.改YML文件
在YML文件中添加Nacos数据源配置
图例如上,8401YML配置文件修改后代码如下
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard地址
dashboard: localhost:8080
# 默认8719端口,如果被占用,那么会端口号+1一直找到不被占用的端口
port: 8719
#############添加Nacos数据源配###################################################################
datasource:
ds1:
nacos:
server-addr: localhost:8848
dataId: cloudalibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
###############################################################################################
management:
endpoints:
web:
exposure:
include: '*'
feign:
sentinel:
enabled: true # 激活Sentinel对Feign的支持
3.添加Nacos业务规则配置
添加进Nacos的配置内容(Json格式)
配置内容解析
4.启动8401后刷新Sentinel发现业务规则有了
启动8401后,访问路径loccalhost:8401/rateLimit/byUrl
之后刷新Sentinel,业务规则就有了(原因是重新访问一次Sentinel流控平台可以扫描到)
5.测试(快速访问测试loccalhost:8401/rateLimit/byUrl
,发现流控规则生效了)
如图: