博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊sentinel的SentinelResourceAspect
阅读量:7017 次
发布时间:2019-06-28

本文共 4051 字,大约阅读时间需要 13 分钟。

  hot3.png

本文主要研究一下sentinel的SentinelResourceAspect

SentinelResourceAspect

com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java

@Aspectpublic class SentinelResourceAspect {    private final Logger logger = LoggerFactory.getLogger(SentinelResourceAspect.class);    @Pointcut("@annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)")    public void sentinelResourceAnnotationPointcut() {    }    @Around("sentinelResourceAnnotationPointcut()")    public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable {        Method originMethod = getMethod(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 = annotation.value();        EntryType entryType = annotation.entryType();        Entry entry = null;        try {            ContextUtil.enter(resourceName);            entry = SphU.entry(resourceName, entryType);            Object result = pjp.proceed();            return result;        } catch (BlockException ex) {            return handleBlockException(pjp, annotation, ex);        } finally {            if (entry != null) {                entry.exit();            }            ContextUtil.exit();        }    }    //......}
  • 使用aspect的around拦截,拦截标注有SentinelResource的注解
  • 进入方法之前调用SphU.entry(resourceName, entryType),结束之后调用entry.exit();
  • 异常的时候调用handleBlockException方法

handleBlockException

private Object handleBlockException(ProceedingJoinPoint pjp, SentinelResource annotation, BlockException ex)        throws Exception {        // Execute fallback for degrading if configured.        Object[] originArgs = pjp.getArgs();        if (isDegradeFailure(ex)) {            Method method = extractFallbackMethod(pjp, annotation.fallback());            if (method != null) {                return method.invoke(pjp.getTarget(), originArgs);            }        }        // Execute block handler if configured.        Method blockHandler = extractBlockHandlerMethod(pjp, annotation.blockHandler(), annotation.blockHandlerClass());        if (blockHandler != null) {            // Construct args.            Object[] args = Arrays.copyOf(originArgs, originArgs.length + 1);            args[args.length - 1] = ex;            if (isStatic(blockHandler)) {                return blockHandler.invoke(null, args);            }            return blockHandler.invoke(pjp.getTarget(), args);        }        // If no block handler is present, then directly throw the exception.        throw ex;    }
  • 这里会先判断是否是降级需要处理的异常,是的话,则调用fallback方法,否则调用block handler方法

SentinelResource

com/alibaba/csp/sentinel/annotation/SentinelResource.java

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Inheritedpublic @interface SentinelResource {    /**     * @return name of the Sentinel resource     */    String value();    /**     * @return the entry type (inbound or outbound), outbound by default     */    EntryType entryType() default EntryType.OUT;    /**     * @return name of the block exception function, empty by default     */    String blockHandler() default "";    /**     * The {@code blockHandler} is located in the same class with the original method by default.     * However, if some methods share the same signature and intend to set the same block handler,     * then users can set the class where the block handler exists. Note that the block handler method     * must be static.     *     * @return the class where the block handler exists, should not provide more than one classes     */    Class
[] blockHandlerClass() default {}; /** * @return name of the fallback function, empty by default */ String fallback() default "";}
  • 这里可以定义fallback方法,以及blockHandler

小结

sentinel的SentinelResourceAspect采用aspect的around拦截SentinelResource,在执行之前进行限流判断,在捕获异常的时候,会根据异常类型判断是调用fallback方法还是调用block handler方法。

doc

转载于:https://my.oschina.net/go4it/blog/1930171

你可能感兴趣的文章
终端tty、虚拟控制台、FrameBuffer的切换过程
查看>>
组合模式(Composite)解析例子
查看>>
写程序的一些感想和教训
查看>>
cassandra 集群并发测试脚本
查看>>
Think Pad笔记本分区解决思路及方法
查看>>
Java数组应用总结
查看>>
快速浏览Silverlight3 Beta: 在多个Silverlight应用间传递信息
查看>>
Hyper-V 3.0部署PART 11:创建Hyper-V群集
查看>>
打造自己的LINQ Provider(上):Expression Tree揭秘
查看>>
《统一沟通-微软-实战》-6-部署-1-前端服务器-4-设置前端服务器和前端池
查看>>
Symfony1.4.11学习笔记(四):数据模型
查看>>
Lync Server 2010企业版系列PART2:部署DNS
查看>>
运维自动化之使用Cobbler自动化安装系统与FAQ
查看>>
浅谈服务台/事件管理
查看>>
深入浅出串口编程(2)――基于DOS的串口编程
查看>>
归纳一下:C#线程同步的几种方法
查看>>
操作主机PDC Emulator[为企业维护windows server 2008系列六]
查看>>
Memcached常用命令及使用说明
查看>>
Java数据库连接池类源码
查看>>
SQL Server 中,实现 varbinary 与 varchar 类型之间的数据转换
查看>>