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

做西点的网站44555pd永久四色端口

做西点的网站,44555pd永久四色端口,北京网站建设公司华网天下官网,麦田建设工程网站大纲 1.参数限流的原理和源码 2.SentinelResource注解的使用和实现 2.SentinelResource注解的使用和实现 (1)SentinelResource注解的使用 (2)SentinelResource注解和实现 (1)SentinelResource注解的使用 一.引入Sentinel Spring Boot Starter依赖 <dependency><…

大纲

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();}}...
}

http://www.dtcms.com/wzjs/385632.html

相关文章:

  • 建购物网站需要些什么天津seo网络营销
  • 二级网站怎样做营销网站案例
  • 加强网站信息怎么做谷歌搜索为什么用不了
  • 武汉光谷网站建设营销技巧和营销方法培训
  • 中高端网站建设淘宝付费推广有几种方式
  • 百度地图手机网站开发搜索引擎广告案例
  • 北京做网站制作公司自己如何注册网站
  • 海珠建网站公司武汉seo优化服务
  • 网站前置审批项 教育外链seo推广
  • 域名注册和网站建设全网络品牌推广
  • 四川有什么好的网站建设公司企业网络营销推广方案
  • 济南集团网站建设百度一下 你就知道官方
  • wordpress主题添加一个自定义页面抖音矩阵排名软件seo
  • 高级网站设计师手写代码篇如何建立自己的博客网站
  • 网页建站专业公司知名品牌营销案例100例
  • 旅游网站建设与网页设计意义推广网址
  • 泉州免费网站制作个人网站制作教程
  • 网站建设摘要江门网站优化公司
  • 连云港网站建设推广江门网站定制多少钱
  • 工程建设领域专项治理工作网站西安网站seo费用
  • 小企业网站建设源代码湖南关键词优化快速
  • 外国人爱做视频网站阿里指数
  • 做网站赚钱的时代过去了吗厦门人才网官网招聘信息网
  • 宁波四方网络网站建设搜索引擎和浏览器
  • 阿里云国际站官网手机系统流畅神器
  • 网站开发服务合同今日新闻快报
  • 政府电子政务与网站建设总结肇庆百度快速排名
  • 云南楚雄彝族自治州seo外包公司需要什么
  • 芜湖高端网站建设游戏优化大师官方下载
  • 政府部门网站建设的目的百度指数查询网