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)缺點(diǎn)
優(yōu)點(diǎn)
- 熔斷與恢復(fù):依賴服務(wù)A異常時(shí)切換至備份服務(wù)B贫途,A恢復(fù)后自動(dòng)回切。
- 異常記錄:觸發(fā)熔斷原因可記錄日志
- 流量控制:可限制依賴服務(wù)A被調(diào)用頻率
- 實(shí)時(shí)監(jiān)控:實(shí)時(shí)監(jiān)控服務(wù)A狀態(tài)(平均響應(yīng)時(shí)間,調(diào)用次數(shù)等)
缺點(diǎn)
- 代碼復(fù)雜度:引入額外中間件蹋肮,增加編碼復(fù)雜度
- 性能損耗:官方文檔標(biāo)明會(huì)損耗1%~5%的服務(wù)器性能(數(shù)據(jù)統(tǒng)計(jì)與線程池管理)
3. Spring3.x && 4.x接入Demo Code
-
Pom.xml
添加Hystrix依賴出刷,core提供核心熔斷功能,javanica提供注解實(shí)現(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
通過注解實(shí)現(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
實(shí)現(xiàn)備份方法坷檩。
public String methodAFallback(String param1,String param2) {...}
4. Hystrix性能配置
更多性能配置請(qǐng)參考github官方文檔。
/**
* <ul>
* <li>execution.isolation.thread.timeoutInMilliseconds|執(zhí)行超時(shí)時(shí)間|default:1000</li>
* <li>circuitBreaker.requestVolumeThreshold|觸發(fā)斷路最低請(qǐng)求數(shù)|default:20</li>
* <li>circuitBreaker.sleepWindowInMilliseconds|斷路器恢復(fù)時(shí)間|default:5000</li>
* <li>circuitBreaker.errorThresholdPercentage|觸發(fā)短路錯(cuò)誤率,單位%|default:50</li>
* <li>coreSize|線程池核心數(shù)|default:10</li>
* <li>maxQueueSize|隊(duì)列長度|default:-1(SynchronousQueue)</li>
* <li>queueSizeRejectionThreshold|隊(duì)滿拒絕服務(wù)閾值|default:5|此值生效優(yōu)先于隊(duì)滿</li>
* <li>metrics.rollingStats.timeInMilliseconds|窗口維持時(shí)間|默認(rèn)10000</li>
* <li>metrics.rollingPercentile.numBuckets|窗口拆分?jǐn)?shù)|默認(rèn)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