源码:
//
//学习资料《springcloud 微服务实战》《springcloud微服务全栈技术与案例解析》
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.netflix.hystrix;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandMetrics;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixCommandMetrics.HealthCounts;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
public interface HystrixCircuitBreaker {
boolean allowRequest();//每个请求判断是都要执行
boolean isOpen();//判断断路器是都打开
void markSuccess();//关闭断路器 //定义一个什么都不做的断路器实现,
public static class NoOpCircuitBreaker implements HystrixCircuitBreaker {
public NoOpCircuitBreaker() {
}
public boolean allowRequest() {
return true;
}
public boolean isOpen() {
return false;
}
public void markSuccess() {
}
}
public static class HystrixCircuitBreakerImpl implements HystrixCircuitBreaker {
private final HystrixCommandProperties properties;//实例的属性对象
private final HystrixCommandMetrics metrics;//记录各类度量指标对象
private AtomicBoolean circuitOpen = new AtomicBoolean(false);//断路器是都打开标志,默认false
private AtomicLong circuitOpenedOrLastTestedTime = new AtomicLong();//上一次的时间戳
protected HystrixCircuitBreakerImpl(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixCommandProperties properties, HystrixCommandMetrics metrics) {
this.properties = properties;
this.metrics = metrics;
}
//用于在半开路状态下,如果成功就将断路器关闭,并重置指标对象
public void markSuccess() {
if(this.circuitOpen.get() && this.circuitOpen.compareAndSet(true, false)) {
this.metrics.resetStream();
}
}
public boolean allowRequest() { //先根据配置文件有没有配置,默认情况根据
if(((Boolean)this.properties.circuitBreakerForceOpen().get()).booleanValue()) {
return false;
} else if(((Boolean)this.properties.circuitBreakerForceClosed().get()).booleanValue()) {
this.isOpen();
return true;
} else {
//默认情况下判断
return !this.isOpen() || this.allowSingleTest();
}
}
public boolean allowSingleTest() {
long timeCircuitOpenedOrWasLastTested = this.circuitOpenedOrLastTestedTime.get();
return this.circuitOpen.get() && System.currentTimeMillis() > timeCircuitOpenedOrWasLastTested + (long)((Integer)this.properties.circuitBreakerSleepWindowInMilliseconds().get()).intValue() && this.circuitOpenedOrLastTestedTime.compareAndSet(timeCircuitOpenedOrWasLastTested, System.currentTimeMillis());
}
public boolean isOpen() {
if(this.circuitOpen.get()) {
return true; //如果开启就直接返回ture
} else { //否则根据health一些健康指标进行判断是否开启
HealthCounts health = this.metrics.getHealthCounts();
if(health.getTotalRequests() < (long)((Integer)this.properties.circuitBreakerRequestVolumeThreshold().get()).intValue()) {
return false;
} else if(health.getErrorPercentage() < ((Integer)this.properties.circuitBreakerErrorThresholdPercentage().get()).intValue()) {
return false;
} else if(this.circuitOpen.compareAndSet(false, true)) {
this.circuitOpenedOrLastTestedTime.set(System.currentTimeMillis());
return true;
} else {
return true;
}
}
}
}
public static class Factory { //静态工厂记录保存Hystrix与key的对应关系保存在map中
private static ConcurrentHashMap<String, HystrixCircuitBreaker> circuitBreakersByCommand = new ConcurrentHashMap();
public Factory() {
}
public static HystrixCircuitBreaker getInstance(HystrixCommandKey key, HystrixCommandGroupKey group, HystrixCommandProperties properties, HystrixCommandMetrics metrics) {
HystrixCircuitBreaker previouslyCached = (HystrixCircuitBreaker)circuitBreakersByCommand.get(key.name());
if(previouslyCached != null) {
return previouslyCached;
} else {
HystrixCircuitBreaker cbForCommand = (HystrixCircuitBreaker)circuitBreakersByCommand.putIfAbsent(key.name(), new HystrixCircuitBreaker.HystrixCircuitBreakerImpl(key, group, properties, metrics));
return cbForCommand == null?(HystrixCircuitBreaker)circuitBreakersByCommand.get(key.name()):cbForCommand;
}
}
public static HystrixCircuitBreaker getInstance(HystrixCommandKey key) {
return (HystrixCircuitBreaker)circuitBreakersByCommand.get(key.name());
}
static void reset() {
circuitBreakersByCommand.clear();
}
}
}