Hystrix快速使用篇

2017-06-01  本文已影响344人  野餐先生

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优缺点

优点

缺点

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

<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>
<bean id="hystrixAspect" class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect"></bean>
@HystrixCommand(fallbackMethod = "methodAFallback")
public String methodA(String param1,String param2) {...}
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

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-metrics-event-stream</artifactId>
    <version>1.5.11</version>
</dependency>
<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>
curl http:ip:port/hystrix.stream
img
img
上一篇 下一篇

猜你喜欢

热点阅读