项目中使用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

前提是你为调用的接口加上了熔断器。 

证明: 我有三个服务,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秒后返回熔断器方法的返回值

springcloud 设置系统时区 springcloud默认超时时间_远程调用

      接下来我在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

springcloud 设置系统时区 springcloud默认超时时间_配置文件_02

第二种情况,没有配置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秒 后进入到断路器

springcloud 设置系统时区 springcloud默认超时时间_springcloud 设置系统时区_03