当前位置:网站首页>【微服务|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方法
这也验证我们前一篇文章,在注解内对异常处理的两种方案。
边栏推荐
- Favorite transaction code management tool in SAP GUI
- Armbain系统根分区空间不足处理
- Electron学习(三)之简单交互操作
- Jerry's question about long press boot detection [chapter]
- [MySQL] basic use of explain and the function of each column
- window10安装wsl(一)(WslRegisterDistribution ERROR)
- plain framework的实际应用和扩展
- [JUC learning road day 8] condition
- Use of locust
- Genicam gentl standard ver1.5 (4) Chapter 5 acquisition engine
猜你喜欢
AAAI22 | 结构标记和交互建模:用于图分类的“SLIM”网络
Digital currency: far-reaching innovation
window10安装wsl(一)(WslRegisterDistribution ERROR)
Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
[机缘参悟-35]:鬼谷子-飞箝篇-远程连接、远程控制与远程测试之术
Tcpdump command usage details
[target tracking] | single target tracking indicator
Map container
Explain JMM in detail
数字化转型道阻且长,如何迈好关键的第一步
随机推荐
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
每日刷题记录 (十)
Cisco -- an external tool for WAN's concept examination
Compare the version number [double pointer to intercept the string you want]
使用3DMax制作一个象棋棋子
Réimpression de l'article csdn
Turn -- the underlying debugging principle of GDB is so simple
locust的使用
Digital currency: far-reaching innovation
使用 EMQX Cloud 实现物联网设备一机一密验证
Little red book scheme jumps to the specified page
Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
OpenVINO 模型性能评估工具—DL Workbench
Design of ESP automatic download circuit
Fiori applications are shared through the enhancement of adaptation project
半监督学习之数据增强
Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
Use 3DMAX to make a chess piece
Redis~02 缓存:更新数据时如何保证MySQL和Redis中的数据一致性?