繁体   English   中英

用于Java中断路器的Hystrix配置

[英]Hystrix configuration for circuit breaker in Java

我正在写一个应用程序,我想实现断路器模式。 这是我写的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使用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"

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

针对您的具体问题:

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

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

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

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

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

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

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

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

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

暂无
暂无

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

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