包hystrix-core-1.5.12
类:HystrixCommand继承了AbstractCommand
protected AbstractCommand(HystrixCommandGroupKey group, HystrixCommandKey key, HystrixThreadPoolKey threadPoolKey, HystrixCircuitBreaker circuitBreaker, HystrixThreadPool threadPool, HystrixCommandProperties.Setter commandPropertiesDefaults, HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults, HystrixCommandMetrics metrics, TryableSemaphore fallbackSemaphore, TryableSemaphore executionSemaphore, HystrixPropertiesStrategy propertiesStrategy, HystrixCommandExecutionHook executionHook)
{
this.commandGroup = initGroupKey(group);
this.commandKey = initCommandKey(key, getClass());
this.properties = initCommandProperties(this.commandKey, propertiesStrategy, commandPropertiesDefaults);
this.threadPoolKey = initThreadPoolKey(threadPoolKey, this.commandGroup, (String)this.properties.executionIsolationThreadPoolKeyOverride().get());
this.metrics = initMetrics(metrics, this.commandGroup, this.threadPoolKey, this.commandKey, this.properties);
this.circuitBreaker = initCircuitBreaker(((Boolean)this.properties.circuitBreakerEnabled().get()).booleanValue(), circuitBreaker, this.commandGroup, this.commandKey, this.properties, this.metrics);
this.threadPool = initThreadPool(threadPool, this.threadPoolKey, threadPoolPropertiesDefaults);
this.eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
this.concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
HystrixMetricsPublisherFactory.createOrRetrievePublisherForCommand(this.commandKey, this.commandGroup, this.metrics, this.circuitBreaker, this.properties);
this.executionHook = initExecutionHook(executionHook);
this.requestCache = HystrixRequestCache.getInstance(this.commandKey, this.concurrencyStrategy);
this.currentRequestLog = initRequestLog(((Boolean)this.properties.requestLogEnabled().get()).booleanValue(), this.concurrencyStrategy);
this.fallbackSemaphoreOverride = fallbackSemaphore;
this.executionSemaphoreOverride = executionSemaphore;
}
初始化HystrixCircuitBreaker ,根据CommandKey进行了缓存,如果存在直接取缓存里的key,不存在则新建HystrixCircuitBreakerImpl对象用于熔断操作
private static HystrixCircuitBreaker initCircuitBreaker(boolean enabled, HystrixCircuitBreaker fromConstructor, HystrixCommandGroupKey groupKey, HystrixCommandKey commandKey, HystrixCommandProperties properties, HystrixCommandMetrics metrics)
{
if (enabled)
{
if (fromConstructor == null) {
return HystrixCircuitBreaker.Factory.getInstance(commandKey, groupKey, properties, metrics);
}
return fromConstructor;
}
return new HystrixCircuitBreaker.NoOpCircuitBreaker();
}
protected HystrixCircuitBreakerImpl(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixCommandProperties properties, HystrixCommandMetrics metrics)
{
this.properties = properties;
this.metrics = metrics;
Subscription s = subscribeToStream();
this.activeSubscription.set(s);
}
private Subscription subscribeToStream()
{
this.metrics.getHealthCountsStream().observe().subscribe(new Subscriber()
{
public void onCompleted() {}
public void onError(Throwable e) {}
public void onNext(HystrixCommandMetrics.HealthCounts hc)
{
if (hc.getTotalRequests() >= ((Integer)HystrixCircuitBreaker.HystrixCircuitBreakerImpl.this.properties.circuitBreakerRequestVolumeThreshold().get()).intValue()) {
if (hc.getErrorPercentage() >= ((Integer)HystrixCircuitBreaker.HystrixCircuitBreakerImpl.this.properties.circuitBreakerErrorThresholdPercentage().get()).intValue()) {
if (HystrixCircuitBreaker.HystrixCircuitBreakerImpl.this.status.compareAndSet(HystrixCircuitBreaker.HystrixCircuitBreakerImpl.Status.CLOSED, HystrixCircuitBreaker.HystrixCircuitBreakerImpl.Status.OPEN)) {
HystrixCircuitBreaker.HystrixCircuitBreakerImpl.this.circuitOpened.set(System.currentTimeMillis());
}
}
}
}
});
}