@SentinelResource可以说是Sentinel学习的突破口,搞懂了这个注解的应用,基本上就搞清楚了 Sentinel 的大部分应用场景。

一、@SentinelResource 解析

Sentinel 提供了 @SentinelResource 注解用于定义资源,并提供了 AspectJ 的扩展用于自动定义资源、处理 BlockException 等。

1、SentinelResource源码

查看 Sentinel的源码,可以看到 SentinelResource 定义了value,entryType,resourceType,blockHandler,fallback,defaultFallback等属性。

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface SentinelResource {/*** @return */String value() default "";/*** @return the entry type (inbound or outbound), outbound by default*/EntryType entryType() default EntryType.OUT;/*** @return the classification (type) of the resource* @since 1.7.0*/int resourceType() default 0;/*** @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 "";/*** The {@code defaultFallback} is used as the default universal fallback method.* It should not accept any parameters, and the return type should be compatible* @return name of the default fallback method, empty by default* @since 1.6.0*/String defaultFallback() default "";/*** The {@code fallback} 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 fallback,* then users can set the class where the fallback function exists. Note that the shared fallback method* @return the class where the fallback method is located (only single class)* @since 1.6.0*/Class<?>[] fallbackClass() default {};/*** @return the list of exception classes to trace, {@link Throwable} by default* @since 1.5.1*/Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class};/*** Indicates the exceptions to be ignored. Note that {@code exceptionsToTrace} should* not appear with {@code exceptionsToIgnore} at the same time, or {@code exceptionsToIgnore}* will be of higher precedence.** @return the list of exception classes to ignore, empty by default* @since 1.6.0*/Class<? extends Throwable>[] exceptionsToIgnore() default {};
}

2、属性说明

参考源码的注释,逐个解释下这几个属性的作用。

value

资源名称,必需项,因为需要通过resource name找到对应的规则,这个是必须配置的。

entryType

entry 类型,可选项,有IN和OUT两个选项,默认为 EntryType.OUT。

public enum EntryType {IN("IN"),OUT("OUT");
}

blockHandler

blockHandler 对应处理 BlockException 的函数名称,可选项。
blockHandler 函数访问范围需要是 public,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException。

blockHandlerClass

blockHandler 函数默认需要和原方法在同一个类中,如果希望使用其他类的函数,则需要指定 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。

fallback

fallback 函数名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。

fallbackClass

fallbackClass的应用和blockHandlerClass类似,fallback 函数默认需要和原方法在同一个类中。
若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。

defaultFallback(since 1.6.0)

如果没有配置defaultFallback方法,默认都会走到这里来。
默认的 fallback 函数名称,可选项,通常用于通用的 fallback 逻辑。
默认 fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,则只有 fallback 会生效。

exceptionsToIgnore(since 1.6.0)

用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 fallback 逻辑中,而是会原样抛出。

二、Sentinel切面配置

应用 @SentinelResource 注解,必须开启对应的切面,引入SentinelResourceAspect。

1、AspectJ方式

Sentinel支持 AspectJ 的扩展用于自动定义资源、处理 BlockException 等,如果应用中开启了 AspectJ,那么需要在 aop.xml 文件中引入对应的 Aspect:

<aspects><aspect name="com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect"/>
</aspects>

2、Spring AOP 方式

如果应用中使用了 Spring AOP,需要在代码中添加SentinelResourceAspect的Bean,通过配置的方式将 SentinelResourceAspect 注册为一个 Spring Bean:

@Configuration
public class SentinelAspectConfiguration {@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();}
}

三、总结

这节内容学习了@SentinelResource的相关属性,以及在项目中通过切面开启SentinelResource的方式,不过为什么使用@SentinelResource必须开启切面?

作者:邴越
链接:https://www.jianshu.com/p/b61727df6ea5
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

一个注解搞懂 Sentinel,@SentinelResource总结相关推荐

  1. java写一个外网访问的接口_【JAVA基础】一个案例搞懂类、对象、重载、封装、继承、多态、覆盖、抽象和接口概念及区别(中篇)...

    0 前言 初学JAVA时,总会对一些概念一知半解,相互混淆,不明其设计的用意,如类.对象.重载.封装.继承.多态.覆盖.抽象类.接口概念.为便于理解和巩固,本文将基于一个案例及其变形,展现各个概念的定 ...

  2. 一个案例搞懂工厂模式和单例模式

    一个案例搞懂工厂模式和单例模式 1 单例模式 一个对象只有一个实例 单例类必须自己创建自己的唯一实例. 单例类必须给所有其他对象提供这一实例. 注意:所有的单例模式,应当使其构造方法私有化. 1.1 ...

  3. 一个注解搞定接口防刷!还有谁不会?

    点击关注公众号,Java干货及时送达 说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点:springboot的基本知识,redis基本操作, 首先是写一个注解类: ...

  4. 一个注解搞定 SpringBoot 接口防刷,还有谁不会?

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者:CS打赢你 blog.csdn.net/weixin ...

  5. 数据翻译的代码辅助插件,一个注解搞定,减少30%SQL代码量

    一.开源项目简介 Easy Trans是一款用于做数据翻译的代码辅助插件,利用MyBatis Plus/JPA/BeetlSQL 等ORM框架的能力自动查表,让开发者可以快速的把ID/字典码 翻译为前 ...

  6. @order注解_Spring Boot+OAuth2,一个注解搞定单点登录!

    今日干货 刚刚发表查看:66666回复:666 公众号后台回复 ssm,免费获取松哥纯手敲的 SSM 框架学习干货. 需要先说一下,松哥最近写的教程,都是成系列的,有一些重复的东西写来写去就没意思了, ...

  7. Java后端:一个注解搞定 Spring Boot 日志!

    此组件解决的问题是: 「谁」在「什么时间」对「什么」做了「什么事」 本组件目前针对 Spring-boot 做了 Autoconfig,如果是 SpringMVC,也可自己在 xml 初始化 bean ...

  8. 一个例子搞懂编码问题

    0x00 前言 相信中文字符编码问题每个程序员都或多或少遇到过,文件读写.网页抓取.数据库等等,凡是有中文的地方总会出现各种的编码问题,但是很少有人愿意花时间去彻底弄清这个问题(至少我以前是这样),每 ...

  9. Android线程创建aop,【android安卓】一个注解搞定线程切换,基于AOP的线程转换框架...

    最简单的使用方法: 模拟进度展示: @RunOnIOThread public void progress() { for (int i = 0; i <= 100; i++) { showPr ...

最新文章

  1. MPB:EGFP荧光标记大肠杆菌的构建
  2. python logging模块的作用_Python 日志模块logging分析及使用-2
  3. 我心中的核心组件(可插拔的AOP)~分布式Session组件
  4. k8s edit命令使用示例
  5. 数组中有一个数字出现的次数超过数组长度的一半
  6. .NET 指南:许可请求
  7. python具备的功能是_用了Python这么多年,揭秘Python不为人知的7大功能和特点!...
  8. sublime Text2 2.0.2 build 2221 64位 破解(已测试)
  9. 分类算法学习(三)——逻辑回归算法的原理及简单实现
  10. Java失宠,谷歌宣布Kotlin现在是Android开发的首选语言
  11. 【土地利用变化分析】土地利用转移矩阵
  12. 1433端口被运营商封锁的解决方法
  13. 第20件事 风险分析
  14. 给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2
  15. Azure Blob Storage 基本用法上传/下载(Java)
  16. Python语言程序设计第二章编程题
  17. 智能家居论文文献_智能家居文献综述范文
  18. HelpingKind.org:一个有偿的市场
  19. nRF24l01无线传输
  20. java 随机生成不重复的数字_生成8位随机不重复的数字编号的方法

热门文章

  1. count(id)count(1)count(*)count(字段)
  2. awk 截取部分字符串_linux三剑客-awk
  3. 关于 $ Super $ $ 和 $ Sub $ $ 的用法
  4. 最新Linux教程发布下载【最后更新4月12日
  5. 扫雷游戏网页版_做游戏,单人行动可行吗(3.让我试试扫雷)
  6. python legb_理解 Python 的 LEGB.
  7. @select注解_mybatis开发,你用 xml 还是注解?我 pick xml
  8. python形参中传入两个实参_认识Python函数的两个概念:形参与实参(16)
  9. switch语句可以被代替吗_大空间建筑内的消防水炮可以代替喷淋装置吗
  10. linux系统怎么建ftp服务器地址,Ubuntu Linux系统建立FTP服务器方法步骤