From 2d790a03a3d8fcdcbd42b0845c9e7eaace9162c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Thu, 27 Aug 2020 14:42:50 +0800 Subject: [PATCH] Try to fix nacos server CLOSE_WAIT (#3703) --- .../alibaba/nacos/naming/misc/HttpClient.java | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java index e3dd73694b5..7ce2daf3eaa 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java @@ -465,10 +465,9 @@ public static void asyncHttpGetLarge(String url, Map headers, by * @return {@link HttpResult} as response */ public static HttpResult httpPutLarge(String url, Map headers, byte[] content) { - try { - HttpClientBuilder builder = HttpClients.custom().setUserAgent(UtilsAndCommons.SERVER_VERSION) - .setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); - CloseableHttpClient httpClient = builder.build(); + HttpClientBuilder builder = HttpClients.custom().setUserAgent(UtilsAndCommons.SERVER_VERSION) + .setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); + try (CloseableHttpClient httpClient = builder.build();) { HttpPut httpPut = new HttpPut(url); for (Map.Entry entry : headers.entrySet()) { @@ -498,11 +497,10 @@ public static HttpResult httpPutLarge(String url, Map headers, b * @return {@link HttpResult} as response */ public static HttpResult httpGetLarge(String url, Map headers, String content) { - - try { - HttpClientBuilder builder = HttpClients.custom(); - builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); - builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); + HttpClientBuilder builder = HttpClients.custom(); + builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); + builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); + try (CloseableHttpClient httpClient = builder.build();) { HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity(); httpGetWithEntity.setURI(new URI(url)); @@ -512,7 +510,7 @@ public static HttpResult httpGetLarge(String url, Map headers, S } httpGetWithEntity.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8"))); - CloseableHttpClient httpClient = builder.build(); + HttpResponse response = httpClient.execute(httpGetWithEntity); HttpEntity entity = response.getEntity(); @@ -535,12 +533,11 @@ public static HttpResult httpGetLarge(String url, Map headers, S * @return {@link HttpResult} as response */ public static HttpResult httpPostLarge(String url, Map headers, String content) { - try { - HttpClientBuilder builder = HttpClients.custom(); - builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); - builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); + HttpClientBuilder builder = HttpClients.custom(); + builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); + builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); + try (CloseableHttpClient httpClient = builder.build();) { - CloseableHttpClient httpClient = builder.build(); HttpPost httpost = new HttpPost(url); for (Map.Entry entry : headers.entrySet()) {