|
|
@@ -62,6 +62,7 @@ import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
|
@@ -98,16 +99,24 @@ public class RestApiUtils {
|
|
|
|
|
|
static {
|
|
|
try {
|
|
|
- //设置协议http和https对应的处理socket链接工厂的对象
|
|
|
+ //1. 设置协议http和https对应的处理socket链接工厂的对象
|
|
|
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
|
.register("http", PlainConnectionSocketFactory.INSTANCE)
|
|
|
.register("https", new SSLConnectionSocketFactory(createIgnoreVerifySSL(), null, null, new DefaultHostnameVerifier()))
|
|
|
.build();
|
|
|
|
|
|
+ //2. Socket层优化
|
|
|
+ SocketConfig socketConfig = SocketConfig.custom()
|
|
|
+ .setTcpNoDelay(true) // 禁用Nagle算法
|
|
|
+ .setSoKeepAlive(true) // 启用TCP KeepAlive
|
|
|
+ .setSoReuseAddress(true)
|
|
|
+ .build();
|
|
|
+
|
|
|
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
|
// 连接池大小
|
|
|
cm.setMaxTotal(220);
|
|
|
cm.setDefaultMaxPerRoute((int)(cm.getMaxTotal() * 0.8));
|
|
|
+ cm.setDefaultSocketConfig(socketConfig);
|
|
|
httpClient = HttpClients.custom()
|
|
|
.setConnectionManager(cm)
|
|
|
.setDefaultCookieStore(cookieStore)
|
|
|
@@ -123,6 +132,8 @@ public class RestApiUtils {
|
|
|
}
|
|
|
})
|
|
|
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, false))
|
|
|
+ .setConnectionManagerShared(false) // 共享连接池
|
|
|
+ .evictIdleConnections(15, TimeUnit.SECONDS) // 定期清理空闲连接
|
|
|
.build();
|
|
|
} catch (Exception e) {
|
|
|
throw new IllegalStateException(e);
|