当前位置:网站首页>【微服务|Sentinel】SentinelResourceAspect详解
【微服务|Sentinel】SentinelResourceAspect详解
2022-07-01 22:29:00 【步尔斯特】
AOP
上文我们讲到@SentinelResource注解,我们都知道注解只是一种标记,那么真正实现限流逻辑的就是AOP了。
AOP在实际开发中都会解决哪些业务问题呢?
事务相关
用来回滚事物性能监控
在方法调用前后记录调用时间,方法执行太长或超时报警。缓存代理
缓存某方法的返回值,下次执行该方法时,直接从缓存里获取。软件破解
使用AOP修改软件的验证类的判断逻辑。记录日志
在方法执行前后记录系统日志。工作流系统
工作流系统需要将业务代码和流程引擎代码混合在一起执行,那么我们可以使用AOP将其分离,并动态挂接业务。权限验证
方法执行前验证是否有权限执行当前方法,没有则抛出没有权限执行异常,由业务代码捕捉。
接下来,我们来看一看它的源码吧com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect
源码
@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 = annotation.exceptionsToIgnore();
// The ignore list will be checked first.
if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
throw ex;
}
if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {
traceException(ex);
return handleFallback(pjp, annotation, ex);
}
// No fallback function can handle the exception, so throw it out.
throw ex;
} finally {
if (entry != null) {
entry.exit(1, pjp.getArgs());
}
}
}
}
源码分析
我们来分析一下这块的代码:
- 使用aspect的around拦截,拦截标注有SentinelResource的注解
- 进入方法之前调用SphU.entry(resourceName, entryType),结束之后调用entry.exit();
- 异常的时候调用handleBlockException方法
这也验证我们前一篇文章,在注解内对异常处理的两种方案。
边栏推荐
- locust的使用
- Istio、eBPF 和 RSocket Broker:深入研究服务网格
- Cisco test -- the concept and configuration test of routing
- 软考信息系统项目管理师_整理的十大管理过程的简写帮助记忆背诵---软考高级之信息系统项目管理师054
- 【Swoole系列1】在Swoole的世界中,你将学习到什么?
- Happy number [fast and slow pointer of ring PROBLEMS]
- el-input文本域字数限制,超过显示变红并禁止输入
- “35岁,公司老总,月薪2万送外卖“:时代抛弃你,连声再见都没有
- 死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
- Cloud Vulnerability Global Database
猜你喜欢

微信个人小商店一键开通助手小程序开发

使用3DMax制作一个象棋棋子

Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle

Summary of "performance testing" of software testing, novice will know the knowledge points on the road

Detailed explanation of twenty common software testing methods (the most complete in History)

You probably haven't noticed the very important testing strategy in your work

正则系列之量词(Quantifiers)

Introduction and use of plantuml

Flynk SQL client uses comparison and is familiar with official documents

Sogou wechat app reverse (II) so layer
随机推荐
思科--高可用和高可靠网络考试
MySQL -- deduction of index storage model
Explain the use of locksupport in detail
mysql ---- Oracle中的rownum转换成MySQL
激发新动能 多地发力数字经济
Istio、eBPF 和 RSocket Broker:深入研究服务网格
Demo program implementation of QT version Huarui camera
半监督学习之数据增强
Electron学习(三)之简单交互操作
shell 自定义函数
转载csdn文章操作
Unable to climb hill sort, directly insert sort
Cisco test -- the concept and configuration test of routing
转--原来gdb的底层调试原理这么简单
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
Cutefishos system~
思科考试--冗余网络
Appium automation test foundation - appium installation (I)
OpenVINO 模型性能评估工具—DL Workbench