DegradeSlot则通过统计信息,以及预设的规则,来做熔断降级;
熔断依赖于响应时长,Qps,失败率。
熔断规则.png
timeWindow
为熔断恢复时间熔断模式有三种:
- 0 根据rt时间,当超过指定规则的时间连续超过5笔,则触发熔断。
- DEGRADE_GRADE_RT=0
- 1 根据异常比例熔断 DEGRADE_GRADE_EXCEPTION_RATIO
- 请求总数<5时,不熔断
- 成功数没有,异常数<5时,不熔断。
- 异常数/总数 <配置的总数时,不熔断
- 其他场景,都做熔断。
- 2 根据单位时间内异常总数做熔断
- 当异常总数<配置的总数时,触发熔断。
- 其他场景不熔断。
当熔断触发后,需要等待timewindow时间,再关闭熔断器。
public class DegradeSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args)
throws Throwable {
DegradeRuleManager.checkDegrade(resourceWrapper, context, node, count);
fireEntry(context, resourceWrapper, node, count, prioritized, args);
}
@Override
public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
fireExit(context, resourceWrapper, count, args);
}
}
public static void checkDegrade(ResourceWrapper resource, Context context, DefaultNode node, int count)
throws BlockException {
//获取该resource对应的所有的熔断规则
List<DegradeRule> rules = degradeRules.get(resource.getName());
if (rules == null) {
return;
}
//逐一规则比对
for (DegradeRule rule : rules) {
//如果不通过,则直接异常熔断
if (!rule.passCheck(context, node, count)) {
throw new DegradeException(rule.getLimitApp(), rule);
}
}
}
public class DegradeRule extends AbstractRule {
private static final int RT_MAX_EXCEED_N = 5;
//熔断时长
private int timeWindow;
//配置总数或比例
private double count;
private static ScheduledExecutorService pool = Executors.newScheduledThreadPool(
Runtime.getRuntime().availableProcessors(), new NamedThreadFactory("sentinel-degrade-reset-task", true));
@Override
public boolean passCheck(Context context, DefaultNode node, int acquireCount, Object... args) {
//先判断是否已经熔断
if (cut.get()) {
return false;
}
//获取Cluster节点
ClusterNode clusterNode = ClusterBuilderSlot.getClusterNode(this.getResource());
if (clusterNode == null) {
return true;
}
//RT模式熔断
if (grade == RuleConstant.DEGRADE_GRADE_RT) {
double rt = clusterNode.avgRt();
if (rt < this.count) {
passCount.set(0);
return true;
}
//连续超过指定次数5,直接熔断
if (passCount.incrementAndGet() < RT_MAX_EXCEED_N) {
return true;
}
} else if (grade == RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) {
//异常比例熔断
double exception = clusterNode.exceptionQps();
double success = clusterNode.successQps();
long total = clusterNode.totalQps();
if (total < RT_MAX_EXCEED_N) {
return true;
}
double realSuccess = success - exception;
//如果没有成功的请求,且失败请求少于5个,不触发熔断
if (realSuccess <= 0 && exception < RT_MAX_EXCEED_N) {
return true;
}
//判断异常比例
if (exception / success < count) {
return true;
}
} else if (grade == RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
//异常总数熔断
double exception = clusterNode.totalException();
if (exception < count) {
return true;
}
}
//熔断触发,等待timeWindow时间,关闭熔断
if (cut.compareAndSet(false, true)) {
ResetTask resetTask = new ResetTask(this);
pool.schedule(resetTask, timeWindow, TimeUnit.SECONDS);
}
return false;
}
}
熔断配置则依赖于DegradeRuleManager#loadRules(List<DegradeRule> rules)
方法,最终 RulePropertyListener
更新规则。