想将修改后的流控规则同步到nacos,需要修改sentinel源码,所以大家需要提前讲sentinel源码下载下来:
下载源码
代码语言:javascript复制git clone https://github.com/alibaba/Sentinel.git
定义nacos 服务地址
代码语言:javascript复制/**
* @Author 乐哥聊编程
* @Doc 关注公众号"乐哥聊编程"获取文档和源码
* @Date 2023/6/18
* @Description
*/
@ConfigurationProperties(prefix = "sentinel.nacos")
public class NacosPropertiesConfig {
private String serverAddr;
private String dataId;
private String groupId="DEFAULT_GROUP";
private String namespace="";
}
application.yaml中配置
代码语言:javascript复制sentinel.nacos.server-addr=192.168.64.2:8848
NacosConfigService 初始化
代码语言:javascript复制@Configuration
@EnableConfigurationProperties(NacosPropertiesConfig.class)
public class NacosConfig {
@Bean
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
return s -> JSON.parseArray(s, FlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService(NacosPropertiesConfig nacosPropertiesConfig) throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, nacosPropertiesConfig.getServerAddr());
properties.put(PropertyKeyConst.NAMESPACE, nacosPropertiesConfig.getNamespace());
properties.put(PropertyKeyConst.SERVER_ADDR, nacosPropertiesConfig.getServerAddr());
return ConfigFactory.createConfigService(properties);
}
}
创建Nacos 配置发布器
代码语言:javascript复制@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<List<FlowRuleEntity>, String> converter;
@Override
public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
configService.publishConfig(app NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
NacosConfigUtil.GROUP_ID, converter.convert(rules));
}
}
定义nacos 配置规则解析器
代码语言:javascript复制@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<FlowRuleEntity>> converter;
@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
String rules = configService.getConfig(appName NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
NacosConfigUtil.GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
}
更新FlowControllerV2,配置nacos作为数据来源
代码语言:javascript复制 @Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
修改页面接口调用
sidebar.html
代码语言:javascript复制<li ui-sref-active="active" ng-if="!entry.isGateway">
<a ui-sref="dashboard.flowV1({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则</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> 流控规则</a>
</li>
项目配置修改
代码语言:javascript复制spring:
application:
name: sentinel-nacos-sync
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:8089
datasource:
flow_ds:
nacos:
server-addr: 192.168.64.2:8848
data-id: sentinel-nacos-sync-flow-rules
data-type: json
group-id: SENTINEL_GROUP
rule-type: flow
Nacos 添加流控规则
代码语言:javascript复制[
{
"app": "sentinel-nacos-sync",
"clusterMode": false,
"controlBehavior": 0,
"count": 554.0,
"gmtModified": 1687070437587,
"grade": 1,
"id": 1,
"limitApp": "default",
"resource": "/test1",
"strategy": 0
},
{
"app": "sentinel-nacos-sync",
"clusterMode": false,
"controlBehavior": 0,
"count": 1.0,
"gmtModified": 1687071108157,
"grade": 1,
"id": 2,
"limitApp": "default",
"resource": "/test2",
"strategy": 0
},
{
"app": "sentinel-nacos-sync",
"clusterMode": false,
"controlBehavior": 0,
"count": 55.0,
"grade": 1,
"id": 3,
"limitApp": "default",
"resource": "/test3",
"strategy": 0
},
{
"app": "sentinel-nacos-sync",
"clusterMode": false,
"controlBehavior": 0,
"count": 55.0,
"grade": 1,
"id": 4,
"limitApp": "default",
"resource": "/test4",
"strategy": 0
}
]
启动验证
成功