源码如下,看完包会
protected static HystrixCommandProperties.Setter createSetter(IClientConfig config,
String commandKey, ZuulProperties zuulProperties) {
//获取HystrixTimeout,设置HystrixCommand的Setter
int hystrixTimeout = getHystrixTimeout(config, commandKey);
return HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(
zuulProperties.getRibbonIsolationStrategy())
.withExecutionTimeoutInMilliseconds(hystrixTimeout);
}
protected static int getHystrixTimeout(IClientConfig config, String commandKey) {
int ribbonTimeout = getRibbonTimeout(config, commandKey);
DynamicPropertyFactory dynamicPropertyFactory = DynamicPropertyFactory
.getInstance();
int defaultHystrixTimeout = dynamicPropertyFactory.getIntProperty(
"hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds",
0).get();
int commandHystrixTimeout = dynamicPropertyFactory
.getIntProperty("hystrix.command." + commandKey
+ ".execution.isolation.thread.timeoutInMilliseconds", 0)
.get();
int hystrixTimeout;
if (commandHystrixTimeout > 0) {
hystrixTimeout = commandHystrixTimeout;
}
else if (defaultHystrixTimeout > 0) {
hystrixTimeout = defaultHystrixTimeout;
}
else {
hystrixTimeout = ribbonTimeout;
}
if (hystrixTimeout < ribbonTimeout) {
LOGGER.warn("The Hystrix timeout of " + hystrixTimeout + "ms for the command "
+ commandKey
+ " is set lower than the combination of the Ribbon read and connect timeout, "
+ ribbonTimeout + "ms.");
}
return hystrixTimeout;
}
protected static int getRibbonTimeout(IClientConfig config, String commandKey) {
int ribbonTimeout;
if (config == null) {
ribbonTimeout = RibbonClientConfiguration.DEFAULT_READ_TIMEOUT
+ RibbonClientConfiguration.DEFAULT_CONNECT_TIMEOUT;
}
else {
int ribbonReadTimeout = getTimeout(config, commandKey, "ReadTimeout",
IClientConfigKey.Keys.ReadTimeout,
//默认1000
RibbonClientConfiguration.DEFAULT_READ_TIMEOUT);
int ribbonConnectTimeout = getTimeout(config, commandKey, "ConnectTimeout",
IClientConfigKey.Keys.ConnectTimeout,
//默认1000
RibbonClientConfiguration.DEFAULT_CONNECT_TIMEOUT);
int maxAutoRetries = getTimeout(config, commandKey, "MaxAutoRetries",
IClientConfigKey.Keys.MaxAutoRetries,
//默认0
DefaultClientConfigImpl.DEFAULT_MAX_AUTO_RETRIES);
int maxAutoRetriesNextServer = getTimeout(config, commandKey,
"MaxAutoRetriesNextServer",
IClientConfigKey.Keys.MaxAutoRetriesNextServer,
//默认1
DefaultClientConfigImpl.DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER);
ribbonTimeout = (ribbonReadTimeout + ribbonConnectTimeout)
* (maxAutoRetries + 1) * (maxAutoRetriesNextServer + 1);
}
return ribbonTimeout;
}
private static int getTimeout(IClientConfig config, String commandKey,
String property, IClientConfigKey<Integer> configKey, int defaultValue) {
DynamicPropertyFactory dynamicPropertyFactory = DynamicPropertyFactory
.getInstance();
return dynamicPropertyFactory
.getIntProperty(commandKey + "." + config.getNameSpace() + "." + property,
config.get(configKey, defaultValue))
.get();
}