动态配置
推荐使用psuh模式,结合nacos使用
组件版本推荐
这里使用:
springboot 2.3.2.RELEASE
springcloud Hoxton.SR8
spring alibaba cluod 2.2.5.RELEASE
sentinel-datasource-nacos 1.8.0
sentinel 1.8.5
sentinel整合nacos
注解配置、控制台配置都无法解决配置丢失问题,通过Nacos配置中心解决该问题
下载源码:https://github.com/alibaba/Sentinel/archive/refs/tags/1.8.5.zip
-
修改pom.xml
将test这块注释掉 -
将test路径下的nacos文件夹拷贝到rule下
-
在配置文件中增加nacos配置
-
修改NacosConfig的配置
-
NacosConfigUtil注意groupid和后缀
-
修改规则方法的引用,新增nacos的方法
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
- 修改sidebar.html文件
<li ui-sref-active="active" ng-if="entry.appType==0">
<a ui-sref="dashboard.flowV1({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则 V1</a>
</li>
<li ui-sref-active="active" ng-if="!entry.isGateway">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则 Nacos</a>
</li>
-
修改 FlowServiceV1 为FlowServiceV2
-
修改flow_v2.html
-
打包
客户端集成
- pom.xml
<!--sentinel持久化 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- nacos服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos配置 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
- 配置
server:
port: 8806
spring:
application:
name: sentinel-user-nacos-push #微服务名称
#配置nacos注册中心地址
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
sentinel:
transport:
# 添加sentinel的控制台地址
dashboard: 127.0.0.1:8080
# 指定应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用的HttpServer
#port: 8719
clientIp: localhost
datasource:
# ds1: #名称自定义,唯一
# nacos:
# server-addr: 127.0.0.1:8848
# dataId: ${spring.application.name}-flow-rules
# groupId: SENTINEL_GROUP
# data-type: json
# rule-type: flow
flow-rules:
nacos:
namespace: dddddd
server-addr: 127.0.0.1:8848
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP# 注意groupId对应Sentinel Dashboard中的定义
data-type: json
rule-type: flow
degrade-rules:
nacos:
server-addr: 127.0.0.1:8848
dataId: ${spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
data-type: json
rule-type: degrade
param-flow-rules:
nacos:
server-addr: 127.0.0.1:8848
dataId: ${spring.application.name}-param-flow-rules
groupId: SENTINEL_GROUP
data-type: json
rule-type: param-flow
authority-rules:
nacos:
server-addr: 127.0.0.1:8848
dataId: ${spring.application.name}-authority-rules
groupId: SENTINEL_GROUP
data-type: json
rule-type: authority
system-rules:
nacos:
server-addr: 127.0.0.1:8848
dataId: ${spring.application.name}-system-rules
groupId: SENTINEL_GROUP
data-type: json
rule-type: system
main:
allow-bean-definition-overriding: true
#暴露actuator端点 http://localhost:8800/actuator/sentinel
management:
endpoints:
web:
exposure:
include: '*'
#feign:
# sentinel:
# enabled: true #开启sentinel对feign的支持 默认false
- 在配置中心创建流控规则文件:sentinel-user-nacos-push-flow-rules
[
{
"clusterConfig":{
"acquireRefuseStrategy":0,
"clientOfflineTime":2000,
"fallbackToLocalWhenFail":true,
"resourceTimeout":2000,
"resourceTimeoutStrategy":0,
"sampleCount":10,
"strategy":0,
"thresholdType":0,
"windowIntervalMs":1000
},
"clusterMode":false,
"controlBehavior":0,
"count":1,
"grade":1,
"limitApp":"default",
"maxQueueingTimeMs":500,
"resource":"/test1",
"strategy":0,
"warmUpPeriodSec":10
}
]
注意:“limitApp”:“default”,这个参数默认为default
- 验证nacos里面是不是有
- sentinel 控制台是不是也有该规则
由于懒加载的方式,需要先访问服务才能显示
资源包:sentinel-dashboard-1.8.5.jar