当前位置:网站首页>[micro service sentinel] sentinelresourceaspect details
[micro service sentinel] sentinelresourceaspect details
2022-07-01 23:12:00 【Bulst】
List of articles
AOP
As we mentioned above @SentinelResource annotation , We all know that annotation is just a mark , Then the real realization of current limiting logic is AOP 了 .
AOP What business problems will be solved in the actual development ?
Transaction related
Used to rollback thingsPerformance monitoring
Record the call time before and after the method call , Method execution too long or timeout alarm .The caching proxy
Cache the return value of a method , The next time you execute this method , Get it directly from the cache .Software cracking
Use AOP Modify the judgment logic of the verification class of the software .Log
Record system logs before and after method execution .Workflow system
Workflow system needs to mix business code and process engine code to execute , Then we can use AOP Separate it , And dynamically connect services .Authority verification
Verify that you have permission to execute the current method before executing the method , If not, throw an exception without permission , Captured by business code .
Next , Let's take a look at its source code com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect
Source code
@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());
}
}
}
}
Source code analysis
Let's analyze this code :
- Use aspect Of around Intercept , Interception is marked with SentinelResource Annotations
- Call before you enter the method SphU.entry(resourceName, entryType), Call after completion entry.exit();
- Call... In case of exception handleBlockException Method
This also verifies our previous article , Two solutions for exception handling in annotations .
边栏推荐
- 【嵌入式系统课设】单个按键控制LED灯
- 想请教股票开户要认识谁?在线开户是安全么?
- 思科--WAN 的概念考试外部工具
- MySQL -- deduction of index storage model
- Cloud Vulnerability Global Database
- Jerry's burning of upper version materials requires [chapter]
- Using emqx cloud to realize one machine one secret verification of IOT devices
- Jielizhi, production line assembly link [chapter]
- Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
- locust的使用
猜你喜欢

window安装wsl(二)

Groups and ranges of regular series

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

Wechat personal small store one click opening assistant applet development

AAAI22 | 结构标记和交互建模:用于图分类的“SLIM”网络

El input text field word limit, beyond which the display turns red and input is prohibited

Explain ThreadLocal in detail

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received

建模和影视后期有什么关联?

"35 years old, the boss of the company, with a monthly salary of 20000, give away takeout": the times abandoned you, not even saying goodbye
随机推荐
Jerry's records are powered by Vbat with a power supply voltage of 4.2V [chapter]
The digital summit is popular, and city chain technology has triggered a new round of business transformation
Tcpdump command usage details
Where can the courses purchased by CSDN be accessed
[MySQL] index creation, viewing and deletion
Compare the version number [double pointer to intercept the string you want]
【嵌入式系统课设】单个按键控制LED灯
Explain JMM in detail
mysql binlog的清理
rviz打开后如何显示实时2D地图
ESP自动下载电路设计
You probably haven't noticed the very important testing strategy in your work
Quantifiers of regular series
Simple interactive operation of electron learning (III)
tcpdump命令使用详解
微服务服务稳定性治理
Turn -- the underlying debugging principle of GDB is so simple
微信个人小商店一键开通助手小程序开发
Advanced skills of testers: a guide to the application of unit test reports
Some thoughts on game performance optimization