微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用隔离策略作为线程

如何解决使用隔离策略作为线程

从几天开始,我正在尝试修复使用 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 举报,一经查实,本站将立刻删除。