簡體   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