Hystrix快速使用篇

1. 什么是Hystrix

In a distributed environment, inevitably some of the many service dependencies will fail. Hystrix is a library that helps you control the interactions between these distributed services by adding latency tolerance and fault tolerance logic. Hystrix does this by isolating points of access between the services, stopping cascading failures across them, and providing fallback options, all of which improve your system’s overall resiliency.
from hystrix官方文檔

2. Hystrix優(yōu)缺點

優(yōu)點

  • 熔斷與恢復(fù):依賴服務(wù)A異常時切換至備份服務(wù)B,A恢復(fù)后自動回切。
  • 異常記錄:觸發(fā)熔斷原因可記錄日志
  • 流量控制:可限制依賴服務(wù)A被調(diào)用頻率
  • 實時監(jiān)控:實時監(jiān)控服務(wù)A狀態(tài)(平均響應(yīng)時間,調(diào)用次數(shù)等)

缺點

  • 代碼復(fù)雜度:引入額外中間件,增加編碼復(fù)雜度
  • 性能損耗:官方文檔標明會損耗1%~5%的服務(wù)器性能(數(shù)據(jù)統(tǒng)計與線程池管理)

3. Spring3.x && 4.x接入Demo Code

  • Pom.xml
    添加Hystrix依賴,core提供核心熔斷功能,javanica提供注解實現(xiàn)方式。
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.5.11</version>
</dependency>
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
    <version>1.5.11</version>
</dependency>
  • Aop
    通過注解實現(xiàn)熔斷,需增加此Aop。
<bean id="hystrixAspect" class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect"></bean>
  • HystrixCommand
    通過注解方式添加熔斷器。
@HystrixCommand(fallbackMethod = "methodAFallback")
public String methodA(String param1,String param2) {...}
  • FallbackMethod
    實現(xiàn)備份方法。
public String methodAFallback(String param1,String param2) {...}

4. Hystrix性能配置

更多性能配置請參考github官方文檔。

    /**
     * <ul>
     * <li>execution.isolation.thread.timeoutInMilliseconds|執(zhí)行超時時間|default:1000</li>
     * <li>circuitBreaker.requestVolumeThreshold|觸發(fā)斷路最低請求數(shù)|default:20</li>
     * <li>circuitBreaker.sleepWindowInMilliseconds|斷路器恢復(fù)時間|default:5000</li>
     * <li>circuitBreaker.errorThresholdPercentage|觸發(fā)短路錯誤率,單位%|default:50</li>
     * <li>coreSize|線程池核心數(shù)|default:10</li>
     * <li>maxQueueSize|隊列長度|default:-1(SynchronousQueue)</li>
     * <li>queueSizeRejectionThreshold|隊滿拒絕服務(wù)閾值|default:5|此值生效優(yōu)先于隊滿</li>
     * <li>metrics.rollingStats.timeInMilliseconds|窗口維持時間|默認10000</li>
     * <li>metrics.rollingPercentile.numBuckets|窗口拆分數(shù)|默認10</li>
     * </ul>
     * 
     * @param e
     * @return
     */
    @Override
    @SuppressWarnings("all")
    @HystrixCommand(fallbackMethod = "getStaticGameServer", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "50") }, threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "10"),
                    @HystrixProperty(name = "maxQueueSize", value = "20"),
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"),
                    @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440") })
    public List<BlhxServer> getBlhxIosGameServer() throws Exception {
        ...
    }

5. HystrixMonitor

  • Pom.xml
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-metrics-event-stream</artifactId>
    <version>1.5.11</version>
</dependency>
  • Web.xml
<servlet>
    <description></description>
    <display-name>HystrixMetricsStreamServlet</display-name>
    <servlet-name>HystrixMetricsStreamServlet</servlet-name>
    <servlet-class>com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HystrixMetricsStreamServlet</servlet-name>
    <url-pattern>/hystrix.stream</url-pattern>
</servlet-mapping>
  • How to test
curl http:ip:port/hystrix.stream
  • monitor
img

img
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容