java - Hystrix 线程池属性

标签 java multithreading concurrency hystrix

在我们的应用程序中,我们使用 Hystrix,因为我们调用了多个外部服务。我们想为我们调用的每个外部服务配置一个线程池——具有特定大小。

假设有三个外部服务,分别称为 S1、S2、S3。此外,我们有 10 个扩展 HystrixCommand 的类,称为 C1 到 C10。

C1 和 C2 调用 S1 并且应该使用相同的线程池,有 15 个线程。在 C1 的构造函数中,我们对 super 进行以下调用:

super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("S1"))
    .andThreadPoolKey(ThreadPools.S1)
    .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(15))
    .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(timeout)));

在一个命令 (C1) 的构造函数中,我们将 S1 的线程池大小指定为 15。ThreadPools 是一个自定义类,其中 final static 属性 S1

定义
S1 = HystrixThreadPoolKey.Factory.asKey("S1");

现在真正的问题是,(1) 为什么线程池核心大小(对于 S1 是 15)是在 HystrixCommand 中而不是中央线程池定义 (这好像不是Hystrix的概念)

假设在 C2 的构造函数中(看起来与上面的代码片段相同)我们要调用 withCoreSize参数不是 15。(2) 将使用哪一个?

(3)有没有办法在一个类中为服务S1、S2、S3定义三个线程池,并在命令类中引用?

Hystrix How to Use指南似乎不包含与此相关的信息。如果有人有时间回答这个问题就太好了。

最佳答案

您可以定义一个名为 config.properties 的属性文件,如下所示:

    hystrix.threadpool.S1.coreSize=15
    hystrix.threadpool.S1.maximumSize=15
    hystrix.threadpool.S1.maxQueueSize=15
    hystrix.threadpool.S1.queueSizeRejectionThreshold=300
    hystrix.threadpool.S1.allowMaximumSizeToDivergeFromCoreSize=true
    hystrix.threadpool.S1.keepAliveTimeMinutes=1

    hystrix.threadpool.S2.coreSize=20
    hystrix.threadpool.S2.maximumSize=20
    hystrix.threadpool.S2.maxQueueSize=20
    hystrix.threadpool.S2.queueSizeRejectionThreshold=300
    hystrix.threadpool.S2.allowMaximumSizeToDivergeFromCoreSize=true
    hystrix.threadpool.S2.keepAliveTimeMinutes=1

...

这里有一个关于 coreSize、maximumSize 和 maxQueueSize 之间区别的很好的解释:

https://github.com/Netflix/Hystrix/issues/1554

关于java - Hystrix 线程池属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307271/

相关文章:

java - Axis 1.4 log4j-1.2.8.jar 与 XPage 不兼容?

c# - 为什么 Interlocked.Add() 方法必须返回一个值?

java - 如何在不卡住 Java Swing 中的 GUI 的情况下与进程随机通信?

c# - 在访问变量之前检查锁定的静态分析工具

java - Android并发: UI thread edit a variable and other thread read the same variable

ios - NSRunLoop的runMode :beforeDate: - the correct approach for setting the "beforeDate"

java - 开发 Java Swing 应用程序,该应用程序将使用数据库存储数据并将其显示在 JFrame 上

java - JTextArea 的前景色没有改变

java - JOOQ 与 hibernate

python - Asyncio 与另一个协程同时运行 Dash (Flask) 服务器