当前位置:网站首页>【微服务|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方法
这也验证我们前一篇文章,在注解内对异常处理的两种方案。
边栏推荐
猜你喜欢

转载csdn文章操作

Istio、eBPF 和 RSocket Broker:深入研究服务网格

工作中非常重要的测试策略,你大概没注意过吧

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

CKS CKA CKAD 将终端更改为远程桌面

Turn -- bring it and use it: share a gadget for checking memory leaks

Multi picture alert ~ comparison of Huawei ECs and Alibaba cloud ECS

转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
![[MySQL] basic use of explain and the function of each column](/img/d6/64f65ba21f5cda2c409477705f6a79.png)
[MySQL] basic use of explain and the function of each column

Design of ESP automatic download circuit
随机推荐
Copy ‘XXXX‘ to effectively final temp variable
【Swoole系列1】在Swoole的世界中,你将学习到什么?
力扣 710. 黑名单中的随机数
Electron学习(三)之简单交互操作
Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
Explain the volatile keyword
ESP自动下载电路设计
若干互联网暴露面的收敛及处置建议
思科考试--路由的概念和配置考试
SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
Detailed explanation of twenty common software testing methods (the most complete in History)
数字化转型道阻且长,如何迈好关键的第一步
死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
下班前几分钟,我弄清了v-model与.sync的区别
Digital currency: far-reaching innovation
激发新动能 多地发力数字经济
Daily question brushing record (10)
[target tracking] | single target tracking indicator
认识线程
软件测试之「 性能测试」总结,新手上路必会知识点