我是靠谱客的博主 平淡夕阳,这篇文章主要介绍聊聊sentinel的ModifyRulesCommandHandler,现在分享给大家,希望可以做个参考。

本文主要研究一下sentinel的ModifyRulesCommandHandler

ModifyRulesCommandHandler

sentinel-transport-common-0.1.1-sources.jar!/com/alibaba/csp/sentinel/command/handler/ModifyRulesCommandHandler.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
@CommandMapping(name = "setRules") public class ModifyRulesCommandHandler implements CommandHandler<String> { static DataSource<?, List<FlowRule>> flowDataSource = null; static DataSource<?, List<AuthorityRule>> authorityDataSource = null; static DataSource<?, List<DegradeRule>> degradeDataSource = null; static DataSource<?, List<SystemRule>> systemSource = null; public static synchronized void registerFlowDataSource(DataSource<?, List<FlowRule>> datasource) { flowDataSource = datasource; } public static synchronized void registerAuthorityDataSource(DataSource<?, List<AuthorityRule>> dataSource) { authorityDataSource = dataSource; } public static synchronized void registerDegradeDataSource(DataSource<?, List<DegradeRule>> dataSource) { degradeDataSource = dataSource; } public static synchronized void registerSystemDataSource(DataSource<?, List<SystemRule>> dataSource) { systemSource = dataSource; } @Override public CommandResponse<String> handle(CommandRequest request) { String type = request.getParam("type"); // rule data in get parameter String data = request.getParam("data"); if (StringUtil.isNotEmpty(data)) { try { data = URLDecoder.decode(data, "utf-8"); } catch (Exception e) { RecordLog.info("decode rule data error", e); return CommandResponse.ofFailure(e, "decode rule data error"); } } RecordLog.info("receive rule change:" + type); RecordLog.info(data); String result = "success"; if ("flow".equalsIgnoreCase(type)) { List<FlowRule> flowRules = JSONArray.parseArray(data, FlowRule.class); FlowRuleManager.loadRules(flowRules); if (flowDataSource != null) { try { flowDataSource.writeDataSource(flowRules); } catch (Exception e) { result = "partial success"; RecordLog.info(e.getMessage(), e); } } return CommandResponse.ofSuccess(result); } else if ("authority".equalsIgnoreCase(type)) { List<AuthorityRule> rules = JSONArray.parseArray(data, AuthorityRule.class); AuthorityRuleManager.loadRules(rules); if (authorityDataSource != null) { try { authorityDataSource.writeDataSource(rules); } catch (Exception e) { result = "partial success"; RecordLog.info(e.getMessage(), e); } } return CommandResponse.ofSuccess(result); } else if ("degrade".equalsIgnoreCase(type)) { List<DegradeRule> rules = JSONArray.parseArray(data, DegradeRule.class); DegradeRuleManager.loadRules(rules); if (degradeDataSource != null) { try { degradeDataSource.writeDataSource(rules); } catch (Exception e) { result = "partial success"; RecordLog.info(e.getMessage(), e); } } return CommandResponse.ofSuccess(result); } else if ("system".equalsIgnoreCase(type)) { List<SystemRule> rules = JSONArray.parseArray(data, SystemRule.class); SystemRuleManager.loadRules(rules); if (systemSource != null) { try { systemSource.writeDataSource(rules); } catch (Exception e) { result = "partial success"; RecordLog.info(e.getMessage(), e); } } return CommandResponse.ofSuccess(result); } return CommandResponse.ofFailure(new IllegalArgumentException("invalid type")); } } 复制代码
  • 如果type是flow的话,则调用FlowRuleManager.loadRules(flowRules);更新

FlowRuleManager

sentinel-core-0.1.1-sources.jar!/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java

复制代码
1
2
3
4
5
6
7
8
9
/** * Load {@link FlowRule}s, former rules will be replaced. * * @param rules new rules to load. */ public static void loadRules(List<FlowRule> rules) { currentProperty.updateValue(rules); } 复制代码
  • 调用的是DynamicSentinelProperty的updateValue

DynamicSentinelProperty

sentinel-core-0.1.1-sources.jar!/com/alibaba/csp/sentinel/property/DynamicSentinelProperty.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
public void updateValue(T newValue) { if (isEqual(value, newValue)) { return; } RecordLog.info("SentinelProperty, config is real updated to: " + newValue); value = newValue; for (PropertyListener<T> listener : listeners) { listener.configUpdate(newValue); } } 复制代码
  • 这里委托给listeners去进行configUpdate

FlowPropertyListener

sentinel-core-0.1.1-sources.jar!/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private static final class FlowPropertyListener implements PropertyListener<List<FlowRule>> { @Override public void configUpdate(List<FlowRule> value) { Map<String, List<FlowRule>> rules = loadFlowConf(value); if (rules != null) { flowRules.clear(); flowRules.putAll(rules); } RecordLog.info("receive flow config: " + flowRules); } @Override public void configLoad(List<FlowRule> conf) { Map<String, List<FlowRule>> rules = loadFlowConf(conf); if (rules != null) { flowRules.clear(); flowRules.putAll(rules); } RecordLog.info("load flow config: " + flowRules); } } 复制代码
  • 这里的configUpdate方法调用loadFlowConf进行加载

FlowRuleManager.loadFlowConf

sentinel-core-0.1.1-sources.jar!/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManager.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
private static Map<String, List<FlowRule>> loadFlowConf(List<FlowRule> list) { Map<String, List<FlowRule>> newRuleMap = new ConcurrentHashMap<String, List<FlowRule>>(); if (list == null) { return newRuleMap; } for (FlowRule rule : list) { if (StringUtil.isBlank(rule.getLimitApp())) { rule.setLimitApp(FlowRule.LIMIT_APP_DEFAULT); } Controller rater = new DefaultController(rule.getCount(), rule.getGrade()); if (rule.getGrade() == RuleConstant.FLOW_GRADE_QPS && rule.getControlBehavior() == RuleConstant.CONTROL_BEHAVIOR_WARM_UP && rule.getWarmUpPeriodSec() > 0) { rater = new WarmUpController(rule.getCount(), rule.getWarmUpPeriodSec(), ColdFactorProperty.coldFactor); } else if (rule.getGrade() == RuleConstant.FLOW_GRADE_QPS && rule.getControlBehavior() == RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER && rule.getMaxQueueingTimeMs() > 0) { rater = new PaceController(rule.getMaxQueueingTimeMs(), rule.getCount()); } rule.setRater(rater); String identity = rule.getResource(); List<FlowRule> ruleM = newRuleMap.get(identity); if (ruleM == null) { ruleM = new ArrayList<FlowRule>(); newRuleMap.put(identity, ruleM); } ruleM.add(rule); } return newRuleMap; } 复制代码
  • 这里主要是设置rater,rater有两种,一种是WarmUpController,另外一种是PaceController

小结

  • 在sentinel的dashboard给指定实例添加限流规则的话,会同步调用实例的sentinel接口,发送setRules指令
  • 应用端接收到setRules指令之后,使用ModifyRulesCommandHandler来处理,具体就是将规则覆盖本地

doc

  • Sentinel工作主流程

最后

以上就是平淡夕阳最近收集整理的关于聊聊sentinel的ModifyRulesCommandHandler的全部内容,更多相关聊聊sentinel内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部