hystrix源码初识之线程池隔离
本篇博客只讲述如下内容:
- hystrixCommand的初始化过程
- hystrix的线程池部分的初始化
一个DubboHystrixCommand构造过程如下
public DubboHystrixCommand(Invoker invoker, Invocation invocation) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(invoker.getInterface().getName()))
.andCommandKey(HystrixCommandKey.Factory.asKey(String.format("%s_%d", invocation.getMethodName(),
invocation.getArguments() == null ? 0 : invocation.getArguments().length)))
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(300)// 10秒钟内至少19此请求失败,熔断器才发挥起作用
.withCircuitBreakerSleepWindowInMilliseconds(1000)// 熔断器中断请求1秒后会进入半打开状态,放部分流量过去重试
.withCircuitBreakerErrorThresholdPercentage(50)// 错误率达到50开启熔断保护
.withExecutionTimeoutEnabled(false))// 使用dubbo的超时,禁用这里的超时
.andThreadPoolPropertiesDefaults(
HystrixThreadPoolProperties.Setter().withCoreSize(getThreadPoolCoreSize(invoker.getUrl()))
.withMaxQueueSize(1000).withQueueSizeRejectionThreshold(700)));// 线程池为30
this.invoker = invoker;
this.invocation = invocation;
}
setter为HystrixCommand的一个静态内部类:
实际调用的构造方法为:
/**
* Construct a {
@link HystrixCommand} with defined {
@link Setter} that allows injecting property and strategy overrides and other optional arguments.
* <p>
* NOTE: The {
@link HystrixCommandKey} is used to associate a {
@link HystrixCommand} with {
@link HystrixCircuitBreaker}, {
@link HystrixCommandMetrics} and other objects.
* <p>
* Do not create multiple {
@link HystrixCommand} implementations with the same {
@link HystrixCommandKey} but different injected default properties as the first instantiated will win.
* <p>
* Properties passed in via {
@link Setter#andCommandPropertiesDefaults} or {@link Setter#andThreadPoolPropertiesDefaults} are cached for the given {@link HystrixCommandKey} for the life of the JVM
* or until {
@link Hystrix#reset()} is called. Dynamic properties allow runtime changes. Read more on the <a href="https://github.com/Netflix/Hystrix/wiki/Configuration">Hystrix Wiki</a>.
*
*