当前位置:网站首页>【微服务|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方法
这也验证我们前一篇文章,在注解内对异常处理的两种方案。
边栏推荐
- Simple interactive operation of electron learning (III)
- 思科--高可用和高可靠网络考试
- Preparation of functional test report
- Istio、eBPF 和 RSocket Broker:深入研究服务网格
- Introduction and use of plantuml
- The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
- Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
- 半监督学习之数据增强
- Use and function of spark analyze command map join broadcast join
- Compare the version number [double pointer to intercept the string you want]
猜你喜欢

赵福全:短期解决保供,长期要打造安全、高效有韧性的供应链

Stimulate new kinetic energy and promote digital economy in multiple places

Explain JMM in detail

死锁的处理策略—预防死锁、避免死锁、检测和解除死锁

Reprint CSDN article operation

“信任机器”为发展赋能

正则系列之组和范围(Groups and Ranges)

Redis~02 缓存:更新数据时如何保证MySQL和Redis中的数据一致性?

Demo program implementation of QT version Huarui camera

Flynk SQL client uses comparison and is familiar with official documents
随机推荐
MySQL -- deduction of index storage model
Turn -- bring it and use it: share a gadget for checking memory leaks
Force buckle 710 Random numbers in the blacklist
Cisco -- an external tool for WAN's concept examination
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
"Trust machine" empowers development
Cutefishos system~
Compare the version number [double pointer to intercept the string you want]
Use 3DMAX to make a chess piece
Arlo's thinking after confusion
Jielizhi, production line assembly link [chapter]
想请教一下,证券开户选择哪个证券比较好?手机开户是安全么?
Convergence and disposal suggestions of some Internet exposure surfaces
shell 自定义函数
微服务服务稳定性治理
Use of locust
思科--WAN 的概念考试外部工具
Talk about what parameters ZABBIX monitors
ECMAScript 2022 was officially released. Have you heard about it?