https://cloud.tencent.com/developer/article/1452017
https://github.com/alibaba/Sentinel/wiki/How-to-Use
https://github.com/alibaba/Sentinel/wiki/FAQ
集群https://github.com/alibaba/Sentinel/wiki/%E9%9B%86%E7%BE%A4%E6%B5%81%E6%8E%A7
网关https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81
热点参数限流:https://github.com/alibaba/Sentinel/wiki/%E7%83%AD%E7%82%B9%E5%8F%82%E6%95%B0%E9%99%90%E6%B5%81
动态规则配置:https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95
作用:
规则类型
FlowRuleManager.loadRules(List<FlowRule> rules); // 流控规则 DegradeRuleManager.loadRules(List<DegradeRule> rules); // 降级规则 SystemRuleManager.loadRules(List<SystemRule> rules); // 系统规则 AuthorityRuleManager.loadRules(List<AuthorityRule> rules); // 授权规则
1. 使用方法
a. 引入sentinel-core和sentinel-annotation-aspectj
b. 加入切面类
<bean id="sentinelResourceAspect" class="com.qxwz.sentinel.annotation.aspectj.SentinelResourceAspect"/>
c. 通过启动时候配置规则
// 限流
List<FlowRule> rules = new ArrayList<>(); FlowRule rule = new FlowRule(); rule.setResource("name"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); rule.setCount(0); rules.add(rule); FlowRuleManager.loadRules(rules);
------------------------------------------------------------------------------------------------------------
//熔断 1. 平均响应时间 (DEGRADE_GRADE_RT) 2. 异常比例 (DEGRADE_GRADE_EXCEPTION_RATIO) 3. 异常数 (DEGRADE_GRADE_EXCEPTION_COUNT)
List<DegradeRule> rules = new ArrayList<>();
DegradeRule rule = new DegradeRule();
rule.setResource(KEY);
rule.setCount(10);
rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
rule.setTimeWindow(10);
rules.add(rule);
DegradeRuleManager.loadRules(rules);
------------------------------------------------------------------------------------------------------------
// 系统保护规则 1. Load(仅对 Linux/Unix-like 机器生效) 2. RT:当单台机器上所有入口流量的平均 RT 达到阈值即触发系统保护,单位是毫秒
// 3. 线程数:当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护 4. 入口 QPS:当单台机器上所有入口流量的 QPS 达到阈值即触发系统保护
List<SystemRule> rules = new ArrayList<>();
SystemRule rule = new SystemRule();
rule.setHighestSystemLoad(10);
rules.add(rule);
SystemRuleManager.loadRules(rules);
------------------------------------------------------------------------------------------------------------
// 访问规则控制
AuthorityRule rule = new AuthorityRule();
rule.setResource("test");
rule.setStrategy(RuleConstant.AUTHORITY_WHITE);
rule.setLimitApp("appA,appB");
AuthorityRuleManager.loadRules(Collections.singletonList(rule));
------------------------------------------------------------------------------------------------------------
//热点规则
sentinel-parameter-flow-control
ParamFlowRule rule = new ParamFlowRule(resourceName)
.setParamIdx(0)
.setCount(5);
// 针对 int 类型的参数 PARAM_B,单独设置限流 QPS 阈值为 10,而不是全局的阈值 5.
ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf(PARAM_B))
.setClassType(int.class.getName())
.setCount(10);
rule.setParamFlowItemList(Collections.singletonList(item));
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
d. 在需要流控的方法上加注解定义异常类及异常处理方法@SentinelResource(value = "name", blockHandler = "handleException", blockHandlerClass = { ExceptionUtil.class }, fallback = "fallbackHandler")