From 12c654e27b9d8a6460ef3ca047f71604ff200f68 Mon Sep 17 00:00:00 2001 From: yangzl Date: Wed, 14 Aug 2024 14:51:30 +0800 Subject: [PATCH] feat: support portal restTemplate Client connection pool config --- .../apollo/portal/component/RestTemplateFactory.java | 7 +++++++ .../apollo/portal/component/config/PortalConfig.java | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/RestTemplateFactory.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/RestTemplateFactory.java index de3d206107c..b9b70faa7b1 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/RestTemplateFactory.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/RestTemplateFactory.java @@ -20,6 +20,7 @@ import com.ctrip.framework.apollo.portal.component.config.PortalConfig; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; @@ -59,8 +60,14 @@ public boolean isSingleton() { } public void afterPropertiesSet() throws UnsupportedEncodingException { + + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); + connectionManager.setMaxTotal(portalConfig.connectPoolMaxTotal()); //最大连接数 + connectionManager.setDefaultMaxPerRoute(portalConfig.connectPoolMaxPerRoute()); //每个路由(域名)最大连接数 + CloseableHttpClient httpClient = HttpClientBuilder.create() .setConnectionTimeToLive(portalConfig.connectionTimeToLive(), TimeUnit.MILLISECONDS) + .setConnectionManager(connectionManager) .build(); restTemplate = new RestTemplate(httpMessageConverters.getConverters()); diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java index 9548da9c05a..93b9e93daf2 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java @@ -173,6 +173,14 @@ public int connectionTimeToLive() { return getIntProperty("api.connectionTimeToLive", -1); } + public int connectPoolMaxTotal() { + return getIntProperty("api.pool.max.total", 20); + } + + public int connectPoolMaxPerRoute() { + return getIntProperty("api.pool.max.per.route", 2); + } + public List organizations() { String organizations = getValue("organizations");