【问题标题】:Hystrix configuration for circuit breaker in JavaJava中断路器的Hystrix配置
【发布时间】:2015-06-19 21:15:32
【问题描述】:

我正在编写一个应用程序,我想实现circuit breaker 模式。这是我写的 Hystrix Command 类:

public class HystrixCommandNextGen extends HystrixCommand<ScriptContext> {

    private ScriptContext scriptctx;
    private ScriptFactory scriptFactory;
    private ScriptContext responseContext = null;

    private Logger logger = LoggerFactory.getLogger(HystrixCommandNextGen.class);

    public HystrixCommandNextGen(ScriptContext scriptctx, ScriptFactory scriptFactory) {
        super(
            Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Thread_Pool"))
            .andCommandKey(HystrixCommandKey.Factory.asKey(scriptctx.getExecutionData(ExecutionParam.SCRIPTNAME)))
        );
        this.scriptctx = scriptctx;
        this.scriptFactory = scriptFactory;

        HystrixCommandProperties.Setter().withCircuitBreakerEnabled(true);
        HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(150);
    }

    @Override
    protected ScriptContext run() throws Exception {
        scriptFactory.execute(scriptctx);
        return scriptctx;
    }

    @Override
    protected ScriptContext getFallback() {
        logger.error("FI is not responding: Error occurred in the execution of " + getClass().getSimpleName());
        return scriptctx;
    }
}

我无法理解如何配置线程数、断路器的阈值时间和要处理的请求数。

【问题讨论】:

  • 除非我误解了 Hystrix 的代码,否则你必须调用 HystrixCommandProperties.Setter() 的那两个调用没有做任何事情,只是创建了一个 Setter 类型的新对象,并没有实际设置任何全局属性跨度>

标签: java netflix hystrix circuit-breaker


【解决方案1】:

完整的配置列表和方法可在此处获得: https://github.com/Netflix/Hystrix/wiki/Configuration

对于您的具体问题:

  1. 配置编号。线程 使用'hystrix.threadpool.HystrixThreadPoolKey.coreSize'

  2. 断路器的阈值时间 使用'hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds'

  3. 没有。要处理的请求数。 这有点棘手。但最大。并发线程数与否相同。要处理的请求数。

在尝试设置之前,最好通读配置 wiki 以了解每个属性的结构和用法。

【讨论】:

    【解决方案2】:

    Hystrix 使用 Archaius 进行配置管理。 Archaius 库允许在运行时动态重新加载属性值。关于如何配置 Archaius 的文档在这里:https://github.com/Netflix/archaius/wiki/Users-Guide

    如果你想在代码中配置 Hystrix,你可以像这样使用 Archaius ConfigurationManager 类:

    ConfigurationManager.getConfigInstance().setProperty(
      "hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds", 
      500);
    

    注意,属性名字符串的HystrixCommandKey部分其实就是你用Setter的.andCommandKey()方法设置的断路器的名字。因此,如果您将命令键设置为“MyCommand”,则超时的属性键实际上是"hystrix.command.MyCommand.execution.isolation.thread.timeoutInMilliseconds"

    【讨论】:

      【解决方案3】:

      最好在创建命令之前设置命令属性。 Hystrix 文档专门针对某些命令属性说明了这一点。例如:

      metrics.rollingStats.numBuckets: 从 1.4.12 开始,该属性仅影响初始指标创建,启动后对该属性所做的调整将不会生效。

      换句话说,不要从构造函数内部初始化这个命令属性,因为太晚了。我使用的是 1.4.3,至少对于这个版本,我发现它适用于所有滚动指标和断路器属性。在初始化命令之前,我已经使用 ConfigurationManager 设置了这些属性:

      ConfigurationManager.getConfigInstance().setProperty("hystrix.command.<HystrixCommandKey>.circuitBreaker.requestVolumeThreshold, 30);
      

      替换为命令键(在被问到的问题中是:“Thread_Pool”)。

      【讨论】:

        猜你喜欢
        • 2016-12-11
        • 2018-07-09
        • 2020-10-29
        • 2016-01-11
        • 2016-08-06
        • 2018-10-05
        • 2019-06-27
        • 2015-12-06
        • 2016-03-15
        相关资源
        最近更新 更多