我第一次使用hystrix异步对第三方库进行set
调用,我真的不在乎数据是否成功发布。
public class SetCacheDataCommand extends BaseHystrixCommand {
public SetCacheDataCommand(CacheClient cacheClient, String cacheKey, Entry cacheValue, int timeToLive){
super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GROUP_NAME))
.andCommandKey(HystrixCommandKey.Factory.asKey(COMMAND_NAME))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(DEFAULT_HYSTRIX_TIMEOUT_MILLISECONDS)));
this.cacheClient = cacheClient;
this.cacheKey = cacheKey;
this.cacheValue = cacheValue;
this.timeToLive = timeToLive;
}
@Override
protected Object run() throws Exception {
cacheClient.set(cacheKey, cacheValue, timeToLive);
return null;
}
Set
是一种无效方法。这是命令调用 -
....Doing something here
new SetCacheDataCommand(cacheClient, cacheKey, cacheValue, 10000).queue();
....Doing something here
我希望上面的代码能够处理这个异步调用。但是我从hystrix documentation中读到了以下内容。
注意:即使调用者,也会在HystrixCommand.queue()上触发超时 永远不会在生成的Future上调用get()。仅在Hystrix 1.4.0之前 调用get()触发超时机制在此类中生效 一个案例。
他们也提到过 -
请注意,有关于关闭每个命令超时的配置, 如果需要(参见command.timeout.enabled)。
几个问题 -
答案 0 :(得分:0)
如果我启用了超时标志,那是否意味着行为是同步的?如果我关闭超时标志,你看到有什么问题吗?
除非您在命令上调用execute
,否则行为不会阻塞。此外,hystrix执行与调用线程池(如Tomcat或任何服务器线程池)分离,并以并行方式在该上下文中执行。
您应该保持线程隔离timeout
以获得正确的值。另外,我建议给它超过缓存客户端套接字超时。如果您没有正确调整它们,您可能需要为该特定命令设置的线程池可能会饱和并且您会被拒绝。
如果池中有20个线程并且所有这些线程都忙于进行异步的Set调用会发生什么?
您获得执行拒绝例外。因此,我建议您根据自己的情况专门调整timeouts
和thread pool
。忽视,失败,并在需要时让电路中断。