Spring Cloud Hystrix降级处理超时时间设置
默认情况下调用接口能够触发Hystrix服务降级处理的超时时间是1000ms,我们可以通过HystrixCommandProperties类的源码查找到,
package com.netflix.hystrix;
import com.netflix.hystrix.strategy.properties.HystrixDynamicProperty;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty;
import com.netflix.hystrix.strategy.properties.HystrixProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class HystrixCommandProperties {
private static final Logger logger = LoggerFactory.getLogger(HystrixCommandProperties.class);
static final Integer default_metricsRollingStatisticalWindow = 10000;
private static final Integer default_metricsRollingStatisticalWindowBuckets = 10;
private static final Integer default_circuitBreakerRequestVolumeThreshold = 20;
private static final Integer default_circuitBreakerSleepWindowInMilliseconds = 5000;
private static final Integer default_circuitBreakerErrorThresholdPercentage = 50;
private static final Boolean default_circuitBreakerForceOpen = false;
static final Boolean default_circuitBreakerForceClosed = false;
private static final Integer default_executionTimeoutInMilliseconds = 1000; <----!!这个 超时时间
private static final Boolean default_executionTimeoutEnabled = true;
private static final HystrixCommandProperties.ExecutionIsolationStrategy default_executionIsolationStrategy;
private static final Boolean default_executionIsolationThreadInterruptOnTimeout;
private static final Boolean default_executionIsolationThreadInterruptOnFutureCancel;
private static final Boolean default_metricsRollingPercentileEnabled;
private static final Boolean default_requestCacheEnabled;
private static final Integer default_fallbackIsolationSemaphoreMaxConcurrentRequests;
private static final Boolean default_fallbackEnabled;
private static final Integer default_executionIsolationSemaphoreMaxConcurrentRequests;
private static final Boolean default_requestLogEnabled;
private static final Boolean default_circuitBreakerEnabled;
private static final Integer default_metricsRollingPercentileWindow;
private static final Integer default_metricsRollingPercentileWindowBuckets;
private static final Integer default_metricsRollingPercentileBucketSize;
private static final Integer default_metricsHealthSnapshotIntervalInMilliseconds;
private final HystrixCommandKey key;
private final HystrixProperty<Integer> circuitBreakerRequestVolumeThreshold;
private final HystrixProperty<Integer> circuitBreakerSleepWindowInMilliseconds;
private final HystrixProperty<Boolean> circuitBreakerEnabled;
private final HystrixProperty<Integer> circuitBreakerErrorThresholdPercentage;
private final HystrixProperty<Boolean> circuitBreakerForceOpen;
private final HystrixProperty<Boolean> circuitBreakerForceClosed;
private final HystrixProperty<HystrixCommandProperties.ExecutionIsolationStrategy> executionIsolationStrategy;
private final HystrixProperty<Integer> executionTimeoutInMilliseconds;
private final HystrixProperty<Boolean> executionTimeoutEnabled;
private final HystrixProperty<String> executionIsolationThreadPoolKeyOverride;
private final HystrixProperty<Integer> executionIsolationSemaphoreMaxConcurrentRequests;
private final HystrixProperty<Integer> fallbackIsolationSemaphoreMaxConcurrentRequests;
private final HystrixProperty<Boolean> fallbackEnabled;
private final HystrixProperty<Boolean> executionIsolationThreadInterruptOnTimeout;
private final HystrixProperty<Boolean> executionIsolationThreadInterruptOnFutureCancel;
private final HystrixProperty<Integer> metricsRollingStatisticalWindowInMilliseconds;
private final HystrixProperty<Integer> metricsRollingStatisticalWindowBuckets;
private final HystrixProperty<Boolean> metricsRollingPercentileEnabled;
private final HystrixProperty<Integer> metricsRollingPercentileWindowInMilliseconds;
private final HystrixProperty<Integer> metricsRollingPercentileWindowBuckets;
private final HystrixProperty<Integer> metricsRollingPercentileBucketSize;
private final HystrixProperty<Integer> metricsHealthSnapshotIntervalInMilliseconds;
private final HystrixProperty<Boolean> requestLogEnabled;
private final HystrixProperty<Boolean> requestCacheEnabled;
根据这个
protected HystrixCommandProperties(HystrixCommandKey key, HystrixCommandProperties.Setter builder, String propertyPrefix) {
this.key = key;
this.circuitBreakerEnabled = getProperty(propertyPrefix, key, "circuitBreaker.enabled", builder.getCircuitBreakerEnabled(), default_circuitBreakerEnabled);
this.circuitBreakerRequestVolumeThreshold = getProperty(propertyPrefix, key, "circuitBreaker.requestVolumeThreshold", builder.getCircuitBreakerRequestVolumeThreshold(), default_circuitBreakerRequestVolumeThreshold);
this.circuitBreakerSleepWindowInMilliseconds = getProperty(propertyPrefix, key, "circuitBreaker.sleepWindowInMilliseconds", builder.getCircuitBreakerSleepWindowInMilliseconds(), default_circuitBreakerSleepWindowInMilliseconds);
this.circuitBreakerErrorThresholdPercentage = getProperty(propertyPrefix, key, "circuitBreaker.errorThresholdPercentage", builder.getCircuitBreakerErrorThresholdPercentage(), default_circuitBreakerErrorThresholdPercentage);
this.circuitBreakerForceOpen = getProperty(propertyPrefix, key, "circuitBreaker.forceOpen", builder.getCircuitBreakerForceOpen(), default_circuitBreakerForceOpen);
this.circuitBreakerForceClosed = getProperty(propertyPrefix, key, "circuitBreaker.forceClosed", builder.getCircuitBreakerForceClosed(), default_circuitBreakerForceClosed);
this.executionIsolationStrategy = getProperty(propertyPrefix, key, "execution.isolation.strategy", builder.getExecutionIsolationStrategy(), default_executionIsolationStrategy);
this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadTimeoutInMilliseconds(), default_executionTimeoutInMilliseconds);
this.executionTimeoutEnabled = getProperty(propertyPrefix, key, "execution.timeout.enabled", builder.getExecutionTimeoutEnabled(), default_executionTimeoutEnabled);
this.executionIsolationThreadInterruptOnTimeout = getProperty(propertyPrefix, key, "execution.isolation.thread.interruptOnTimeout", builder.getExecutionIsolationThreadInterruptOnTimeout(), default_executionIsolationThreadInterruptOnTimeout);
this.executionIsolationThreadInterruptOnFutureCancel = getProperty(propertyPrefix, key, "execution.isolation.thread.interruptOnFutureCancel", builder.getExecutionIsolationThreadInterruptOnFutureCancel(), default_executionIsolationThreadInterruptOnFutureCancel);
this.executionIsolationSemaphoreMaxConcurrentRequests = getProperty(propertyPrefix, key, "execution.isolation.semaphore.maxConcurrentRequests", builder.getExecutionIsolationSemaphoreMaxConcurrentRequests(), default_executionIsolationSemaphoreMaxConcurrentRequests);
this.fallbackIsolationSemaphoreMaxConcurrentRequests = getProperty(propertyPrefix, key, "fallback.isolation.semaphore.maxConcurrentRequests", builder.getFallbackIsolationSemaphoreMaxConcurrentRequests(), default_fallbackIsolationSemaphoreMaxConcurrentRequests);
this.fallbackEnabled = getProperty(propertyPrefix, key, "fallback.enabled", builder.getFallbackEnabled(), default_fallbackEnabled);
this.metricsRollingStatisticalWindowInMilliseconds = getProperty(propertyPrefix, key, "metrics.rollingStats.timeInMilliseconds", builder.getMetricsRollingStatisticalWindowInMilliseconds(), default_metricsRollingStatisticalWindow);
this.metricsRollingStatisticalWindowBuckets = getProperty(propertyPrefix, key, "metrics.rollingStats.numBuckets", builder.getMetricsRollingStatisticalWindowBuckets(), default_metricsRollingStatisticalWindowBuckets);
this.metricsRollingPercentileEnabled = getProperty(propertyPrefix, key, "metrics.rollingPercentile.enabled", builder.getMetricsRollingPercentileEnabled(), default_metricsRollingPercentileEnabled);
this.metricsRollingPercentileWindowInMilliseconds = getProperty(propertyPrefix, key, "metrics.rollingPercentile.timeInMilliseconds", builder.getMetricsRollingPercentileWindowInMilliseconds(), default_metricsRollingPercentileWindow);
this.metricsRollingPercentileWindowBuckets = getProperty(propertyPrefix, key, "metrics.rollingPercentile.numBuckets", builder.getMetricsRollingPercentileWindowBuckets(), default_metricsRollingPercentileWindowBuckets);
this.metricsRollingPercentileBucketSize = getProperty(propertyPrefix, key, "metrics.rollingPercentile.bucketSize", builder.getMetricsRollingPercentileBucketSize(), default_metricsRollingPercentileBucketSize);
this.metricsHealthSnapshotIntervalInMilliseconds = getProperty(propertyPrefix, key, "metrics.healthSnapshot.intervalInMilliseconds", builder.getMetricsHealthSnapshotIntervalInMilliseconds(), default_metricsHealthSnapshotIntervalInMilliseconds);
this.requestCacheEnabled = getProperty(propertyPrefix, key, "requestCache.enabled", builder.getRequestCacheEnabled(), default_requestCacheEnabled);
this.requestLogEnabled = getProperty(propertyPrefix, key, "requestLog.enabled", builder.getRequestLogEnabled(), default_requestLogEnabled);
this.executionIsolationThreadPoolKeyOverride = HystrixPropertiesChainedProperty.forString().add(propertyPrefix + ".command." + key.name() + ".threadPoolKeyOverride", (Object)null).build();
}
确认通过配置属性"execution.isolation.thread.timeoutInMilliseconds" 可以使超时时间属性生效
this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadTimeoutInMilliseconds(), default_executionTimeoutInMilliseconds);
代码中设置为
@HystrixCommand(fallbackMethod = "timeout_handler", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
})