代码:https://gitee.com/gsls200808/sentinel-dashboard-nacos
jar包:https://gitee.com/gsls200808/sentinel-dashboard-nacos/releases/tag/v1.8.6.0
代码如果看不到可能需要登录。
官方参考文档:
动态规则扩展 · alibaba/Sentinel Wiki · GitHub
需要修改的代码如下:
为了便于后续版本集成nacos,简单讲一下集成思路
1.更改pom
修改sentinel-datasource-nacos的范围
将
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<scope>test</scope>
</dependency>
改为
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!--<scope>test</scope>-->
</dependency>
2.拷贝示例
将test目录下的com.alibaba.csp.sentinel.dashboard.rule.nacos包下的内容拷贝到src的 com.alibaba.csp.sentinel.dashboard.rule的目录
test目录只包含限流,其他规则参照创建即可。
创建时注意修改常量,并且在NacosConfig实现各种converter
3.修改controller
v2目录的FlowControllerV2
将
@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;
controller目录的其他controller包括AuthorityRuleController DegradeController FlowControllerV1 ParamFlowRuleController SystemController
做如下更改(以DegradeController为例)
将
@Autowired
private SentinelApiClient sentinelApiClient;
改成
@Autowired
@Qualifier("degradeRuleNacosProvider")
private DynamicRuleProvider<List<DegradeRuleEntity>> ruleProvider;
@Autowired
@Qualifier("degradeRuleNacosPublisher")
private DynamicRulePublisher<List<DegradeRuleEntity>> rulePublisher;
将原有publishRules方法删除,统一改成
private void publishRules(/*@NonNull*/ String app) throws Exception {
List<DegradeRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
}
之后解决报错的地方即可。
获取所有rules的地方
将
List<DegradeRuleEntity> rules = sentinelApiClient.fetchDegradeRuleOfMachine(app, ip, port);
改成
List<DegradeRuleEntity> rules = ruleProvider.getRules(app);
原有调用publishRules方法的地方,删除掉
在上一个try catch方法里加上
publishRules(entity.getApp());
这里的entity.getApp()也有可能是oldEntity.getApp()/app等变量。根据删除的publishRules代码片段推测即可。
4.修改前端文件
文件路径:src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html
将
<a ui-sref="dashboard.flowV1({app: entry.app})">
改成
<a ui-sref="dashboard.flow({app: entry.app})">
5.最后打包即可
执行命令打包成jar
mvn clean package
运行方法与官方jar一致,不做赘述
6.微服务程序集成
pom.xml添加
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
配置文件application.yml添加
spring:
cloud:
sentinel:
datasource:
# 名称随意
flow:
nacos:
server-addr: 192.168.11.5:8848
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
# 规则类型,取值见:
# org.springframework.cloud.alibaba.sentinel.datasource.RuleType
rule-type: flow
degrade:
nacos:
server-addr: 192.168.11.5:8848
dataId: ${spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
rule-type: degrade
system:
nacos:
server-addr: 192.168.11.5:8848
dataId: ${spring.application.name}-system-rules
groupId: SENTINEL_GROUP
rule-type: system
authority:
nacos:
server-addr: 192.168.11.5:8848
dataId: ${spring.application.name}-authority-rules
groupId: SENTINEL_GROUP
rule-type: authority
param-flow:
nacos:
server-addr: 192.168.11.5:8848
dataId: ${spring.application.name}-param-flow-rules
groupId: SENTINEL_GROUP
rule-type: param-flow
7.测试验证
在sentinel控制台界面添加几个流控规则后尝试关闭微服务和sentinel,然后重新打开sentinel和微服务,看流控规则是否还在。