4.Sentinel源码分析— Sentinel是如何做到降级的? (4)

如果求得的平均响应时间小于设置的count时间,那么就重置passCount并返回true,表示不抛出异常;如果有连续5次的响应时间都超过了count,那么就返回false抛出异常进行降级。

DEGRADE_GRADE_EXCEPTION_RATIO根据异常比例降级 if (grade == RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) { //获取每秒异常的次数 double exception = clusterNode.exceptionQps(); //获取每秒成功的次数 double success = clusterNode.successQps(); //获取每秒总调用次数 double total = clusterNode.totalQps(); // If total amount is less than minRequestAmount, the request will pass. // 如果总调用次数少于5,那么不进行降级 if (total < minRequestAmount) { return true; } // In the same aligned statistic time window, // "success" (aka. completed count) = exception count + non-exception count (realSuccess) double realSuccess = success - exception; if (realSuccess <= 0 && exception < minRequestAmount) { return true; } if (exception / success < count) { return true; } } 。。。 return false;

这个方法中获取成功调用的Qps和异常调用的Qps,验证后,然后求一下比率,如果没有大于count,那么就返回true,否则返回false抛出异常。

我们再进入到exceptionQps方法中看一下:
StatisticNode#exceptionQps

public double exceptionQps() { return rollingCounterInSecond.exception() / rollingCounterInSecond.getWindowIntervalInSec(); }

rollingCounterInSecond.getWindowIntervalInSec方法是表示窗口的时间长度,用秒来表示。这里返回的是1。
ArrayMetric#exception

public long exception() { data.currentWindow(); long exception = 0; List<MetricBucket> list = data.values(); for (MetricBucket window : list) { exception += window.exception(); } return exception; }

这个方法和我上面分析的差不多,大家看看就好了。

根据异常数降级DEGRADE_GRADE_EXCEPTION_COUNT if (grade == RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) { double exception = clusterNode.totalException(); if (exception < count) { return true; } }

根据异常数降级是非常的直接的,直接根据统计的异常总次数判断是否超过count。

到这里就讲完了降级的实现咯~~

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydpsp.html