微服务组件--限流框架Spring Cloud Hystrix分析( 五 )

【10】分析核心applyHystrixSemantics方法
private Observable<R> applyHystrixSemantics(final AbstractCommand<R> _cmd) {//执行命令开始执行的钩子方法 可能有人会问 前面绑定了那么多的钩子方法 这里怎么才开始//start 因为前面绑定但是并没有执行 。当有订阅者订阅 这里才是开始执行的代码逻辑executionHook.onStart(_cmd);//判断断路器是否开启if (circuitBreaker.allowRequest()) {//如果是信号量隔离返回TryableSemaphoreActual 根据设置的并发量来判断是否能执行,如果不能执行,进入fallback 。//如果是线程池隔离 返回TryableSemaphoreNoOp直接返回true没有任何操作final TryableSemaphore executionSemaphore = getExecutionSemaphore();final AtomicBoolean semaphoreHasBeenReleased = new AtomicBoolean(false);final Action0 singleSemaphoreRelease = new Action0() {@Overridepublic void call() {if (semaphoreHasBeenReleased.compareAndSet(false, true)) {executionSemaphore.release();}}};final Action1<Throwable> markExceptionThrown = new Action1<Throwable>() {@Overridepublic void call(Throwable t) {eventNotifier.markEvent(HystrixEventType.EXCEPTION_THROWN, commandKey);}};//判断能否正常执行if (executionSemaphore.tryAcquire()) {try {/* used to track userThreadExecutionTime */executionResult = executionResult.setInvocationStartTime(System.currentTimeMillis());//核心方法return executeCommandAndObserve(_cmd).doOnError(markExceptionThrown).doOnTerminate(singleSemaphoreRelease).doOnUnsubscribe(singleSemaphoreRelease);} catch (RuntimeException e) {return Observable.error(e);}} else {//信号量执行的时候并发太大直接回退return handleSemaphoreRejectionViaFallback();}} else {//执行降级return handleShortCircuitViaFallback();}}//TryableSemaphoreActual类#tryAcquire方法@Overridepublic boolean tryAcquire() {int currentCount = count.incrementAndGet();if (currentCount > numberOfPermits.get()) {count.decrementAndGet();return false;} else {return true;}}//TryableSemaphoreNoOp类#tryAcquire方法@Overridepublic boolean tryAcquire() {return true;}【11】分析allowRequest方法是怎么判断是否允许通过的
@Overridepublic boolean allowRequest() {if (properties.circuitBreakerForceOpen().get()) {// 属性要求我们强制打开电路,这样我们将允许NO请求return false;}if (properties.circuitBreakerForceClosed().get()) {// 我们仍然希望允许isOpen()执行它的计算,因此我们模拟正常的行为isOpen();// 属性要求我们忽略错误,所以我们将忽略isOpen的结果,只允许所有的流量通过return true;}return !isOpen() || allowSingleTest();}@Overridepublic boolean isOpen() {//如果断路器打开立即返回trueif (circuitOpen.get()) {return true;}// we're closed, so let's see if errors have made us so we should trip the circuit openHealthCounts health = metrics.getHealthCounts();// check if we are past the statisticalWindowVolumeThresholdif (health.getTotalRequests() < properties.circuitBreakerRequestVolumeThreshold().get()) {// we are not past the minimum volume threshold for the statisticalWindow so we'll return false immediately and not calculate anythingreturn false;}if (health.getErrorPercentage() < properties.circuitBreakerErrorThresholdPercentage().get()) {return false;} else {// our failure rate is too high, trip the circuitif (circuitOpen.compareAndSet(false, true)) {// if the previousValue was false then we want to set the currentTimecircuitOpenedOrLastTestedTime.set(System.currentTimeMillis());return true;} else {// How could previousValue be true? If another thread was going through this code at the same time a race-condition could have// caused another thread to set it to true already even though we were in the process of doing the same// In this case, we know the circuit is open, so let the other thread set the currentTime and report back that the circuit is openreturn true;}}}public boolean allowSingleTest() {long timeCircuitOpenedOrWasLastTested = circuitOpenedOrLastTestedTime.get();// 1) 如果断路器是打开的// 2) 且已经过了休眠时间,尝试打开if (circuitOpen.get() && System.currentTimeMillis() > timeCircuitOpenedOrWasLastTested + properties.circuitBreakerSleepWindowInMilliseconds().get()) {//已经过了休眠时间,允许一个请求尝试 。//如果成功,断路器被关闭 。if (circuitOpenedOrLastTestedTime.compareAndSet(timeCircuitOpenedOrWasLastTested, System.currentTimeMillis())) {//如果这个返回true,意味着我们设置了时间,因此我们将返回true以允许单次尝试//如果它返回false,这意味着另一个线程在我们之前运行并允许单次尝试return true;}}return false;}

经验总结扩展阅读