Spring boot Rest模板使用hystrix包装时需要花费大量时间,隔离策略为线程
从几天开始,我一直在尝试修复一个HystrixTimeoutException,这是我在使用Spring的restemplate调用远程服务时遇到的 因此,我有一个Spring boot Rest模板使用hystrix包装时需要花费大量时间,隔离策略为线程,spring-boot,spring-mvc,hystrix,spring-resttemplate,Spring Boot,Spring Mvc,Hystrix,Spring Resttemplate,从几天开始,我一直在尝试修复一个HystrixTimeoutException,这是我在使用Spring的restemplate调用远程服务时遇到的 因此,我有一个EmployeeServiceProxy类,它具有getEmployeesInfo方法,该方法使用RestTemplate进行远程服务调用,并用HystrixCommand包装。 例如 @HystrixCommand(commandKey=“employee bulk”, groupKey=“员工批量组”, threadPoolKey
EmployeeServiceProxy
类,它具有getEmployeesInfo
方法,该方法使用RestTemplate
进行远程服务调用,并用HystrixCommand
包装。
例如
@HystrixCommand(commandKey=“employee bulk”,
groupKey=“员工批量组”,
threadPoolKey=“员工批量线程池”,
fallbackMethod=“employeeFallback”,commandProperties={
@物理性质(
name=“execution.isolation.thread.timeoutines”,
value=“5000”)
})
公共列表getEmployeesInfo(列出EMPID){
LOGGER.info(“内部Hystrix getEmployeesInfo”);
EmployeeRequest请求=新EmployeeRequest(empIds);
字符串url=buildEmployeeFetchUrl();
LOGGER.info(“调用远程服务”);
长启动=System.currentTimeMillis();
EmployeeResult结果=empRestTemplate.postForObject(url、请求、EmployeeResult.class);
info(“远程服务调用已完成:{}”,(System.currentTimeMillis()-start));
return(result==null)?Collections.emptyList():result.getEmployees();
}
公共列表员工后备(列表员工ID,可丢弃的e){
LOGGER.warn(“访问员工服务时出现Hystrix异常”,e);
返回集合。emptyList();
}
而且,我正在调用上面的getEmployeesInfo
,它来自循环中的另一个类,一次调用100个员工ID。
现在,employee fetch api(上面使用)的平均响应时间(real)为<1秒
当我在没有Hystrix注释
或使用信号量
隔离策略的情况下运行上述代码时,它会在几秒钟内快速响应。但是,如果我使用Thread
隔离策略,那么它会给出HystrixTimeoutException
我曾尝试添加一些带有计时信息的日志,如调用getEmployeesInfo,在Hystrix getEmployeesInfo
内部等,发现方法getEmployeesInfo
在几毫秒内立即被调用,但resttemplate大约需要1分钟的时间
注意,我已经为resttemplate配置了readtimeout=2000&connectTimeout=500。
我在过去的项目中也使用过Hystrix,但没有遇到这种行为
感谢您对如何解决此问题的帮助
@HystrixCommand(commandKey = "employee-bulk",
groupKey = "employee-bulk-group",
threadPoolKey = "employee-bulk-thread-pool",
fallbackMethod = "employeeFallback", commandProperties = {
@HystrixProperty(
name="execution.isolation.thread.timeoutInMilliseconds",
value="5000")
})
public List<Employee> getEmployeesInfo(List<String> empIds) {
LOGGER.info("inside Hystrix getEmployeesInfo");
EmployeeRequest request = new EmployeeRequest(empIds);
String url = buildEmployeeFetchUrl();
LOGGER.info("invoking remote service");
long start = System.currentTimeMillis();
EmployeeResult result = empRestTemplate.postForObject(url, request, EmployeeResult.class);
LOGGER.info("remote service invocation took : {}", (System.currentTimeMillis() - start));
return (result == null) ? Collections.emptyList() : result.getEmployees();
}
public List<Employee> employeeFallback(List<String> empIds, Throwable e) {
LOGGER.warn("Hystrix Exception in accessing employee service ", e);
return Collections.emptyList();
}