【问题标题】:Turn off Hystrix functionality关闭 Hystrix 功能
【发布时间】:2016-07-05 14:16:16
【问题描述】:

我正在将 Hystrix 集成到应用程序中。该应用程序已经投入生产,我们将在沙盒中测试 hystrix 集成工作,然后再将其投入生产。 我的问题是有没有办法使用一些配置设置来打开/关闭 hystrix 功能?

【问题讨论】:

  • 也许将 circuitBreaker.forceClosed 设置为 true 或将 circuitBreaker.enabled 设置为 false 即可。

标签: hystrix


【解决方案1】:

对此没有单一的设置。您需要设置多个参数来禁用 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 版本以获取可用参数。

【讨论】:

  • 我很快就会对此进行测试。
  • 顺便说一句,为什么将设置策略更改为 SEMAPHORE 很重要?
  • 使用信号量可以避免为包装好的 Hystrix 命令创建新线程。由于您没有在此禁用设置中限制并发请求,否则您最终会得到很多线程。
【解决方案2】:

正如 ahus1 所说,没有一种方法可以完全禁用 Hystrix。为了在我们的应用程序中禁用它,我们认为将 HystrixCommand 放在包装器类中是最干净和最安全的,并且该包装器类只暴露了我们使用的 HystrixCommand 的部分(在我们的例子中,execute() 方法)。在构建包装类时,我们向它传递一个包含我们想要执行的代码的 Callable,如果 Hystrix 被禁用(根据我们自己的配置值),我们只需调用该 Callable 而无需创建 HystrixCommand。这避免了执行任何 Hystrix 代码,并且更容易说 Hystrix 在禁用时不会影响我们的应用程序根本

【讨论】:

  • 这是一个非常简洁的解决方案。我一直在寻找相同问题的想法并将使用它。这是我们第一次引入 Hystrix,能够轻松关闭/打开它会很有用。
【解决方案3】:

如果您的项目是 spring 托管的,您可以在 applicationContext.xml 中注释 hystrixAspect 的 bean 定义 注释以下行

bean id="hystrixAspect"class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect"/>

这将从您的项目中删除 Hystrix。

【讨论】:

    【解决方案4】:

    这就是你所需要的:

    # Disable Circuit Breaker (Hystrix)
    
    spring:
      cloud:
        circuit:
          breaker:
            enabled: false
    
    hystrix:
      command:
        default:
          circuitBreaker:
            enabled: false
    

    【讨论】:

    • 谢谢,这是最正确和最简单的解决方案,它对我很有效。
    【解决方案5】:

    有几种方法可以实现这一点-

    1. 为您的每个组(包括默认组)执行此操作。虽然这不会禁用 hystrix(它只会一直保持电路关闭)但你会得到相同的结果-

      hystrix.command.{group-key}.circuitBreaker.forceClosed=false

    2. 如果您使用的是 java,您可以在 @HystrixCommand 注释上创建一个环绕建议,并根据标志绕过 hystrix 执行。

    #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;
    }
    

    【讨论】:

    • 第二种方法的唯一问题是它会触发实际 HystrixCommandAspect 中的切面,因为切入点对于您的 Aspect 和 HystrixCommandAspect 都是相同的。
    • @SherinSyriac 是的。我的建议只适用于使用实际 HystrixCommand 注释的方法。这样我就可以控制 hystrix 是否启动。我正在编辑我的答案以包含我的切入点,以便它变得更加清晰。
    • 你在声明 吗?然后我认为 HystrixCommandAspect 会被触发不止一次。这是因为两个切面都有相同的切入点。另外,如果切面和 HystrixCommandAspect 使用相同的切入点,如何确保它们触发的顺序?
    • 是的,我已经创建了一个你提到的 bean,但我没有指定任何建议顺序。我想我的自定义建议总是首先执行是偶然的。在阅读了您的评论后,我还尝试了@Order,以查看是否可以让我的自定义建议在 HystricCommand 注释上定义的原始建议之后运行,但无法使其正常工作。我的建议总是先执行。知道为什么会这样吗?
    【解决方案6】:

    我遇到了这种情况,我想使用单个属性完全关闭 Hystrix(我们使用 IBM uDeploy 来管理动态属性)。我们使用的是基于 Hystrix 构建的 javanica 库

    1. 创建一个配置类来创建 HystrixCommandAspect

    
    
    

    @Configuration public class HystrixConfiguration{

    @Bean(name = "hystrixCommandAspect")
    @Conditional(HystrixEnableCondition.class)
    public HystrixCommandAspect hystrixCommandAspect(){ 
          return new HystrixCommandAspect()}
    }
    

    2. 条件类将基于系统属性启用。

    
    
     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"));
      }
    
     }
    

    【讨论】:

      【解决方案7】:

      设置 hystrix.command.default.execution.isolation.strategy=SEMAPHORE 足够的。 此外,您可能或应该禁用超时线程 hystrix.command.default.execution.timeout.enabled=false

      【讨论】:

        猜你喜欢
        • 2017-01-02
        • 1970-01-01
        • 2015-05-04
        • 1970-01-01
        • 2021-12-18
        • 1970-01-01
        • 2013-07-14
        • 2012-11-05
        • 1970-01-01
        相关资源
        最近更新 更多