源码分析(spring alibaba 源码分析之 sentinel)

本文深入分析Sentinel的源码实现,通过AOP注解@SentinelResource来理解其工作原理。核心逻辑在于通过SphU.entry进入流量控制,经过一系列slot判断,超过阈值则触发限流或降级。文章重点讨论了流控、熔断和降级的三个关键slot,并介绍了滑动时间窗口在统计指标中的应用。
摘要由CSDN通过智能技术生成

前两篇文章只画了流程图,发现反响不够好,没几个阅读量,今天我把看源码的方式也记录下,详细解析下主线逻辑。

sentinel实现其实就是一个大的Try catch ,在StatisticSlot保存各种指标,在其他slot中进行判断,通过就继续执行其他slot,不能通过就抛出异常,达到阈值就进行限流或者降级操作。

先上一个源码分析的流程图

1. sentinel是通过aop实现的它的注解是@SentinelResource那我们就找他的实现

2. 找到SentinelResourceAspect类的invokeResourceWithSentinel方法就是具体实现。

public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable {
    Method originMethod = this.resolveMethod(pjp);
    SentinelResource annotation = (SentinelResource)originMethod.getAnnotation(SentinelResource.class);
    if (annotation == null) {
        throw new IllegalStateException("Wrong state for SentinelResource annotation");
    } else {
        String resourceName = this.getResourceName(annotation.value(), originMethod);
        EntryType entryType = annotation.entryType();
        int resourceType = annotation.resourceType();
        Entry entry = null;

        try {
            Object var18;
            try {
                entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs());
                Object result = pjp.proceed();
                var18 = result;
                return var18;
            } catch (BlockException var15) {
                var18 = this.handleBlockException(pjp, annotation, var15);
                return var18;
            } catch (Throwable var16) {
                Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
                if (exceptionsToIgnore.length > 0 && this.exceptionBelongsTo(var16, exceptionsToIgnore)) {
                    throw var16;
                } else if (this.exceptionBelongsTo(var16, annotation.exceptionsToTrace())) {
                    this.traceException(var16, annotation);
                    Object var10 = this.handleFallback(pjp, annotation, var16);
                    return var10;
                } else {
                    throw var16;
                }
            }
        } finally {
            if (entry != null) {
                entry.exit(1, pjp.getArgs());
            }

        }
    }
}

 

3 调用SphU.entry方法,一步步跟到具体实现,找到CtSph类的entryWithPriority方法。

获取slot的方法。

ProcessorSlot<Object> chain = lookProcessChain(resourceWrapper);
ProcessorSlot<Object> lookProcessChain(ResourceWrapper resourceWrapper) {
    ProcessorSlotChain chain = chainMap.get(resourceWrapper);
    if (chain == null) {
        synchronized (LOCK) {
            chain = chainMap.get(resourceWrapper);
            if (chain == null) {
                // Entry size limit.
                if (chainMap.size() >= Const
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值