代码自定义 FlowRuleManager.loadRules(flowRules) 不生效
- Dominant language
- Java
- Stars
- 23.1k
- Forks
- 8.1k
- PR merge metrics
- No merged PRs in 30d
Description
在我们得项目中,我们想要实现使用java代码得方式去自定义流控规则,而不是通过sentinel-dashboard 控制台得方式,下面是我遇到得问题
## 代码
```java
@Component
public class NacosListener {
private static final String FLOW_RULE_DATA_ID = "esb-client-flow-rules";
private static final String FLOW_RULE_GROUP = "SENTINEL_GROUP";
private static final String NACOS_SERVER_ADDR = "localhost:8848";
private static final String NACOS_NAMESPACE = "36a6cffb-6ec5-4ed8-a357-f4170e2ab131";
@Autowired
FlowRuleNacosPublisher flowRuleNacosPublish;
// @NacosConfigListener(dataId = FLOW_RULE_DATA_ID, groupId = FLOW_RULE_GROUP)
@PostConstruct
public void onConfigChange() throws NacosException {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, NACOS_SERVER_ADDR);
properties.put(PropertyKeyConst.NAMESPACE, NACOS_NAMESPACE);
FlowRuleListConverter flowRuleListConverter = new FlowRuleListConverter();
// 创建 NacosDataSource 并设置转换器
NacosDataSource> nacosDataSource
= new NacosDataSource>(properties, FLOW_RULE_GROUP, FLOW_RULE_DATA_ID, flowRuleListConverter);
// 注册监听器,监听配置变化
nacosDataSource.getProperty().addListener(new PropertyListener>() {
@Override
public void configUpdate(List flowRules) {
// 获得了 nacos 中得规则 运用规则
try {
applyFlowRules(flowRules);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void configLoad(List flowRules) {
try {
applyFlowRules(flowRules);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void applyFlowRules(List flowRules) throws Exception {
System.out.println("______________________________________________________________________");
System.out.println(flowRules);
System.out.println("______________________________________________________________________");
List ruleEntities = new ArrayList<>();
flowRules.stream().forEach(item -> {
FlowRuleEntity flowRule = new FlowRuleEntity();
BeanUtils.copyProperties(item , flowRule);
ruleEntities.add(flowRule);
});
flowRuleNacosPublish.publish("esb-client" , ruleEntities );
FlowRuleManager.loadRules(flowRules);
}
private static class FlowRuleListConverter implements Converter> {
@Override
public List convert(String source) {
// 根据从 Nacos 获取的配置信息将其转换为限流规则对象
return JSON.parseObject(source, new TypeReference>() {
});
}
}
}
```
## 函数解释
注册一个nacos 配置监听器用于监听我设置的 一个关于sentinel 限流规则
我在这个监听器中实现了它的首次加载方法和更新方法,都是去执行一个 applyFlowRules(flowRules) 静态函数,其中flowRules 是一个 List 类型,在applyFlowRules 方法中,我 将 flowRules 发布到 sentienl-dashboard ,并将流控规则重新加载到 内存中
## 问题
经由applyFlowRules(flowRules) 函数执行后,流控规则可以正常发布到控制台,但是 执行了 FlowRuleManager.loadRules(flowRules) 却不起效果,流控规则不生效,请问有什么解决方法吗?
Contributor guide
Research direction
Start with FlowRuleManager.loadRules(flowRules) in applyFlowRules, then trace how the NacosDataSource listener supplies the List and how Sentinel consumes loaded rules. Reproduce the reported no-effect behavior from the shown NacosListener flow and compare the loaded rule state with the expected runtime behavior; done means the cause is isolated and a verified fix or documentation guidance is available.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100