Hystrix基本使用

来源:互联网 发布:百度旅游预测数据 编辑:程序博客网 时间:2024/06/07 07:04
0,User Guid
https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#fallback
1,需要AOP引入
  1. import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.EnableAspectJAutoProxy;
  5. /**
  6. * HystrixCommandAop
  7. * Created by jinglongjun on 16/9/19.
  8. */
  9. @Configuration
  10. @EnableAspectJAutoProxy
  11. public class HystrixCommandAop {
  12. @Bean
  13. public HystrixCommandAspect hystrixAspect() {
  14. return new HystrixCommandAspect();
  15. }
  16. }


2,需要在外部调用方法加入注解

  1. @HystrixCommand(commandProperties = {
  2. @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
  3. }, fallbackMethod = "defaultCountOfTaobao")
  4. public TradeCount countOfTaobao(User user, TradeCount tc, Boolean isBindUser) {
  5. try {
  6. Thread.sleep(2000);
  7. } catch (InterruptedException e) {
  8. e.printStackTrace();
  9. }
  10. }
注意此方法必须是外部调用,如果是内部调用,则会导致方法无效。因为是通过命令模式的切面进入的,service内部methoda调用methodb无法对methodb加入切片.

加入缓存等注解初步测试会对缓存产生影响。

3,配置说明

  1. @HystrixCommand(groupKey="UserGroup", commandKey = "GetUserByIdCommand"
  2. commandProperties = {
  3. @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
  4. },
  5. threadPoolProperties = {
  6. @HystrixProperty(name = "coreSize", value = "30"),
  7. @HystrixProperty(name = "maxQueueSize", value = "101"),
  8. @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
  9. @HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"),
  10. @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
  11. @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
  12. })

参数
作用
备注

groupKey

表示所属的group,一个group共用线程池

默认值:getClass().getSimpleName();

commandKey

 默认值:当前执行方法名

execution.isolation.strategy

隔离策略,有THREAD和SEMAPHORE

默认使用THREAD模式,以下几种可以使用SEMAPHORE模式:

  • 只想控制并发度
  • 外部的方法已经做了线程隔离
  • 调用的是本地方法或者可靠度非常高、耗时特别小的方法(如medis)

execution.isolation.thread.timeoutInMilliseconds

 

超时时间

默认值:1000

在THREAD模式下,达到超时时间,可以中断

在SEMAPHORE模式下,会等待执行完成后,再去判断是否超时

设置标准:

有retry,99meantime+avg meantime

没有retry,99.5meantime

execution.timeout.enabled

是否打开超时 

execution.isolation.thread.interruptOnTimeout

是否打开超时线程中断THREAD模式有效

execution.isolation.semaphore.maxConcurrentRequests

信号量最大并发度SEMAPHORE模式有效,默认值:10

fallback.isolation.semaphore.maxConcurrentRequests

fallback最大并发度默认值:10

circuitBreaker.requestVolumeThreshold

熔断触发的最小个数/10s默认值:20

circuitBreaker.sleepWindowInMilliseconds

熔断多少秒后去尝试请求默认值:5000

circuitBreaker.errorThresholdPercentage

失败率达到多少百分比后熔断

默认值:50

主要根据依赖重要性进行调整

circuitBreaker.forceClosed

是否强制关闭熔断如果是强依赖,应该设置为true

coreSize

线程池coreSize

默认值:10

设置标准:qps*99meantime+breathing room

maxQueueSize

请求等待队列

默认值:-1

如果使用正数,队列将从SynchronizeQueue改为LinkedBlockingQueue


0 0