java Java中断路器的Hystrix配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29624338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:37:23  来源:igfitidea点击:

Hystrix configuration for circuit breaker in Java

javanetflixhystrixcircuit-breaker

提问by Sumit Kumar

I am writing an application and I want to implement circuit breakerpattern. This is the Hystrix Command class I have written:

我正在编写一个应用程序,我想实现断路器模式。这是我写的 Hystrix 命令类:

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

I am not able to understand how to configure the number of threads, threshold time for circuit breaker and number of requests to handle.

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

回答by harperska

Hystrix uses Archaius for configuration management. The Archaius library allows for dynamic reloading of property values at runtime. Documentation on how to configure Archaius is here: https://github.com/Netflix/archaius/wiki/Users-Guide

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

If you want to configure Hystrix in code, you can use the Archaius ConfigurationManager class like this:

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

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

Note that the HystrixCommandKey part of the property name string is actually the name of the circuit breaker you set with the .andCommandKey() method of the Setter. So if you set the command key to be "MyCommand", the property key for timeout would actually be "hystrix.command.MyCommand.execution.isolation.thread.timeoutInMilliseconds"

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

回答by Senthilkumar Gopal

The complete list of configurations and the means are available here: https://github.com/Netflix/Hystrix/wiki/Configuration

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

For your specific questions:

对于您的具体问题:

  1. configure the no. of threads use 'hystrix.threadpool.HystrixThreadPoolKey.coreSize'

  2. threshold time for circuit breaker use 'hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds'

  3. no. of requests to handle. This is a little tricky. But the max. number of concurrent threads is the same as no. of requests to handle.
  1. 配置编号 线程使用' hystrix.threadpool.HystrixThreadPoolKey.coreSize'

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

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

It would be better to read through the Configuration wiki to understand the structure and the usage of each property before attempting to setup.

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

回答by Aamir Quraishi

It is best to set command properties before creating the command. Hystrix documentation specifically states this for some of the command properties. For example:

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

metrics.rollingStats.numBuckets: As of 1.4.12, this property affects the initial metrics creation only, and adjustments made to this property after startup will not take effect.

metrics.rollingStats.numBuckets:从1.4.12开始,这个属性只影响初始metrics的创建,启动后对这个属性的调整不会生效。

In other words, do not initialize this command property from inside the constructor as that is too late. I am using 1.4.3 and at least for this version, I found this to hold for all rolling metrics and circuit breaker properties. I have used ConfigurationManager to set these properties before initializing the command:

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

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

Replace with the command key (which in the question being asked is: "Thread_Pool").

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