Hystrix超时设置无效及解决原因

我有一个http接口如下,Hystrix策略设置为线程隔离,超时时间为10秒

@PostMapping("addBatch")
@HystrixCommand(fallbackMethod = "addBatchFallBack", commandProperties = {
  @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
  @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000"),
  @HystrixProperty(name = "execution.timeout.enabled", value = "true")
})
public CommonResult addBatch(@RequestBody List<TblEmployee> employees, @RequestParam(name = "t", required = false) String t) {
  return employeeService.addBatch(employees, t);
}

private CommonResult addBatchFallBack(List<TblEmployee> employees, String t, Throwable throwable) {
  throwable.printStackTrace();
  return new CommonResult(false, "fallback,原因:" + throwable.getMessage(), null);
}

yml文件中hystrix部分的配置如下:

hystrix:
  dashboard:
    proxy-stream-allow-list: localhost
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000   #默认
        timeout:
          enabled: true                    # 必须设置true,否则会交给ribbon
    serverMethod:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000   #配置具体方法的超时时间

但是实际请求发现,1秒左右就自动触发了fallback方法,远远未达到我设置10秒超时的阈值,再次检测配置无误后不仅陷入沉思。

查看异常发现一个关键信息:

超时的异常是由ribbon抛出,而不是hystrix。突然想起来ribbon有自己的超时设置,于是果断调整ribbon的配置

EMPLOYEE:
  ribbon:
    ReadTimeout: 300000 #5分钟
    ConnectTimeout: 300000

再次请求接口,打印的异常如下:

可以发现现在超时的异常是由hystrix抛出,大功告成

原文地址:https://www.cnblogs.com/wugang/p/14497236.html