sentinel流控与限流源码解析
2021-11-27 17:33:09 0 举报
sentinel流控与限流源码解析
作者其他创作
大纲/内容
if (currentState.get() == State.OPEN) { return; } if (currentState.get() == State.HALF_OPEN) { // In detecting request if (error == null) { fromHalfOpenToClose(); } else { fromHalfOpenToOpen(1.0d); } return; } 。。。。断路器close 正常计数
com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot#performChecking
@Bean @ConditionalOnMissingBean public SentinelResourceAspect sentinelResourceAspect() { return new SentinelResourceAspect(); }
根据@Spi的order排序执行负值越大,则先执行
①构建slot责任链
使用SPI技术加载所有slotMETA-INF/services/
DegradeSlot
spring.factories
exit执行
com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot#entry
com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.ResponseTimeCircuitBreaker#onRequestComplete
com.alibaba.csp.sentinel.slotchain.ProcessorSlot#exit
com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.ExceptionCircuitBreaker#onRequestComplete
com.alibaba.csp.sentinel.CtSph#lookProcessChain
com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot#exit
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.6.RELEASE</version> </dependency>
响应时间
ClusterBuilderSlot
NodeSelectorSlot
com.alibaba.csp.sentinel.CtEntry#trueExit
com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.ResponseTimeCircuitBreaker#handleStateChangeWhenThresholdExceeded
com.alibaba.csp.sentinel.annotation.aspectj.AbstractSentinelAspectSupport#handleBlockException
AuthoritySlot
com.alibaba.csp.sentinel.CtEntry#exitForContext
②使用责任链,依次执行
①流控策略
异常数、异常比例
com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect#invokeResourceWithSentinel
FlowSlot
firstAbstractLinkedProcessorSlot
null
com.alibaba.csp.sentinel.slotchain.SlotChainProvider#newSlotChain
com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration
com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreaker#tryPass
@Override public boolean tryPass(Context context) { // Template implementation. if (currentState.get() == State.CLOSED) { return true; } if (currentState.get() == State.OPEN) { // For half-open state we allow a request for probing. 超过一段时间,允许一个请求过来,从open到halfOpen return retryTimeoutArrived() && fromOpenToHalfOpen(context); } return false; }
抛出业务异常,走自定义处理函数
责任链模式,构建全部SLOT
②指定业务流程
③finally
com.alibaba.csp.sentinel.spi.SpiLoader#loadFirstInstanceOrDefault
if (currentState.get() == State.OPEN) { return; } if (currentState.get() == State.HALF_OPEN) { // In detecting request // TODO: improve logic for half-open recovery if (rt > maxAllowedRt) { fromHalfOpenToOpen(1.0d); } else { font color=\"#ff0000\
com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.ExceptionCircuitBreaker#handleStateChangeWhenThresholdExceeded
com.alibaba.csp.sentinel.spi.SpiLoader#load
标准的切点/切面在方法执行前后流控策略
LogSlot
如果被流控/降级会抛出blockException处理异常
SystemSlot
Object result = pjp.proceed();
entry执行
com.alibaba.csp.sentinel.slotchain.ProcessorSlot#entry
StatisticSlot
0 条评论
下一页