简体   繁体   English

使用RestTemplate时有很多TIME_WAIT连接?

[英]Lot of TIME_WAIT connections while using RestTemplate?

I am using Spring RestTemplate to make a HTTP Calls to my RestService. 我正在使用Spring RestTemplate对我的RestService进行HTTP调用。 I am using spring framework 3.2.8 version of RestTemplate. 我正在使用Spring Framework 3.2.8版本的RestTemplate。 I cannot upgrade this since in our company we have a parent POM in which we are using Spring Framework version 3.2.8 so I need to stick to that. 我不能升级它,因为在我们公司中有一个上级POM,其中我们使用的是Spring Framework版本3.2.8,因此我必须坚持这一点。

Let's say I have two machines: 假设我有两台机器:

  • machineA: This machine is running my code which uses RestTemplate as my HttpClient and from this machine I make HTTP Calls to my RestService which is running on a different machine (machineB). machineA:这台机器正在运行我的代码,该代码使用RestTemplate作为我的HttpClient,并且我从这台机器上对运行在另一台机器(machineB)上的RestService进行HTTP调用。 I have wrapped the below code around multithreaded application so that I can do load and performance testing on my client code. 我将以下代码包装在多线程应用程序周围,以便可以对客户端代码进行负载和性能测试。
  • machineB: On this machine, I am running my RestService. machineB:在这台计算机上,我正在运行RestService。

Now the problem I am seeing is whenever I run a load and performance testing on machineA - Meaning, my client code will make lot of HTTPClient calls to the RestService running on machineB very fast since the client code is getting called in a multithreaded way. 现在,我看到的问题是,每当我在machineA上运行负载和性能测试时-意味着,由于以多线程方式调用了客户端代码,因此我的客户端代码将非常快地对在machineB上运行的RestService进行很多HTTPClient调用。

I always see lot of TIME_WAIT connections on machineA as shown below: 我总是在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

I don't think it's a good sign that we have lot of TIME_WAIT connections here. 我认为我们这里有很多TIME_WAIT连接并不是一个好兆头。 Problem Statement:- 问题陈述:-

  • What does this high TIME_WAIT connection mean here in a simple language on machineA? 在machineA上用简单的语言表示,这种高TIME_WAIT连接意味着什么?
  • Is there any reason why this is happening with RestTemplate or is it just the way I am using RestTemplate? 使用RestTemplate发生这种情况是否有任何原因,还是我使用RestTemplate的方式? If I am doing anything wrong in the way I am using RestTemplate, then what's the right way to use it? 如果我在使用RestTemplate的方式上做任何错误,那么使用它的正确方法是什么?

Do I need to set any keep-alive header or Connection:Close thing while using RestTemplate? 使用RestTemplate时是否需要设置任何keep-alive标头或Connection:Close东西? Any inputs/suggestions are greatly appreciated as I am confuse what's going on here. 非常感谢任何输入/建议,因为我混淆了这里发生的事情。

Below is how I am using RestTemplate in my code base in a simple way (just to explain the whole idea of how I am using RestTemplate): 以下是我以一种简单的方式在代码库中使用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;
    }
}

And below is my simple Task class 下面是我简单的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" is the state that a TCP connection mantains during a configurable amount of time after closed (FIN/FIN reception). “ TIME_WAIT”是TCP连接在关闭后(FIN / FIN接收)后的可配置时间内保持的状态。 In this way, a possible "delayed" packet of one connection can not be mixed with a latter connection that reuses same port. 以此方式,一个连接的可能的“延迟”分组不能与重用相同端口的后一个连接混合。

In a high-traffic test, it is normal to have a lot of them, but they should disappear after a few minutes test finished. 在高流量测试中,有很多是正常的,但是几分钟后测试应该会消失。

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

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