@HystrixCommand属性动态修改

@HystrixCommand的使用对于应用开发来说非常方便,但也有个限制就是像动态修改注解的属性就很麻烦。

因为被该注解标注的方法在执行前是被com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect实时地读取注解元数据的,同时Hystrix内部只提供了Archaius动态配置,但也是有不小的使用限制。

另类解决方式:

`@Aspect

@Component

public class MyHystrixCommandAspect {

public static BooleantimeoutInMilliseconds = Boolean.FALSE;

    @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)")

public void hystrixCommandAnnotationPointcut() {

}

@Around("hystrixCommandAnnotationPointcut()")

public ObjectmethodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint)throws Throwable {

if(timeoutInMilliseconds){

System.out.print("%%%%%%%%%%%%%%%%%%%%%");

            Method method =getMethodFromTarget(joinPoint);

            HystrixCommand hystrixCommand = method.getAnnotation(HystrixCommand.class);

            HystrixProperty[] hystrixProperties = hystrixCommand.commandProperties();

            for (HystrixProperty hystrixProperty : hystrixProperties){

if("execution.isolation.thread.timeoutInMilliseconds".equals(hystrixProperty.name())){

InvocationHandler h = Proxy.getInvocationHandler(hystrixProperty);

                    Field hField = h.getClass().getDeclaredField("memberValues");

                    hField.setAccessible(true);

                    Map memberValues = (Map) hField.get(h);

                    memberValues.put("value", "6000");

                }

}

}

Object obj =  joinPoint.proceed();

        return obj;

    }

}`

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,292评论 19 139
  • 什么是注解(Annotation):Annotation(注解)就是Java提供了一种元程序中的元素关联任何信息和...
    九尾喵的薛定谔阅读 8,426评论 0 2
  • 如果说最近有什么比较火的电视剧,小编只能说出3个:《楚乔传》、《我的前半生》、《军师联盟》。而这里面,竟有一位迷倒...
    文琪来啦阅读 2,629评论 0 0
  • 2018年1月2日 第一天使用简书,浏览了许多主题。也想了好多新的一年想要做的事情。果然,年初就是一个让人对...
    o_1a3b阅读 1,137评论 0 0
  • 文/九品带刀刺猬君 1.A和B相恋结婚多年,从未红过脸,遇到什么问题,两个人都能坐下来,平静而又有商有量的讨论问题...
    南十三阅读 1,664评论 1 1