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优缺点
优点
- 熔断与恢复:依赖服务A异常时切换至备份服务B,A恢复后自动回切。
- 异常记录:触发熔断原因可记录日志
- 流量控制:可限制依赖服务A被调用频率
- 实时监控:实时监控服务A状态(平均响应时间,调用次数等)
缺点
- 代码复杂度:引入额外中间件,增加编码复杂度
- 性能损耗:官方文档标明会损耗1%~5%的服务器性能(数据统计与线程池管理)
3. Spring3.x && 4.x接入Demo Code
-
Pom.xml
添加Hystrix依赖,core提供核心熔断功能,javanica提供注解实现方式。
<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
通过注解实现熔断,需增加此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
实现备份方法。
public String methodAFallback(String param1,String param2) {...}
4. Hystrix性能配置
更多性能配置请参考github官方文档。
/**
* <ul>
* <li>execution.isolation.thread.timeoutInMilliseconds|执行超时时间|default:1000</li>
* <li>circuitBreaker.requestVolumeThreshold|触发断路最低请求数|default:20</li>
* <li>circuitBreaker.sleepWindowInMilliseconds|断路器恢复时间|default:5000</li>
* <li>circuitBreaker.errorThresholdPercentage|触发短路错误率,单位%|default:50</li>
* <li>coreSize|线程池核心数|default:10</li>
* <li>maxQueueSize|队列长度|default:-1(SynchronousQueue)</li>
* <li>queueSizeRejectionThreshold|队满拒绝服务阈值|default:5|此值生效优先于队满</li>
* <li>metrics.rollingStats.timeInMilliseconds|窗口维持时间|默认10000</li>
* <li>metrics.rollingPercentile.numBuckets|窗口拆分数|默认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