【问题标题】:Lot of TIME_WAIT connections while using RestTemplate?使用 RestTemplate 时有很多 TIME_WAIT 连接?
【发布时间】:2015-06-05 06:26:01
【问题描述】:

我正在使用 Spring RestTemplate 对我的 RestService 进行 HTTP 调用。我使用的是 Spring Framework 3.2.8 版本的 RestTemplate。我无法升级它,因为在我们公司我们有一个父 POM,我们在其中使用 Spring Framework 3.2.8 版,所以我需要坚持下去。

假设我有两台机器:

  • machineA:这台机器正在运行我的代码,它使用 RestTemplate 作为我的 HttpClient,并且从这台机器上我对在另一台机器 (machineB) 上运行的 RestService 进行 HTTP 调用。我将以下代码封装在多线程应用程序中,以便我可以对客户端代码进行负载和性能测试。
  • machineB:在这台机器上,我正在运行我的 RestService。

现在我看到的问题是每当我在 machineA 上运行负载和性能测试时 - 意思是,我的客户端代码会对在 machineB 上运行的 RestService 进行大量 HTTPClient 调用,因为客户端代码在多线程中被调用方式。

我总是在machineA 上看到很多 TIME_WAIT 连接,如下所示:

   298 ESTABLISHED
    14 LISTEN
     2 SYN_SENT
 10230 TIME_WAIT

  291 ESTABLISHED
   14 LISTEN
    1 SYN_SENT
17767 TIME_WAIT

    285 ESTABLISHED
   14 LISTEN
    1 SYN_SENT
24055 TIME_WAIT

我认为我们这里有很多 TIME_WAIT 连接并不是一个好兆头。 问题陈述:-

  • 这种高 TIME_WAIT 连接在 machineA 上的简单语言中意味着什么?
  • 是否有任何原因导致 RestTemplate 发生这种情况,或者这只是我使用 RestTemplate 的方式?如果我在使用 RestTemplate 的方式上做错了什么,那么正确的使用方法是什么?

在使用 RestTemplate 时是否需要设置任何 keep-alive 标头或 Connection:Close 东西?非常感谢任何输入/建议,因为我对这里发生的事情感到困惑。

下面是我如何在我的代码库中以一种简单的方式使用 RestTemplate(只是为了解释我如何使用 RestTemplate 的整个想法):

public class DataClient implements Client {

    private final RestTemplate restTemplate = new RestTemplate();
    private ExecutorService executor = Executors.newFixedThreadPool(10);

    // for synchronous call
    @Override
    public String getSyncData(DataKey key) {        
        String response = null;
        Future<String> handler = null;
        try {
            handler = getAsyncData(key);
            response = handler.get(100, TimeUnit.MILLISECONDS); // we have a 100 milliseconds timeout value set
        } catch (TimeoutException ex) {
            // log an exception
            handler.cancel(true);
        } catch (Exception ex) {
            // log an exception
        }

        return response;
    }

    // for asynchronous call
    @Override
    public Future<String> getAsyncData(DataKey key) {
        Future<String> future = null;

        try {
            Task task = new Task(key, restTemplate);
            future = executor.submit(task); 
        } catch (Exception ex) {
            // log an exception
        }

        return future;
    }
}

下面是我的简单任务类

class Task implements Callable<String> {

    private final RestTemplate restTemplate;
    private final DataKey key;

    public Task(DataKey key, RestTemplate restTemplate) {
        this.key = key;
        this.restTemplate = restTemplate;
    }

    public String call() throws Exception {
        ResponseEntity<String> response = null;

        String url = "some_url_created_by_using_key";

        // handling all try catch here
        response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);

        return response.getBody();
    }
}

【问题讨论】:

    标签: java spring tcp resttemplate time-wait


    【解决方案1】:

    “TIME_WAIT”是 TCP 连接在关闭后可配置的时间内保持的状态(FIN/FIN 接收)。这样,一个连接的可能“延迟”数据包不能与后一个重用相同端口的连接混合。

    在高流量的测试中,有很多它们是正常的,但它们应该在测试完成几分钟后消失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 2013-09-03
      相关资源
      最近更新 更多