使用隔离策略作为线程

问题描述

从几天开始,我正在尝试修复使用 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 annotationSemaphore 隔离策略的情况下运行上面的代码时,它会在几秒钟内快速响应。但是,如果我使用 Thread 隔离策略,那么它会给出 HystrixTimeoutException

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

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

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)