@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;

    }

}`

你可能感兴趣的:(@HystrixCommand属性动态修改)