1

HystrixCommands我对工作原理的理解是CommandProperties可以临时更改。

但是,当我尝试使用该IsolationSemaphoreMaxConcurrentRequests 属性执行此操作时,我对命令配置的更改似乎没有被采纳。

我是否误解了为已经在同一键下指定了配置的 Hystrix 命令提供额外配置的能力?

在 Hystrix 的上下文中,我是否缺少特定于隔离/信号量的东西?

下面是我的问题的一个例子。代码开始配置一个允许最多 1 个信号量的命令,然后将该数量增加到 2 个信号量。此时,它尝试使用两个信号量,但由于无法获取第二个信号量而失败(我猜是因为允许的最大值仍为 1?)。

HystrixCommandKey hystrixCommandKey = HystrixCommandKey.Factory.asKey("Command1");

// Note that we're initially setting the max concurrent requests to 1
HystrixCommandProperties.Setter maxConcurrentRequests1 = HystrixCommandProperties.Setter()
        .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)
        .withExecutionTimeoutEnabled(false)
        .withExecutionIsolationSemaphoreMaxConcurrentRequests(1);

final HystrixCommand.Setter hystrixCommandSetter = HystrixCommand.Setter
        .withGroupKey(HystrixCommandGroupKey.Factory.asKey("Group"))
        .andCommandKey(hystrixCommandKey)
        .andCommandPropertiesDefaults(maxConcurrentRequests1);


class SleepOneSecond implements Callable<Boolean> {
    @Override
    public Boolean call() throws Exception {
        return (new HystrixCommand<Boolean>(hystrixCommandSetter) {
            @Override
            protected Boolean run() throws Exception {
                Thread.sleep(2_000);
                return true;
            }
        }).execute();
    }
}

ExecutorService threadPool = Executors.newFixedThreadPool(2);
threadPool.submit(new SleepOneSecond()).get();

// Bumping up the command settings to 2
hystrixCommandSetter.andCommandPropertiesDefaults(
        maxConcurrentRequests1.withExecutionIsolationSemaphoreMaxConcurrentRequests(2));

// We should be allowed to have two concurrent commands now because we've bumped our semaphore cound up to two for
// these commands...
for (Future<Boolean> result :  threadPool.invokeAll(ImmutableList.of(new SleepOneSecond(), new SleepOneSecond()))) {
    result.get();
    // Instead on the second result we end up with a:
    // com.netflix.hystrix.exception.HystrixRuntimeException: Command1 could not acquire a semaphore
    //   for execution and no fallback available.
}
4

1 回答 1

1

从 Github 问题#1048复制的答案:

这里的问题是属性如何应用的粒度。每个命令键,该键有一个属性实例。该键的所有命令都获取该配置。这样做是为了减轻操作负担。如果有各种具有不同配置(比如超时值)的命令实例,这将是一个非常难以操作、调整和理解的系统。

但是,可调整这些值也很重要。Hystrix 提供 Archaius 集成,这将允许您在运行时修改信号量计数(或任何其他配置值)。执行此操作时,您正在修改单例属性实例,并且所有命令都会选择该值。有关此集成如何工作的更多详细信息,请参阅https://github.com/Netflix/Hystrix/wiki/Configuration

于 2016-01-13T18:37:11.560 回答