Skip to content

Commit

Permalink
feat: support portal restTemplate Client connection pool config
Browse files Browse the repository at this point in the history
  • Loading branch information
youngzil committed Aug 14, 2024
1 parent c28eb49 commit 12c654e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Organization> organizations() {

String organizations = getValue("organizations");
Expand Down

0 comments on commit 12c654e

Please sign in to comment.