当前位置:网站首页>[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 .
边栏推荐
- URL 介绍
- 认识线程
- MySQL -- convert rownum in Oracle to MySQL
- Map container
- Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
- 第三方验收测试有什么好处?专业第三方软件测试机构推荐
- window安装wsl(二)
- AirServer最新Win64位个人版投屏软件
- You probably haven't noticed the very important testing strategy in your work
- Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
猜你喜欢
Istio, ebpf and rsocket Broker: in depth study of service grid
El input text field word limit, beyond which the display turns red and input is prohibited
Stimulate new kinetic energy and promote digital economy in multiple places
AirServer最新Win64位个人版投屏软件
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
Jielizhi, production line assembly link [chapter]
"Trust machine" empowers development
CKS CKA CKAD 将终端更改为远程桌面
Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
随机推荐
Jerry's burning of upper version materials requires [chapter]
CKS CKA CKAD 将终端更改为远程桌面
window安装wsl(二)
Summary of "performance testing" of software testing, novice will know the knowledge points on the road
Turn -- the underlying debugging principle of GDB is so simple
Hide the creation and use of users
工作中非常重要的测试策略,你大概没注意过吧
Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
2022年危险化学品经营单位安全管理人员考试题及在线模拟考试
有些能力,是工作中学不来的,看看这篇超过90%同行
Multiple smart pointers
Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
Tcpdump command usage details
mysql binlog的清理
Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
[MySQL] database optimization method
会声会影2022智能、快速、简单的视频剪辑软件
Stimulate new kinetic energy and promote digital economy in multiple places
What class loading mechanisms does the JVM have?
Redis~02 cache: how to ensure data consistency in MySQL and redis when updating data?