簡體   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