在前面的博客中,我给大家演示了使用 @SentinelResource 定义资源完成限流的例子,

下面就从源码解析开始,看下SentinelResource是如何实现限流的,以及@SentinelResource提供了哪些功能,支持哪些属性。

@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 { @Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
}

三、总结

这节内容学习了@SentinelResource的相关属性,

以及在项目中通过切面开启SentinelResource的方式,

不过为什么使用@SentinelResource必须开启切面?

下一节就学习下 Sentinel中的 SentinelResourceAspect 切面原理。

一个注解搞懂 Sentinel,@SentinelResource 总结的更多相关文章

  1. 一个注解搞定SpringBoot接口定制属性加解密

    前言 上个月公司另一个团队做的新项目上线后大体上运行稳定,但包括研发负责人在内的两个人在项目上线后立马就跳槽了,然后又交接给了我这个「垃圾回收人员」. 本周甲方另一个厂家的监控平台扫描到我们这个项目某 ...

  2. Spring Security基于Oauth2的SSO单点登录怎样做?一个注解搞定

    一.说明 单点登录顾名思义就是在多个应用系统中,只需要登录一次,就可以访问其他相互信任的应用系统,免除多次登录的烦恼.本文主要介绍 同域 和 跨域 两种不同场景单点登录的实现原理,并使用 Spring ...

  3. 一个故事搞懂Java并发编程

    最近在给别人讲解Java并发编程面试考点时,为了解释锁对象这个概念,想了一个形象的故事.后来慢慢发现这个故事似乎能讲解Java并发编程中好多核心概念,于是完善起来形成了了这篇文章.大家先忘记并发编程, ...

  4. 一个系列搞懂YARN(1)——Yarn架构

    前言 几天前和大哥说起了Yarn,大哥问我,你知道Yarn里面怎么进行资源的动态分配回收的吗?我和诚实,说不知道,然后就有了这个系列博文.不同版本的hadoop版本对应的yarn文档会有差别,本文中选 ...

  5. ERP 到底是什么? 一则故事搞懂ERP

    你知道什么是ERP? ERP是什么? 你知道什么是ERP吗? (通俗易懂版) 一个故事搞懂“ERP” 一天中午,丈夫在外给家里打电话:“亲爱的老婆,晚上我想带几个同事回家吃饭可以吗?”(订货意向) 妻 ...

  6. 一个例子搞清楚Java程序执行顺序

    当我们new一个GirlFriend时,我们都做了什么? 一个例子搞懂Java程序运行顺序 public class Girl { Person person = new Person("G ...

  7. 这一次搞懂Spring事务注解的解析

    前言 事务我们都知道是什么,而Spring事务就是在数据库之上利用AOP提供声明式事务和编程式事务帮助我们简化开发,解耦业务逻辑和系统逻辑.但是Spring事务原理是怎样?事务在方法间是如何传播的?为 ...

  8. Spring第四天,BeanPostProcessor源码分析,彻底搞懂IOC注入及注解优先级问题!

  9. java线程间通信:一个小Demo完全搞懂

    版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.从一个小Demo说起 上篇我们聊到了Java多线程的同步 ...

随机推荐

  1. ES6类的继承

    ES6 引入了关键字class来定义一个类,constructor是构造方法,this代表实例对象. constructor相当于python的init 而this 则相当于self 类之间通过ext ...

  2. Visual Studio Team Services and Team Foundation Server官方资料入口

    Team Foundation Server msdn 中文文档入口 Visual Studio Team Services or Team Foundation Server www.visuals ...

  3. map实现删除给定字符串中的小写字母

    def del_lowerletters(s): if s>='a' and s<='z': return " " else: return s print(" ...

  4. Linux 操作虚拟机、数据库

    1.打开虚拟机,输入命令:ifconfig 查看iP和端口号,端口号一般为:22 2.打开Xshell(先安装好),连接虚拟机(根据iP和端口号) 若连接成功,Xshell则会显示虚拟机的ip和端口号 ...

  5. 第二阶段:1.流程图:9.excel绘制甘特图

    后面的框都是日期 可以以一个月为周期计算或者周或者... 因为产品经理应该严格把控产品的时间 因此甘特图特别有必要 注意:任务拆解的越细 把控度越强 然后对格式进行设置 注意时间下面可以用颜色填充来表 ...

  6. SpringBoot源码学习系列之启动原理简介

    本博客通过debug方式简单跟一下Springboot application启动的源码,Springboot的启动源码是比较复杂的,本博客只是简单梳理一下源码,浅析其原理 为了方便跟源码,先找个Ap ...

  7. 在Asp.Net Core中使用ModelConvention实现全局过滤器隔离

    从何说起 这来自于我把项目迁移到Asp.Net Core的过程中碰到一个问题.在一个web程序中同时包含了MVC和WebAPI,现在需要给WebAPI部分单独添加一个接口验证过滤器IActionFil ...

  8. 洛谷P5664 Emiya 家今天的饭 题解 动态规划

    首先来看一道题题: 安娜写宋词 题目背景 洛谷P5664 Emiya 家今天的饭[民间数据] 的简化版本. 题目描述 安娜准备去参加宋词大赛,她一共掌握 \(n\) 个 词牌名 ,并且她的宋词总共有 ...

  9. 1067 试密码 (20分)C语言

    当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死.本题就请你实现这个小功能. 输入格式: 输入在第一行给出一个密码(长度不超过 20 的.不包含空格. ...

  10. 在Git的PR(Pull Request)提示冲突无法merge合并的解决方案

    问题 假设有一个分支A,向master分支提交PR,然后发生无法自动解决的冲突,PR提示不能执行merge合并. 解决方案1 本地checkout检出并切换到A分支,pull拉取更新到最新代码 在本地 ...