我正在将Hystrix集成到一个应用程序中。该应用程序已经投入生产,我们将在沙盒中测试hystrix集成工作,然后再将其推向生产阶段。我的问题是,有没有办法使用一些配置设置打开/关闭hystrix功能?
没有单一的设置。您需要设置多个参数才能禁用Hystrix。
有关配置选项,请参阅https://github.com/Netflix/Hystrix/wiki/Configuration:
hystrix.command.default.execution.isolation.strategy=SEMAPHORE
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=100000 # basically 'unlimited'
hystrix.command.default.execution.timeout.enabled=false
hystrix.command.default.circuitBreaker.enabled=false
hystrix.command.default.fallback.enabled=false
请仔细检查您的Hystrix版本以获取可用参数。
这就是你所需要的:
# Disable Circuit Breaker (Hystrix)
spring:
cloud:
circuit:
breaker:
enabled: false
hystrix:
command:
default:
circuitBreaker:
enabled: false
正如ahus1所说,没有一种方法可以完全禁用Hystrix。要在我们的应用程序中禁用它,我们认为将HystrixCommand放在包装类中是最干净和最安全的,并且该包装类只暴露了我们使用的HystrixCommand的部分(在我们的例子中,是execute()方法)。在构造包装类时,我们传递一个包含我们想要执行的代码的Callable,如果Hystrix被禁用(根据我们自己的配置值),我们只需调用Callable而不创建HystrixCommand。这样可以避免执行任何Hystrix代码,并且更容易说Hystrix在禁用时根本不会影响我们的应用程序。
如果您的项目是spring Managed,您可以在applicationContext.xml中注释hystrixAspect的bean定义注释以下行
bean id =“hystrixAspect”class =“com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect”/>
这将从您的项目中删除Hystrix。
有几种方法可以达到这个目的 -
hystrix.command.{group-key}.circuitBreaker.forceClosed=false
#2的Java代码
@Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)")
public void hystrixCommandAnnotationPointcut() {
}
@Around("hystrixCommandAnnotationPointcut()")
public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable {
Object result = null;
Method method = AopUtils.getMethodFromTarget(joinPoint);
if ((System.getProperty(enable.hystrix).equals("true")) {
result = joinPoint.proceed();
} else {
result = method.invoke(joinPoint.getTarget(), joinPoint.getArgs());
}
return result;
}
我遇到了这种情况,我想用一个属性完全关闭Hystrix(我们使用IBM uDeploy来管理动态属性)。我们正在使用建立在Hystrix之上的javanica库
@Configuration
public class HystrixConfiguration{
@Bean(name = "hystrixCommandAspect")
@Conditional(HystrixEnableCondition.class)
public HystrixCommandAspect hystrixCommandAspect(){
return new HystrixCommandAspect()}
}
2. And the conditional class would be enabled based on a system property.
public class HystrixEnableCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata){
return
"YES".equalsIgnoreCase(
context.getEnvironment().getProperty("circuitBreaker.enabled")) ||
"YES".equalsIgnoreCase(
System.getProperty("circuitBreaker.enabled"));
}
}