当前位置: 首页 > news >正文

Sentinel源码—7.参数限流和注解的实现二

大纲

1.参数限流的原理和源码

2.@SentinelResource注解的使用和实现

2.@SentinelResource注解的使用和实现

(1)@SentinelResource注解的使用

(2)@SentinelResource注解和实现

(1)@SentinelResource注解的使用

一.引入Sentinel Spring Boot Starter依赖

<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel</artifactId><version>2.2.1.RELEASE</version>
</dependency>

二.为方法添加@SentinelResource注解

下面的代码为sayHello()方法添加了@SentinelResource注解,并指定了资源名称为sayHello以及熔断降级时的回调方法fallback()。这样在请求sayHello()方法后,就可以在Sentinel Dashboard上看到此资源,然后就可以针对此资源进行一系列的规则配置了。

@Service
public class MyService {@SentinelResource(value = "sayHello", fallback = "fallback")public String sayHello(String name) {return "Hello, " + name;}public String fallback(String name, Throwable throwable) {return "Fallback: " + name + ", reason: " + throwable.getMessage();}
}

(2)@SentinelResource注解和实现

利用Spring AOP拦截@SentinelResource注解,最后调用SphU.entry()方法来进行处理。

//Aspect for methods with {@link SentinelResource} annotation.
@Aspect
public class SentinelResourceAspect extends AbstractSentinelAspectSupport {//SentinelResource注解@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注解,有了这个注解,就可以获取到注解的各种属性值了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对象,通过SphU.entry(resourceName)将当前方法纳入Sentinel的保护体系//如果当前资源的调用未触发任何Sentinel规则,则正常执行被拦截的方法,否则将执行对应的限流、熔断降级等处理逻辑Entry entry = null;try {entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs());return pjp.proceed();} 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());}}  }
}//Some common functions for Sentinel annotation aspect.
public abstract class AbstractSentinelAspectSupport {...protected Object handleBlockException(ProceedingJoinPoint pjp, SentinelResource annotation, BlockException ex) throws Throwable {//Execute block handler if configured.Method blockHandlerMethod = extractBlockHandlerMethod(pjp, annotation.blockHandler(), annotation.blockHandlerClass());if (blockHandlerMethod != null) {Object[] originArgs = pjp.getArgs();//Construct args.Object[] args = Arrays.copyOf(originArgs, originArgs.length + 1);args[args.length - 1] = ex;return invoke(pjp, blockHandlerMethod, args);}//If no block handler is present, then go to fallback.return handleFallback(pjp, annotation, ex);}private Object invoke(ProceedingJoinPoint pjp, Method method, Object[] args) throws Throwable {try {if (!method.isAccessible()) {makeAccessible(method);}if (isStatic(method)) {return method.invoke(null, args);}return method.invoke(pjp.getTarget(), args);} catch (InvocationTargetException e) {//throw the actual exceptionthrow e.getTargetException();}}...
}

相关文章:

  • 用一个大型语言模型(LLM)实现视觉与语言的融合: Liquid_V1_7B
  • vscode 打开新页签
  • 你学会了些什么221016?--请求如何到达后端并得到响应
  • 4.21总结
  • Kafka 详细解读
  • 探索大语言模型(LLM):ReAct、Function Calling与MCP——执行流程、优劣对比及应用场景
  • 使用Cloudflare加速网站的具体操作步骤
  • 高级java每日一道面试题-2025年4月19日-微服务篇[Nacos篇]-Nacos未来的发展方向和规划有哪些?
  • 图像预处理-图像轮廓特征查找
  • Web 前端打包工具与构建系统的进阶指南
  • 07前端项目----面包屑
  • JVM有什么调优参数?
  • Java秒杀功能-案例
  • 丝杆升降机蜗轮蜗杆加工工艺深度解析:从选材到制造的全流程技术要点​
  • 软件工程中的维护类型
  • 模型的RAG
  • 运营商二要素认证API接口有哪些发展前景?
  • 基于超启发鲸鱼优化算法的混合神经网络多输入单输出回归预测模型 HHWOA-CNN-LSTM-Attention
  • 【项目日记(三)】
  • pytest-项目结构
  • 人民日报刊文:美国滥施关税及中国反制措施的法理视角透析
  • 我国自主研制的重大航空装备AG600性能怎样?专家解读
  • 人民日报和音:书写周边命运共同体建设新篇章
  • 青创上海—2025浦东徒步行活动举行,“青年草坪创新创业湃对”正式亮相
  • 为护航企业“出海”,“无问西东·中外商会”海上沙龙举行
  • 提升青春期+亲子含量:社区商业综合体这样用好“二次元”