当前位置:网站首页>【微服务|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方法
这也验证我们前一篇文章,在注解内对异常处理的两种方案。
边栏推荐
- shell 流程控制
- 【嵌入式系统课设】单个按键控制LED灯
- Simple interactive operation of electron learning (III)
- Cloud Vulnerability Global Database
- Using emqx cloud to realize one machine one secret verification of IOT devices
- Jerry's burning of upper version materials requires [chapter]
- Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
- plain framework的实际应用和扩展
- 数字化转型道阻且长,如何迈好关键的第一步
- URL 介绍
猜你喜欢
数字货币:影响深远的创新
Flynk SQL client uses comparison and is familiar with official documents
Sogou wechat app reverse (II) so layer
AAAI22 | 结构标记和交互建模:用于图分类的“SLIM”网络
[MySQL] basic use of explain and the function of each column
使用 EMQX Cloud 实现物联网设备一机一密验证
Groups and ranges of regular series
Tcpdump command usage details
Compare the version number [double pointer to intercept the string you want]
Jerry's records are powered by Vbat with a power supply voltage of 4.2V [chapter]
随机推荐
shell 流程控制
[机缘参悟-35]:鬼谷子-飞箝篇-远程连接、远程控制与远程测试之术
认识线程
[kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party
Congratulations on the release of friends' new book (send welfare)
力扣 710. 黑名单中的随机数
Tcpdump command usage details
Flink SQL command line connection yarn
Jielizhi, production line assembly link [chapter]
SAP intelligent robot process automation (IRPA) solution sharing
数字货币:影响深远的创新
Summary of "performance testing" of software testing, novice will know the knowledge points on the road
[JUC learning road day 8] condition
Single step debugging analysis of rxjs observable of operator
Advanced skills of testers: a guide to the application of unit test reports
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
Pytorch nn. functional. Simple understanding and usage of unfold()
常见的二十种软件测试方法详解(史上最全)
转载csdn文章操作
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向