Sentinel源码分析
2025-02-26 15:07:37 0 举报
Sentinel源码分析,分享给大家学习。 更多干货内容,欢迎关注我的公众号:Fox爱分享
作者其他创作
大纲/内容
负责收集资源的路径,并将这些资源的调用路径.以树状结构存储起来,用于根据调用路径来限流降级
SentinelAutoConfiguration
存在
根据预设的限流规则以及前面 slot 统计的状态,来进行流量控制
获取到所有的授权规则
Sentinel的切面配置类,拦截@SentinelResource注解修饰的方法
调用addInterceptors添加拦截器
next
校验降级规则
AuthoritySlot#entry
SPI机制获取builder
规则校验BlockException异常处理,处理注解属性blockHandler配置的方法
NodeSelectorSlot
fireEntry
校验黑白名单
NodeSelectorSlot#entry
判断当前资源是否存在热点流控规则
是否是集群模式
ClusterBuilderSlot#entry
业务异常Throwable处理,处理注解属性fallback配置的方法
DegradeRuleManager.getCircuitBreakers(r.getName())
SPI机制获取到所有的ProcessorSlot实现类,然后添加到chain中
AoP逻辑,调用Around通知方法@Around(\"sentinelResourceAnnotationPointcut()\")
不通过抛出FlowException
SentinelResourceAspect
获取到Sentinel 上下文
SlotChainBuilder
校验流控规则
获取到当前资源的流控规则
try
sentinel-spring-webmvc-adapter包提供
校验热点参数规则
invokeResourceWithSentinel(ProceedingJoinPoint pjp)
@Bean
throw new ParamFlowException
throw new FlowException
FlowSlot
不通过抛出 SystemBlockException异常
FlowSlot#entry
implements
sentinel web自动配置类
ParamFlowSlot
SystemRuleManager.checkSystem(resourceWrapper)
Object result = pjp.proceed()
向上层抛出异常
获取当前资源的授权规则
授权规则校验核心逻辑
FlowRuleChecker#passLocalCheck
DegradeSlot#performChecking
finally
校验系统规则
DefaultBlockExceptionHandler#handle
根据配置的黑白名单和调用来源信息,来做黑白名单控制
FlowRuleChecker#canPassCheck
DefaultController#canPass
针对资源的平均响应时间(RT)以及异常比率,来决定资源是否在接下来的时间被自动熔断掉
sentinel-core-1.8.0.jar!\\META-INF\\services
for循环执行
Context context = ContextUtil.getContext()
DegradeSlot
ruleProvider.apply(resource.getName())
@SentinelResource
DegradeSlot#entry
StatisticSlot 是 Sentinel 的核心功能插槽之一,用于统计实时的调用数据
node.increaseBlockQps(count)
first
获取到ProcessorSlotChain
没有就会构建
cb.tryPass(context)
WebMvcConfigurer
throw new DegradeException
SystemSlot#entry
ParamFlowSlot#entry
rule.isClusterMode()
校验热点流控规则
捕获BlockException异常的处理逻辑
ProcessorSlotEntryCallback
chain = SlotChainProvider.newSlotChain()
LogSlot#entry
触发后面的规则校验
extends
ProcessorSlot<Object> chain = lookProcessChain(resourceWrapper)
AuthorityRuleManager.getAuthorityRules()
catch
AuthoritySlot
HandlerInterceptor
Spring WebMvc请求
throw new SystemBlockException
StatisticSlot#entry
authorityRules.get(resource.getName())
不存在
LogSlot
FlowRuleChecker#passClusterCheck
ParamFlowRuleManager.getRulesOfResource(resourceWrapper.getName())
SpringMvc配置接口
FlowRuleManager.getFlowRuleMap()
SpiLoader.loadPrototypeInstanceListSorted(ProcessorSlot.class)
chainMap.get(resourceWrapper)
SentinelWebAutoConfiguration
不通过抛出DegradeException异常
不通过抛出 AuthorityException异常
FlowRuleChecker#checkFlow
SentinelWebInterceptor
是否存在热点参数规则
StatisticSlot
DefaultSlotChainBuilder#build
ClusterBuilderSlot
Throw e
end
构建资源的校验链条ProcessorSlotChain每个资源都有都有自己独立的处理链条
不通过抛出ParamFlowException异常
throw new AuthorityException
AbstractLinkedProcessorSlot
TrafficShapingController
ParamFlowRuleManager.hasRules(resourceWrapper.getName())
http://localhost:8806/user/findOrderByUserId/1
slotChainBuilder.build()
BlockExceptionHandler
AbstractSentinelInterceptor#preHandle
DefaultProcessorSlotChain
依次调用校验链条soltChain中每个规则slot的entry方法
CircuitBreaker
获取到当前资源的热点参数规则
先从缓存chainMap中取resource对应的ProcessorSlotChain
是
调用业务逻辑方法
SystemSlot
捕获到BlockException
否
根据对于当前系统的整体情况,对入口资源的调用进行动态调配,来控制总的入口流量
CtSph#entryWithType
获取到当前资源的熔断器

收藏

收藏
0 条评论
下一页