简介
Hystrix使用Archaius作为配置属性的默认实现。
下面的文档描述了默认的HystrixPropertiesStrategy实现,除非您使用插件覆盖它。
每个属性有四个优先级:
1.代码的全局默认值
如果以下3个选项都没有设置,这是默认设置。
全局默认值在下表中显示为“Default Value”。
2.动态全局默认属性
可以使用属性更改全局默认值。
全局默认属性名在下面的表中显示为“Default Property”。
3.代码中的实例默认值
您可以定义特定于实例的默认值。例子:
HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(int value)
您可以将这类命令以类似的方式插入到HystrixCommand构造函数中:
public HystrixCommandInstance(int id) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(500))); this.id = id; }
对于通常设置的初值,有一些方便的构造函数。这里有一个例子:
public HystrixCommandInstance(int id) { super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"), 500); this.id = id; }
4.动态实例属性
您可以动态地设置特定于实例的值,这些值覆盖了前面三个级别的缺省值。
动态实例属性名在下面的表中显示为“Instance Property”。
例子:
Instance Property | hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds |
---|
使用目标HystrixCommand
的HystrixCommandKey.name()的值
替换属性中的HystrixCommandKey 。
例如,如果key 名为“SubscriberGetAccount”,则属性名为:
hystrix.command.SubscriberGetAccount.execution.isolation.thread.timeoutInMilliseconds
Command属性
以下属性控制HystrixCommand行为:
Execution
以下属性控制HystrixCommand.run()的执行方式。
execution.isolation.strategy
此属性指示HystrixCommand.run()使用以下两个选项之一执行隔离策略:
THREAD
它在单独的线程上执行,并发请求受线程池中线程数量的限制SEMAPHORE
它在调用线程上执行,并发请求受信号量计数的限制
THREAD
还是SEMAPHORE
默认情况下,以及推荐的设置,是使用线程隔离(THREAD)运行HystrixCommands,使用信号量隔离(SEMAPHORE)运行HystrixObservableCommands 。
在线程中执行的命令具有额外的一层保护,可以防止网络超时所能提供的延迟。
通常,您应该为HystrixCommands使用信号量隔离的惟一时间是当调用的容量非常大(每秒数百个,每个实例),以至于单独线程的开销过高时;这通常只适用于非网络调用。
Netflix API有100多个命令运行在40多个线程池中,只有少数命令不在线程中运行——这些命令从内存缓存中获取元数据,或者是线程隔离命令的外观。(有关这方面的更多信息,请参见“Primary + Secondary with Fallback”模式)。
有关此决策的更多信息,请参见隔离是如何工作的。
Default Value | THREAD (see ExecutionIsolationStrategy.THREAD) |
---|---|
Possible Values | THREAD, SEMAPHORE |
Default Property | hystrix.command.default.execution.isolation.strategy |
Instance Property | hystrix.command.HystrixCommandKey.execution.isolation.strategy |
How to Set Instance Default: | // to use thread isolation HystrixCommandProperties.Setter() .withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD) // to use semaphore isolation HystrixCommandProperties.Setter() .withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE) |
execution.isolation.thread.timeoutInMilliseconds
此属性设置调用方观察超时并退出命令执行的毫秒时间。Hystrix将HystrixCommand标记为超时,并执行回退逻辑。请注意,如果需要的话,可以对每个命令进行关闭超时的配置(请参见command.timeout.enabled)。
注意:超时将在HystrixCommand.queue()上触发,即使调用方从未在结果的Future调用get()。在Hystrix 1.4.0之前,只有对get()的调用才会触发超时机制在这种情况下生效。
Default Value | 1000 |
---|---|
Default Property | hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds |
Instance Property | hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds |
How to Set Instance Default | HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(int value) |
execution.timeout.enabled
该属性指示HystrixCommand.run()执行是否应该有超时。
Default Value | true |
---|---|
Default Property | hystrix.command.default.execution.timeout.enabled |
Instance Property | hystrix.command.HystrixCommandKey.execution.timeout.enabled |
How to Set Instance Default | HystrixCommandProperties.Setter() .withExecutionTimeoutEnabled(boolean value) |
execution.isolation.thread.interruptOnTimeout
此属性指示是否应在超时发生时中断HystrixCommand.run()执行。
Default Value | true |
---|---|
Default Property | hystrix.command.default.execution.isolation.thread.interruptOnTimeout |
Instance Property | hystrix.command.HystrixCommandKe |