springcloud Zuul Hystrix Ribbon 超时时间和机制详解

       项目中使用spring cloud框架, 有个接口是导出接口,由于数据量有时会比较大,导致访问的时候后台返回了超时错误,由于对Zuul、Ribbon、Hystrix的超时机制不太了解,花了很长时间修复,趁着闲暇时间跟了下源码把这里理清楚了,做下笔记方便自己消化也希望能帮助到别人。

1、Hystrix 熔断器超时机制

    熔断器的配置类是  HystrixCommandProperties ,我们可以看到如下的一个属性:

private static final Integer default_executionTimeoutInMilliseconds = 1000

    这个是Hystrix的默认超时时间,为1000毫秒 = 1秒 ,如果想更改这个时间,需要在yml文件中使用如下配置key:

# 熔断器的超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 8000

 如果没有通过网关Zuul调用,直接通过Ribbon调用的话,只需要更改如上配置即可(配置Ribbon超时也不起作用的),前提是你为调用的接口加上了熔断器。 

证明: 我有三个服务,A:注册中心 B:提供服务方  C:接口调用方 

         B:提供的服务接口中休眠了15秒 

    /**
     * get 服务接口
     * @param name
     * @return
     */
    @RequestMapping(value = "get_service", method = RequestMethod.GET)
    public String getService(@RequestParam String name){
        try {
            Thread.sleep(15000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello " + name + ", I am spring boot ! and this message from netflix-eureka-client ";
    }

         C: 服务中 consume 接口内部通过ribbon方式调用 B 服务的 get_service 接口,配置文件中配置了熔断器的超时时间为8000毫秒

    /**
     * ribbon get请求远程调用方式一
     * @param xinming
     * @return
     */
    @RequestMapping(value = "consume", method = RequestMethod.GET)
    public String consume1(@RequestParam String xinming){
        String response = consumerService.consume1(xinming);
        return response;
    }
    /**
     * ribbon get请求远程调用方式一
     * @param xinming
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallBackMethod")
    public String consume1(String xinming){
        String response = restTemplate.getForObject("http://netflix-eureka-client/get_service?name={1}", String.class, xniming);
        return response;
    }
    /**
     * 熔断器方法
     * @param xinming
     * @return
     */
    public String fallBackMethod(String xinming){
        return "sorry " + xinming + ", the server is unavailable now, please try later";
    }
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 8000

        接下来进行调用,因为C中休眠了15秒,而B中熔断器配置的超时为8秒,预期结果是8秒后返回熔断器方法的返回值

      接下来我在yml配置文件中增加ribbon的超时,看是否有效果,结果还是8秒后返回熔断器的返回值,ribbon的超时在这里无效。

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 8000
ribbon:
  ConnectTimeout: 1000
  ReadTimeout: 1000

2.在引入了Feign组件以后,Ribbon超时和Hystrix超时将会以两个中小的为最终超时时间:

      第一种情况,没有配置Hystrix超时时间,然而Hystrix的默认超时时间为1秒钟,所以导致配置的ribbon没到就已经超时了:

#hystrix:
#  command:
#    default:
#      execution:
#        isolation:
#          thread:
#            timeoutInMilliseconds: 5000

ribbon:
  ConnectTimeout: 1000
  ReadTimeout: 3000

第二种情况,没有配置Ribbon超时时间:

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 8000

#ribbon:
#  ConnectTimeout: 1000
#  ReadTimeout: 1000
#  MaxAutoRetries: 0
#  MaxAutoRetriesNextServer: 1

这里注释的Ribbon的超时与重试都是默认值,而且Ribbon超时后会触发重发机制:

重试的次数 :RetryCount = (maxAutoRetries + 1) * (maxAutoRetriesNextServer + 1) = 1

所以页面会在 2秒 后进入到断路器

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值