简体   繁体   English

用于Java中断路器的Hystrix配置

[英]Hystrix configuration for circuit breaker in Java

I am writing an application and I want to implement circuit breaker pattern. 我正在写一个应用程序,我想实现断路器模式。 This is the Hystrix Command class I have written: 这是我写的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;
    }
}

I am not able to understand how to configure the number of threads, threshold time for circuit breaker and number of requests to handle. 我无法理解如何配置线程数,断路器的阈值时间和要处理的请求数。

Hystrix uses Archaius for configuration management. Hystrix使用Archaius进行配置管理。 The Archaius library allows for dynamic reloading of property values at runtime. Archaius库允许在运行时动态重新加载属性值。 Documentation on how to configure Archaius is here: https://github.com/Netflix/archaius/wiki/Users-Guide 有关如何配置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. 请注意,属性名称字符串的HystrixCommandKey部分实际上是使用Setter的.andCommandKey()方法设置的断路器的名称。 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" 因此,如果将命令键设置为“MyCommand”,则超时的属性键实际上是"hystrix.command.MyCommand.execution.isolation.thread.timeoutInMilliseconds"

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 ' 线程使用' hystrix.threadpool.HystrixThreadPoolKey.coreSize '

  2. threshold time for circuit breaker use 'hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds' 断路器的阈值时间使用'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. 并发线程数与no相同。 of requests to handle. 请求处理。

It would be better to read through the Configuration wiki to understand the structure and the usage of each property before attempting to setup. 在尝试设置之前,最好通过Configuration wiki阅读以了解每个属性的结构和用法。

It is best to set command properties before creating the command. 最好在创建命令之前设置命令属性。 Hystrix documentation specifically states this for some of the command properties. Hystrix文档特别针对某些命令属性说明了这一点。 For example: 例如:

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开始,此属性仅影响初始度量标准创建,启动后对此属性所做的调整不会生效。

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. 我使用的是1.4.3,至少对于这个版本,我发现这适用于所有滚动指标和断路器属性。 I have used ConfigurationManager to set these properties before initializing the command: 我在初始化命令之前使用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”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM