繁体   English   中英

使用RestTemplate时有很多TIME_WAIT连接?

[英]Lot of TIME_WAIT connections while using RestTemplate?

我正在使用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连接并不是一个好兆头。 问题陈述:-

  • 在machineA上用简单的语言表示,这种高TIME_WAIT连接意味着什么?
  • 使用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;
    }
}

下面是我简单的Task类

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM