我有一個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拋出,大功告成