Hystrix 简单源码解析 6.27日

包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());
              }
            }
          }
        }
      });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值