服务容错保护(Spring Cloud Hystrix)之断路器原理
源码:
//
//学习资料《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);//断路器是都打开标志,默认falseprivate 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 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();}}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
