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.
}