当前位置:网站首页>[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 .
边栏推荐
- 想请教一下,证券开户选择哪个证券比较好?手机开户是安全么?
- Advanced skills of testers: a guide to the application of unit test reports
- Tcpdump command usage details
- Cisco test -- the concept and configuration test of routing
- 测试人进阶技能:单元测试报告应用指南
- leetcode - 287. Find duplicates
- 攻防演习防御体系构建之第三篇之建立实战化的安全体系
- 通过Go语言创建CA与签发证书
- Understanding threads
- vim给目录加颜色
猜你喜欢

从第三次技术革命看企业应用三大开发趋势

Cisco exam -- redundant network

Using emqx cloud to realize one machine one secret verification of IOT devices

Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry

2022年起重机司机(限桥式起重机)考试试题及模拟考试

【小程序】通过scroll-view组件实现左右【滑动】列表

tcpdump命令使用详解

Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification

Copy ‘XXXX‘ to effectively final temp variable

测试人进阶技能:单元测试报告应用指南
随机推荐
日本购物网站的网络乞丐功能
[MySQL] basic use of explain and the function of each column
微信个人小商店一键开通助手小程序开发
半监督学习之数据增强
使用3DMax制作一个象棋棋子
Cisco test -- the concept and configuration test of routing
rviz打开后如何显示实时2D地图
Genicam gentl standard ver1.5 (4) Chapter 5 acquisition engine
Istio, ebpf and rsocket Broker: in depth study of service grid
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
vim给目录加颜色
Mysql 5.7 实现 rank 排名
OpenVINO 模型性能评估工具—DL Workbench
Explain the volatile keyword
MySQL -- convert rownum in Oracle to MySQL
用JpaTransactionManager操作数据库事务
想请教股票开户要认识谁?在线开户是安全么?
Unable to climb hill sort, directly insert sort
测试人进阶技能:单元测试报告应用指南
Business visualization - make your flowchart'run'up