0

从几天开始,我正在尝试修复在使用 Spring 的 RestTemplate 调用远程服务时遇到的 HystrixTimeoutException。

所以,我有一个EmployeeServiceProxy类,该类具有getEmployeesInfo使用 进行远程服务调用RestTemplate并用HystrixCommand. 例如

    @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();
    }

而且,我getEmployeesInfo一次从另一个类中调用 100 个员工 ID。现在,实际员工获取 api(上面使用)的平均响应时间是< 1 sec.

当我在没有Hystrix annotation或使用Semaphore隔离策略的情况下运行上面的代码时,它会在几秒钟内快速响应。但是,如果我使用Thread隔离策略,那么它会给出HystrixTimeoutException.

我尝试添加一些带有时间信息的日志,例如calling getEmployeesInfoinside Hystrix getEmployeesInfo等。并且,发现该方法getEmployeesInfo在几毫秒内立即被调用,但是 resttemplate 需要大约 1 分钟的时间。

请注意,我已经为 resttemplate 配置了 readtimeout=2000 & connectTimeout=500。我在过去的项目中也使用过 Hystrix,但没有遇到这种行为。

感谢有关如何解决此问题的任何帮助。

4

0 回答 0