天天看點

hystrix 1.5.13 學習筆記hystrix 1.5.13 學習筆記

hystrix 1.5.13 學習筆記

概述

  • 參考
    • https://segmentfault.com/a/1190000005988895
    • http://www.infoq.com/cn/news/2013/01/netflix-hystrix-fault-tolerance
    • http://www.sczyh30.com/posts/Microservice/netflix-hystrix-how-it-works-summary/
    • 用艙壁隔離的方式進行資源隔離,利用熔斷器進行鍊路的保護與恢複

HystrixThreadPool—->資源隔離

  • HystrixThreadPoolDefault—->HystrixThreadPool

HystrixCircuitBreaker—->熔斷器

  • HystrixCircuitBreakerImpl—->HystrixCircuitBreaker
    • com.netflix.hystrix.HystrixCircuitBreaker.HystrixCircuitBreakerImpl#attemptExecution—->指令根據此方法判斷是否應該執行
      • 基于circuitBreakerForceOpen(false)/default_circuitBreakerForceClosed(false)
      • circuitOpened—->熔斷打開時間(-1)
      • isAfterSleepWindow—->是否是在熔斷的休眠時間後,基于currentTime > circuitOpenTime + sleepWindowTime(circuitBreakerSleepWindowInMilliseconds—-5000ms)
      • 置熔斷狀态為半開狀态status.compareAndSet(Status.OPEN, Status.HALF_OPEN)
    • com.netflix.hystrix.HystrixCircuitBreaker.HystrixCircuitBreakerImpl#subscribeToStream
      • 當達到設定的閥值時,status.compareAndSet(Status.CLOSED, Status.OPEN)
  • NoOpCircuitBreaker—->HystrixCircuitBreaker—–>關閉熔斷支援
  • 熔斷器三種狀态—->CLOSED, OPEN, HALF_OPEN

HystrixCommand—->指令模式

  • HystrixCommand—->AbstractCommand
  • terminateCommandCleanup—->完成清理動作
  • unsubscribeCommandCleanup—->完成從status.compareAndSet(Status.HALF_OPEN, Status.OPEN)的轉化
  • com.netflix.hystrix.AbstractCommand#applyHystrixSemantics—->核心流程方法
    • com.netflix.hystrix.AbstractCommand#executeCommandAndObserve
    • com.netflix.hystrix.AbstractCommand#executeCommandWithSpecifiedIsolation
    • 正常回調
      • com.netflix.hystrix.AbstractCommand#getUserExecutionObservable—->調用HystrixCommand的方法,進行回調業務系統
      • com.netflix.hystrix.HystrixCommand#getExecutionObservable—->回調使用者定義的業務run方法
    • 異常回調
      • com.netflix.hystrix.AbstractCommand#handleShortCircuitViaFallback
      • com.netflix.hystrix.AbstractCommand#getFallbackOrThrowException
      • com.netflix.hystrix.HystrixCommand#getFallbackObservable—->回調使用者定義的業務異常getFallback方法

HystrixKeyDefault—->HystrixKey—->key支援(String name)

  • HystrixThreadPoolKey—->利用intern存放以name為key,HystrixThreadPoolKeyDefault為值的Map
  • HystrixCommandKey—->利用intern存放以name為key,HystrixCommandKeyDefault為值的Map
  • HystrixCommandGroupKey—->利用intern存放以name為key,HystrixCommandGroupDefault為值的Map
  • HystrixCollapserKey—->利用intern存放以name為key,HystrixCollapserKeyDefault為值的Map

屬性支援—->例如指令屬性、線程池屬性

  • HystrixThreadPoolProperties
  • HystrixCommandProperties
  • HystrixCollapserProperties

HystrixMetrics—->例如名額資訊

  • HystrixThreadPoolMetrics—->HystrixMetrics—->名額資訊
  • HystrixCommandMetrics—->HystrixMetrics
  • HystrixCollapserMetrics—->HystrixMetrics

測試

  • com.netflix.hystrix.examples.demo.HystrixCommandDemo