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

【14】分析隔离线程池的线程隔离threadPool.getScheduler的初始化【位于第六步里面】
private static HystrixThreadPool initThreadPool(HystrixThreadPool fromConstructor, HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults) {if (fromConstructor == null) {// get the default implementation of HystrixThreadPool//通过塞入注解信息threadPoolPropertiesDefaults进行初始化return HystrixThreadPool.Factory.getInstance(threadPoolKey, threadPoolPropertiesDefaults);} else {return fromConstructor;}}static HystrixThreadPool getInstance(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties.Setter propertiesBuilder) {// get the key to use instead of using the object itself so that if people forget to implement equals/hashcode things will still workString key = threadPoolKey.name();// this should find it for all but the first timeHystrixThreadPool previouslyCached = threadPools.get(key);if (previouslyCached != null) {return previouslyCached;}// if we get here this is the first time so we need to initializesynchronized (HystrixThreadPool.class) {if (!threadPools.containsKey(key)) {threadPools.put(key, new HystrixThreadPoolDefault(threadPoolKey, propertiesBuilder));}}return threadPools.get(key);}public HystrixThreadPoolDefault(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties.Setter propertiesDefaults) {this.properties = HystrixPropertiesFactory.getThreadPoolProperties(threadPoolKey, propertiesDefaults);HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();this.queueSize = properties.maxQueueSize().get();this.metrics = HystrixThreadPoolMetrics.getInstance(threadPoolKey,concurrencyStrategy.getThreadPool(threadPoolKey, properties),properties);this.threadPool = this.metrics.getThreadPool();this.queue = this.threadPool.getQueue();/* strategy: HystrixMetricsPublisherThreadPool */HystrixMetricsPublisherFactory.createOrRetrievePublisherForThreadPool(threadPoolKey, this.metrics, this.properties);}【15】断路器初始化分析【位于第六步里面】
private static HystrixCircuitBreaker initCircuitBreaker(boolean enabled, HystrixCircuitBreaker fromConstructor,HystrixCommandGroupKey groupKey, HystrixCommandKey commandKey,HystrixCommandProperties properties, HystrixCommandMetrics metrics) {// 如果启用了熔断器if (enabled) {// 若commandKey没有对应的CircuitBreaker,则创建if (fromConstructor == null) {// get the default implementation of HystrixCircuitBreakerreturn HystrixCircuitBreaker.Factory.getInstance(commandKey, groupKey, properties, metrics);} else {// 如果有则返回现有的return fromConstructor;}} else {return new NoOpCircuitBreaker();}}//circuitBreaker以commandKey为维度,每个commandKey都会有对应的circuitBreakerpublic static HystrixCircuitBreaker getInstance(HystrixCommandKey key, HystrixCommandGroupKey group, HystrixCommandProperties properties, HystrixCommandMetrics metrics) {// 如果有则返回现有的, key.name()即command的name作为检索条件HystrixCircuitBreaker previouslyCached = circuitBreakersByCommand.get(key.name());if (previouslyCached != null) {return previouslyCached;}// 如果没有则创建并cacheHystrixCircuitBreaker cbForCommand = circuitBreakersByCommand.putIfAbsent(key.name(), new HystrixCircuitBreakerImpl(key, group, properties, metrics));if (cbForCommand == null) {// this means the putIfAbsent step just created a new one so let's retrieve and return itreturn circuitBreakersByCommand.get(key.name());} else {// this means a race occurred and while attempting to 'put' another one got there before// and we instead retrieved it and will now return itreturn cbForCommand;}}protected HystrixCircuitBreakerImpl(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixCommandProperties properties, HystrixCommandMetrics metrics) {this.properties = properties;this.metrics = metrics;}Hystrix源码分析图

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

文章插图
MetaHolder

推荐阅读