源码分析sentinel工作流程

本文详细介绍了Sentinel的工作流程,从SentinelResource注解的切入点开始,深入源码分析了SpringBoot自动装配的SentinelResourceAspect切面类,探讨了AOP技术在限流和降级中的应用。Sentinel通过SPI机制加载配置,建立责任链,FlowSlot和DegradeSlot等关键组件协同工作,实现流量控制和熔断降级。
摘要由CSDN通过智能技术生成

一、sentinel简介:

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。伴随着公司用户量和流量的日益增加,对于数据库的压力是越来越大,Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护

二、源码入口

使用sentinel有两种方式,一种方式是对需要限流和降级的接口资源方法上面加入@SentinelResource注解

在这里插入图片描述

还有一种就是通过拦截器的方式进行资源保护限流和降级,其实两种方法执行的关键方法都是同一段代码,今天我们就讲 一下通过注解这种aop切面的形式,sentinel是如何实现限流和降级的呢

源码的如何还是基于SpringBoot的自动装配原理,在spring-cloud-starter-alibaba-sentinel.jar
下面的spring.fatories里面的SentinelAutoConfiguration类,我们来看看这个类的源码
在这里插入图片描述
在这里插入图片描述
在spring容器启动的时候实例化了一个SentinelResourceAspect类,看这个类的命名就应该可以猜到这个类就是一个切面类,我们点击进入

/**

  • Aspect for methods with {@link SentinelResource} annotation.

  • @author Eric Zhao
    */
    @Aspect
    public class SentinelResourceAspect extends AbstractSentinelAspectSupport {

    @Pointcut("@annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)")
    public void sentinelResourceAnnotationPointcut() {
    }

    @Around(“sentinelResourceAnnotationPointcut()”)
    public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable {
    Method originMethod = resolveMethod(pjp);

     SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class);
     if (annotation == null) {
         // Should not go through here.
         throw new IllegalStateException("Wrong state for SentinelResource annotation");
     }
     String resourceName = getResourceName(annotation.value(), originMethod);
     EntryType entryType = annotation.entryType();
     int resourceType = annotation.resourceType();
     Entry entry = null;
     try {
         entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs());
         Object result = pjp.proceed();
         return result;
     } catch (BlockException ex) {
         return handleBlockException(pjp, annotation, ex);
     } catch (Throwable ex) {
         Class<? extends Throwable>[] exceptionsToIgnore = an
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值