[英]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: 假设我有两台机器:
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:-
问题陈述:-
TIME_WAIT
connection mean here in a simple language on machineA? TIME_WAIT
连接意味着什么? 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.