Hystrix配置详解

在之前介绍Hystrix的使用方法时,已经涉及过一下Hystrix属性的配置,我们可以根据实现HystrixCommand的不同方式将配制方法分为如下两类。

①通过继承的方式实现,可用Setter对象来对请求命令的属性进行设置,比如下面的例子:

public UserCommand() {
	super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GroupName"))
            .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
            .withExecutionTimeoutInMilliseconds(5000))
	    .andCommandKey(HystrixCommandKey.Factory.asKey("CommandName"))
	    .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("ThreadPoolKey")));
	}

②通过注解的方法实现,只需要使用@HystrixCommand中的commandProperties属性来设置,比如:

@HystrixCommand(commandKey="hello",commandProperties={
	@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000")
})
public User findById(String name,int age){
	return restTemplate.getForObject("http://HELLO-SERVICE/hystrix/getUser?name={1}&age={2}", User.class, name, age);
}

Hystrix为我们提供的属性都存在四种优先级别的配置(优先级由低到高)。

  1. 全局默认值:如果没有设置下面三个优先级的属性,那么这个属性就是默认值。由于该属性通过代码定义,所以对这个级别,我们需要关注它在代码中定义的默认值即可。
  2. 全局配置属性:通过在配置文件中定义全局属性值,在应用启动时或在与Spring Cloud Config和Spring Cloud Bus实现的动态刷新配置功能的配合下,可以实现对“全局默认值”的覆盖已经在运行期对“全局默认值”的动态调整。
  3. 实例默认值:通过代码为实例定义的默认值。通过代码的方式为实例设置属性值来覆盖默认的全局配置。
  4. 实例配置属性:通过配置文件来为指定的实例进行属性配置,以覆盖前面的三个默认值。它也可用Spring Cloud Config 和Spring Cloud Bus实现的动态刷新配置功能实现对具体实例配置的动态调整。

关于配置的写法,这里作简要说明:

属性配置可以是在properties文件中,也可以是在方法注解的属性里配置,两处配置的属性名称有区别,在properties里配置的属性是以 hystrix.command.default. 、 hystrix.threadpool.default 、 hystrix.collapser.default 开头,其中default表示默认值,如需要配置指定commandKey的值,将default换成commandKey即可。如果是在方法注解的属性里配置,则不需要这个前缀。下面我们来具体看看Hystrix有哪些具体的属性配置,且详细说明了属性配置在properties里配置和在方法注解里配置的写法。

Command属性

Command属性主要用来控制HystrixCommand命令的行为。它主要有下面5种不同类型的属性配置。

execution配置

execution配置控制的是HystrixCommand.run()的执行。

execution.isolation.strategy:该属性用来设置HystrixCommand.run()执行的隔离策略,它有如下两个选项。

THREAD:通过线程池隔离的策略。它在独立的线程上执行,并且它的并发限制受线程池中线程数量的限制。

扫描二维码关注公众号,回复: 3166028 查看本文章

SEMAPHORE:通过信号量隔离的策略。它在调用线程上执行,并且它的并发限制受信号量计数的限制。

全局默认值 THREAD
全局配置属性 hystrix.command.default.execution.isolation.strategy
实例默认值 通过HystrixCommandProperties.Setter().withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)设置,也可通过@HystrixProperty(name="execution.isolation.strategy",value="THREAD")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.execution.isolation.strategy

execution.isolation.thread.timeoutMilliseconds:该属性用来配置HystrixCommand执行的超时时间,单位为毫秒。当HystrixCommand执行时间超过该配置值后,Hystrix会将该命令标记为TIMEOUT并进入服务降级处理逻辑。

全局默认值 1000毫秒
全局配置属性 hystrix.command.default.execution.isolation.thread.timeoutMilliseconds
实例默认值 通过HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(int value)设置,也可通过@HystrixProperty(name="execution.isolation.thread.timeoutMilliseconds",value="2000")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutMilliseconds

execution.timeout.enabled:该属性用来设置HystrixCommand.run()的执行是否启用超时时间。默认为true,如果设置为false,那么execution.isolation.thread.timeoutMilliseconds属性将不起作用。

全局默认值 true
全局配置属性 hystrix.command.default.execution.timeout.enabled
实例默认值 通过HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(boolean value)设置,也可通过@HystrixProperty(name="execution.timeout.enabled",value="false")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.execution.timeout.enabled

execution.isolation.thread.interruptOnTimeout:该属性用来配置当HystrixCommand.run()执行超时的时候是否要将它中断。

全局默认值 true
全局配置属性 hystrix.command.default.execution.isolation.thread.interruptOnTimeout
实例默认值 通过HystrixCommandProperties.Setter().withExecutionIsolationThreadInterruptOnTimeout(boolean value)设置,也可通过@HystrixProperty(name="execution.isolation.thread.interruptOnTimeout",value="false")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout

execution.isolation.thread.interruptOnCancel:该属性用来配置当HystrixCommand.run()执行被取消时候是否要将它中断。

全局默认值 true
全局配置属性 hystrix.command.default.execution.isolation.thread.interruptOnCancel
实例默认值 通过HystrixCommandProperties.Setter().withExecutionIsolationThreadInterruptOnCancel(boolean value)设置,也可通过@HystrixProperty(name="execution.isolation.thread.interruptOnCancel",value="false")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnCancel

execution.isolation.semaphore.maxConcurrentRequests:当HystrixCommand的隔离策略使用信号量时,该属性用来配置信号量的大小(并发请求数)。当最大并发请求数达到该设置值时,后续的请求将会被拒绝。

全局默认值 10
全局配置属性 hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
实例默认值 通过HystrixCommandProperties.Setter().withExecutionIsolationSemaphoreMaxConcurrentRequests(int value)设置,也可通过@HystrixProperty(name="execution.isolation.semaphore.maxConcurrentRequests",value="20")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests

fallback配置

下面这些属性用来控制HystrixCommand.getFallback()的执行。这些属性同时适用于线程池的信号量的隔离策略。

fallback.isolation.semaphore.maxConcurrentRequests:该属性用来设置从调用线程中允许HystrixCommand.fallback()方法执行的最大并发请求数。当达到最大并发请求数时,后续的请求将会被拒绝并抛出异常(因为它已经没有后续的fallback可以被调用了)。

全局默认值 10
全局配置属性 hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests
实例默认值 通过HystrixCommandProperties.Setter().withFallbackIsolationSemaphoreMaxConcurrentRequests(int value)设置,也可通过@HystrixProperty(name="fallback.isolation.semaphore.maxConcurrentRequests",value="20")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests

fallback.enabled:该属性用来设置服务降级策略是否启用,如果设置为false,那么当请求失败或拒绝发生时,将不会调用HystrixCommand.getFallback()来执行服务降级逻辑。

全局默认值 true
全局配置属性 hystrix.command.default.fallback.enabled
实例默认值 通过HystrixCommandProperties.Setter().withFallbackEnabled(boolean value)设置,也可通过@HystrixProperty(name="fallback.enabled",value="false")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.fallback.enabled

circuitBreaker配置

下面这些是断路器的属性位置,用来控制HystrixCircuitBreaker的行为。

circuitBreaker.enabled:该属性用来确定当服务请求命令失败时,是否使用断路器来跟踪其健康指标和熔断请求。

全局默认值 true
全局配置属性 hystrix.command.default.circuitBreaker.enabled
实例默认值 通过HystrixCommandProperties.Setter().withCircuitBreakerEnabled(boolean value)设置,也可通过@HystrixProperty(name="circuitBreaker.enabled",value="false")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.circuitBreaker.enabled

circuitBreaker.requestVolumeThreshold:该属性用来设置在滚动时间窗中,断路器熔断的最小请求数。例如:默认值为20的时候,如果滚动时间窗(默认10s)内收到了19个请求,即使这19个请求都失败了,断路器也不会打开。

全局默认值 20
全局配置属性 hystrix.command.default.circuitBreaker.requestVolumeThreshold
实例默认值 通过HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(int value)设置,也可通过@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value="30")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold

circuitBreaker.sleepWindowInMilliseconds:该属性用来设置当断路器打开之后的休眠时间窗。休眠时间窗结束后,会将断路器置为“半开”状态,尝试熔断的请求命令,如果依然失败就将断路器继续设置为“打开”状态,如果成功就设置为“关闭”状态。

全局默认值 5000
全局配置属性 hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds
实例默认值 通过HystrixCommandProperties.Setter().withCircuitBreakerSleepWindowInMilliseconds(int value)设置,也可通过@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="3000")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds

circuitBreaker.errorThresholdPercentage:该属性用来设置断路器打开的错误百分比条件。比如,默认值为50的情况下,表示在滚动时间窗中,在请求数量超过circuitBreaker.requestVolumeThreshold阈值的前提下,如果错误请求数的百分比超过50,就把断路器设置为“打开”状态,否则就设置为“关闭”状态。

全局默认值 50
全局配置属性 hystrix.command.default.circuitBreaker.errorThresholdPercentage
实例默认值 通过HystrixCommandProperties.Setter().withCircuitBreakerErrorThresholdPercentage(int value)设置,也可通过@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="40")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage

circuitBreaker.forceOpen:如果将该属性设置为true,断路器将强制进入“打开”状态,它会拒绝所有请求。该属性优先于circuitBreaker.forceClosed属性。

全局默认值 false
全局配置属性 hystrix.command.default.circuitBreaker.forceOpen
实例默认值 通过HystrixCommandProperties.Setter().withCircuitBreakerForceOpen(boolean value)设置,也可通过@HystrixProperty(name="circuitBreaker.forceOpen",value="true")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen

circuitBreaker.forceClosed:如果将该属性设置为true,断路器将强制进入“关闭”状态,它会接收所有请求。如果circuitBreaker.forceOpen属性为true,该属性不会生效。

全局默认值 false
全局配置属性 hystrix.command.default.circuitBreaker.forceClosed
实例默认值 通过HystrixCommandProperties.Setter().withCircuitBreakerForceClosed(boolean value)设置,也可通过@HystrixProperty(name="circuitBreaker.forceClosed",value="true")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed

metrics配置

下面的属性均与HystrixCommand和HystrixObservableCommand执行中捕获的指标信息有关。

metrics.rollingStats.timeInMilliseconds:该属性用来设置滚动时间窗的长度,单位为毫秒。该时间同于断路器判断健康度时需要收集信息的持续时间。断路器在收集指标信息的时候会根据设置的时间窗长度拆分成多个“桶”来累计各度量值,每个“桶”记录了一段时间内的采集指标。例如,当采用默认值10000毫秒时,断路器默认将其拆分成10个桶(桶的数量也可通过metrics.rollingStats.numBuckets参数设置),每个桶记录1000毫秒内的指标信息。

全局默认值 10000
全局配置属性 hystrix.command.default.metrics.rollingStats.timeInMilliseconds
实例默认值 通过HystrixCommandProperties.Setter().withMetricsRollingStatisticalWindowInMilliseconds(int value)设置,也可通过@HystrixProperty(name="metrics.rollingStats.timeInMilliseconds",value="20000")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.metrics.rollingStats.timeInMilliseconds

注意:该属性从Hystrix1.4.12版本开始只有在应用初始化时生效,通过动态刷新不会产生效果,避免运行期监测数据丢失。

metrics.rollingStats.numBuckets:该属性用来设置滚动时间窗统计指标信息时划分“桶”的数量。

全局默认值 10
全局配置属性 hystrix.command.default.metrics.rollingStats.numBuckets
实例默认值 通过HystrixCommandProperties.Setter().withMetricsRollingStatisticalWindowBuckets(int value)设置,也可通过@HystrixProperty(name="metrics.rollingStats.numBuckets",value="20")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.metrics.rollingStats.numBuckets

注意:metrics.rollingStats.timeInMilliseconds参数的设置必须能被metrics.rollingStats.numBuckets参数整除,否则抛出异常,且1.4.12版本开始只有在应用初始化时生效,通过动态刷新不会产生效果,避免运行期监测数据丢失。

metrics.rollingPercentile.enabled:该属性用来设置对命令执行的延迟是否使用百分位数来跟踪和计算。如果为false,那么所有的概要统计都将返回-1。

全局默认值 true
全局配置属性 hystrix.command.default.metrics.rollingPercentile.enabled
实例默认值 通过HystrixCommandProperties.Setter().withMetricsRollingPercentileEnabled(boolean value)设置,也可通过@HystrixProperty(name="metrics.rollingPercentile.enabled",value="false")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.metrics.rollingPercentile.enabled

metrics.rollingPercentile.timeInMilliseconds:该属性用来设置百分位统计的滚动窗口的持续时间,单位毫秒。

全局默认值 60000
全局配置属性 hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds
实例默认值 通过HystrixCommandProperties.Setter().withMetricsRollingPercentileWindowInMilliseconds(int value)设置,也可通过@HystrixProperty(name="metrics.rollingPercentile.timeInMilliseconds",value="50000")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.metrics.rollingPercentile.timeInMilliseconds

注意:该属性从1.4.12版本开始只有在应用初始化时生效,通过动态刷新不会产生效果,避免运行期监测数据丢失。

metrics.rollingPercentile.numBuckets:该属性用来设置百分位统计滚动窗口中使用“桶”的数量。

全局默认值 6
全局配置属性 hystrix.command.default.metrics.rollingPercentile.numBuckets
实例默认值 通过HystrixCommandProperties.Setter().withMetricsRollingPercentileWindowBuckets(int value)设置,也可通过@HystrixProperty(name="metrics.rollingPercentile.numBuckets",value="5")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.metrics.rollingPercentile.numBuckets

注意:metrics.rollingPercentile.timeInMilliseconds参数的设置必须能被metrics.rollingPercentile.numBuckets参数整除,否则抛出异常,且1.4.12版本开始只有在应用初始化时生效,通过动态刷新不会产生效果,避免运行期监测数据丢失。

metrics.rollingPercentile.bucketSize:该属性用来设置在执行过程中每个“桶”中保留的最大执行次数。如果在滚动时间窗内发生超过该设定值的执行次数,就从最初的位置开始重写。例如,将该值设为100,滚动窗口10秒,若在10秒内一个“桶”中发生500次执行,那么该“桶”中只保留最后的100次执行统计。另外,增加该值的大小将会增加内存量的消耗,并增加排序百分位数所需的计算时间。

全局默认值 100
全局配置属性 hystrix.command.default.metrics.rollingPercentile.bucketSize
实例默认值 通过HystrixCommandProperties.Setter().withMetricsRollingPercentileBucketSize(int value)设置,也可通过@HystrixProperty(name="metrics.rollingPercentile.bucketSize",value="120")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.metrics.rollingPercentile.bucketSize

注意:该属性从1.4.12版本开始只有在应用初始化时生效,通过动态刷新不会产生效果,避免运行期监测数据丢失。

metrics.healthSnapshot.intervalInMilliseconds:该属性用来设置采集影响断路器状态的健康快照(请求成功、错误百分比)的间隔等待时间。

全局默认值 500
全局配置属性 hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds
实例默认值 通过HystrixCommandProperties.Setter().withMetricsHealthSnapshotIntervalInMilliseconds(int value)设置,也可通过@HystrixProperty(name="metrics.healthSnapshot.intervalInMilliseconds",value="600")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.metrics.healthSnapshot.intervalInMilliseconds

requestContext配置

下面的属性涉及HystrixCommand使用的HystrixRequestContext的设置。

requestCache.enabled:此属性用来设置是否开启请求缓存。

全局默认值 true
全局配置属性 hystrix.command.default.requestCache.enabled
实例默认值 通过HystrixCommandProperties.Setter().withRequestCacheEnabled(boolean value)设置,也可通过@HystrixProperty(name="requestCache.enabled",value="false")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.requestCache.enabled

requestLog.enabled:该属性用来设置HystrixCommand的执行和事件是否打印日志到HystrixRequestLog中。

全局默认值 true
全局配置属性 hystrix.command.default.requestLog.enabled
实例默认值 通过HystrixCommandProperties.Setter().withRequestLogEnabled(boolean value)设置,也可通过@HystrixProperty(name="requestLog.enabled",value="false")注解设置
实例配置属性 hystrix.command.HystrixCommandKey.requestLog.enabled

collapser属性

该属性除了在代码中用Setter()和配置文件配置外,也可使用注解进行配置。可使用@HystrixCollapser中的collapserProperties属性来设置,比如:

@HystrixCollapser(batchMethod="findByBatch",collapserProperties={
			@HystrixProperty(name="timerDelayInMilliseconds",value="100")
	})

下面这些属性用来控制命令合并相关的行为。

maxRequestsInBatch:该参数用来设置一次请求合并批处理中允许的最大请求数。

全局默认值 Integer.MAX_VALUE
全局配置属性 hystrix.collapser.default.maxRequestsInBatch
实例默认值 通过HystrixCollapserProperties.Setter().withMaxRequestsInBatch(int value)设置,也可通过@HystrixProperty(name="maxRequestsInBatch",value="600")注解设置
实例配置属性 hystrix.collapser.HystrixCollapserKey.maxRequestsInBatch

timerDelayInMilliseconds:该属性用来设置批处理过程中每个命令延迟的时间,单位为毫秒。

全局默认值 10
全局配置属性 hystrix.collapser.default.timerDelayInMilliseconds
实例默认值 通过HystrixCollapserProperties.Setter().withTimerDelayInMilliseconds(int value)设置,也可通过@HystrixProperty(name="timerDelayInMilliseconds",value="20")注解设置
实例配置属性 hystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds

requestCache.enabled:该属性用来设置批处理过程中是否开启缓存。

全局默认值 true
全局配置属性 hystrix.collapser.default.requestCache.enabled
实例默认值 通过HystrixCollapserProperties.Setter().withRequestCacheEnabled(boolean value)设置,也可通过@HystrixProperty(name="requestCache.enabled",value="false")注解设置
实例配置属性 hystrix.collapser.HystrixCollapserKey.requestCache.enabled

threadPool属性

该属性除了在代码中用Setter()和配置文件配置外,还可使用注解进行配置。可使用@HystrixCommand中的threadPoolProperties属性来设置,比如:

@HystrixCommand(threadPoolProperties={
		@HystrixProperty(name="coreSize",value="20")
})

下面这些属性用来控制Hystrix命令所属线程池的配置。

coreSize:该属性用来设置执行命令线程池的核心线程数,该值也是命令执行的最大并发量。

全局默认值 10
全局配置属性 hystrix.threadpool.default.coreSize
实例默认值 通过HystrixThreadPoolProperties.Setter().withCoreSize(int value)设置,也可通过@HystrixProperty(name="coreSize",value="20")注解设置
实例配置属性 hystrix.threadpool.HystrixThreadPoolKey.coreSize

maxQueueSize:该属性用来设置线程池的最大队列大小。当设置为-1时,线程池将使用SynchronousQueue实现的队列,否则将使用LinkedBlockingQueue实现的队列。

全局默认值 -1
全局配置属性 hystrix.threadpool.default.maxQueueSize
实例默认值 通过HystrixThreadPoolProperties.Setter().withMaxQueueSize(int value)设置,也可通过@HystrixProperty(name="maxQueueSize",value="10")注解设置
实例配置属性 hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize

注意:该属性只有在应用初始化时生效,通过动态刷新不会产生效果。

queueSizeRejectionThreshold:该属性用来为队列设置拒绝阈值。通过该参数,即使队列没有达到最大值也能拒绝请求。该参数主要是对LinkedBlockingQueue队列的补充,因为LinkedBlockingQueue队列不能动态修改它的对象大小,而通过该属性就可以调整拒绝请求的队列大小了。

全局默认值 5
全局配置属性 hystrix.threadpool.default.queueSizeRejectionThreshold
实例默认值 通过HystrixThreadPoolProperties.Setter().withQueueSizeRejectionThreshold(int value)设置,也可通过@HystrixProperty(name="queueSizeRejectionThreshold",value="10")注解设置
实例配置属性 hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold

注意:当maxQueueSize属性值为-1时,该属性不会生效。

metrics.rollingStats.timeInMilliseconds:该属性用来设置滚动时间窗的长度,单位为毫秒。该滚动时间窗的长度用于线程池的指标度量,它会被分成多个“桶”来统计指标。

全局默认值 10000
全局配置属性 hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds
实例默认值 通过HystrixThreadPoolProperties.Setter().withMetricsRollingStatisticalWindowInMilliseconds(int value)设置,也可通过@HystrixProperty(name="metrics.rollingStats.timeInMilliseconds",value="10")注解设置
实例配置属性 hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.timeInMilliseconds

metrics.rollingStats.numBuckets:该属性用来设置滚动时间窗被划分成“桶”的数量。

全局默认值 10
全局配置属性 hystrix.threadpool.default.metrics.rollingStats.numBuckets
实例默认值 通过HystrixThreadPoolProperties.Setter().withMetricsRollingStatisticalWindowsBuckets(int value)设置,也可通过@HystrixProperty(name="metrics.rollingStats.numBuckets",value="20")注解设置
实例配置属性 hystrix.threadpool.HystrixThreadPoolKey.metrics.rollingStats.numBuckets

注意:metrics.rollingStats.timeInMilliseconds参数的设置必须能被metrics.rollingStats.numBuckets参数整除,否则抛出异常。

threadPool属性通过Setter设置示例:

public UserCommand() {
	super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GroupName"))
				.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
						.withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)
						.withExecutionTimeoutInMilliseconds(5000)
						)
				.andCommandKey(HystrixCommandKey.Factory.asKey("CommandName"))
				.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("ThreadPoolKey"))
//设置 threadPool属性
				.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
						.withCoreSize(20)));
}

下面是整理:

  • HystrixCommandProperties
/**
 * Command execution properties.
 */
# 隔离策略,默认是线程隔离,还有信号量隔离,参见枚举:ExecutionIsolationStrategy
hystrix.command.default.execution.isolation.strategy=THREAD
# 隔离线程超时时间,默认1s
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
# 是否启用超时配置
hystrix.command.default.execution.timeout.enabled=true
# 超时的时候是否中断隔离线程
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true
# 隔离线程正在执行取消操作时是否中断
hystrix.command.default.execution.isolation.thread.interruptOnFutureCancel=false
# 隔离策略的最大信号量,只有使用信号量隔离策略时生效
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=10

/**
 * Command fallback properties.HystrixCommand.getFallback()
 */
# 降级方法的最大调用线程数,如果超出此信号量,会抛出异常
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=10
# 是否启用降级
hystrix.command.default.fallback.enabled=true

/**
 * Command circuit breaker properties.
 */
# 是否启用断路器
hystrix.command.default.circuitBreaker.enabled=true
# 请求量阈值,请求量达到该值是会开启断路器
hystrix.command.default.circuitBreaker.requestVolumeThreshold=20
# 当断路器打开后,会直接拒绝请求,此时间是配置多长时候后再次尝试处理请求
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000
# 打开断路器并走回退逻辑的错误率,默认50%
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
# 是否强制打开断路器,打开后会直接拒绝所有请求
hystrix.command.default.circuitBreaker.forceOpen=false
# 是否强制关闭断路器,关闭后会处理所有请求
hystrix.command.default.circuitBreaker.forceClosed=false

/**
 * Command metrics properties.主要用于统计执行情况
 */
# 统计的时间窗口值
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000
# 统计时间窗口内分成的份数,需要保证timeInMilliseconds % numBuckets == 0
hystrix.command.default.metrics.rollingStats.numBuckets=10
# 是否启用百分数统计
hystrix.command.default.metrics.rollingPercentile.enabled=true
# 百分数统计的时间周期
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds=60000
# 百分数统计时间内分成的份数
hystrix.command.default.metrics.rollingPercentile.numBuckets=6
# 百分数统计每份的最大数量。每个bucket只取这个配置数量的执行数来统计
hystrix.command.default.metrics.rollingPercentile.bucketSize=100
# 记录健康快照间隔毫秒数
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds=500

/**
 * Command CommandRequest Context properties.
 */
# 是否启用请求缓存。当HystrixCommand.getCacheKey()调用后,缓存到HystrixRequestCache
hystrix.command.default.requestCache.enabled=true
# 是否启用请求日志记录。HystrixCommand执行或者事件的日志到HystrixRequestLog
hystrix.command.default.requestLog.enabled=true
  • HystrixCollapserProperties
/**
 * Collapser properties.
 */
# 批处理最大请求数,达到该值时就算没有达到时间也会触发批处理,默认值Integer.MAX_VALUE
hystrix.collapser.default.maxRequestsInBatch=0x7fffffff
# 触发批处理的延迟,在触发之前的同样请求可能会放到同一个批处理中
hystrix.collapser.default.timerDelayInMilliseconds=10
# 是否启用请求缓存
hystrix.collapser.default.requestCache.enabled=true
# 统计时间窗口值
hystrix.collapser.default.metrics.rollingStats.timeInMilliseconds=10000
# 统计时间窗口内分成的份数
hystrix.collapser.default.metrics.rollingStats.numBuckets=10
# 是否启用百分数统计
hystrix.collapser.default.metrics.rollingPercentile.enabled=true
# 百分数统计的时间周期
hystrix.collapser.default.metrics.rollingPercentile.timeInMilliseconds=60000
# 百分数统计时间内分成的份数
hystrix.collapser.default.metrics.rollingPercentile.numBuckets=6
# 百分数统计每份的最大数量。每个bucket只取这个配置数量的执行数来统计
hystrix.collapser.default.metrics.rollingPercentile.bucketSize=100
  • HystrixThreadPoolProperties
/**
 * Thread pool properties.
 */
# 是否启用maximumSize配置
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=false
# 线程数量
hystrix.threadpool.default.coreSize=10
# 最大执行线程数
hystrix.threadpool.default.maximumSize=10
# 线程存活毫秒数
hystrix.threadpool.default.keepAliveTimeMinutes=1
# 最大等待线程队列,如果-1为SynchronousQueue;其他则为LinkedBlockingQueue
hystrix.threadpool.default.maxQueueSize=-1
# 拒绝队列大小,即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝。当maxQueueSize为-1,则该属性不可用
hystrix.threadpool.default.queueSizeRejectionThreshold=5
# 线程池统计时间窗口值
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=10000
# 线程池统计时间窗口内分成的份数
hystrix.threadpool.default.metrics.rollingStats.numBuckets=10

猜你喜欢

转载自blog.csdn.net/WYA1993/article/details/82352890