Just wondering if RestTemplate out of the box uses connection pooling or does it simply establish a new connection each time ?
I believe RestTemplate
doesn’t use a connection pool to send requests, it uses a SimpleClientHttpRequestFactory
that wraps a standard JDK
’s HttpURLConnection
opening and closing the connection.
Indeed you can configure RestTemplate
to use a pooled implementation such as HttpComponentsClientHttpRequestFactory
but most-likely you might also need to configure some settings to prevent requests from timing out.
I have blogged about this issue at Troubleshooting Spring's RestTemplate Requests Timeout
Yes, Spring RestTemplateBuilder
uses Apache HttpClient for pooling (usage).
RestTemplateBuilder
creates HttpComponentsClientHttpRequestFactory
and uses HttpClientBuilder
.
HttpClientBuilder
, by default, sets pool size per route (host) to 5 and total pool size to 10 (source):
s = System.getProperty("http.maxConnections", "5");
int max = Integer.parseInt(s);
poolingmgr.setDefaultMaxPerRoute(max);
poolingmgr.setMaxTotal(2 * max);
To check connection pool logging set logging level as follows:
org.apache.http.impl.conn.PoolingHttpClientConnectionManager=TRACE
PoolingHttpClientConnectionManager
directly (Source). And those values also have been changed over the years ... But as said, Spring Boot uses HttpClientBuilder
which has its own defaults. –
Gorge I believe RestTemplate
doesn’t use a connection pool to send requests, it uses a SimpleClientHttpRequestFactory
that wraps a standard JDK
’s HttpURLConnection
opening and closing the connection.
Indeed you can configure RestTemplate
to use a pooled implementation such as HttpComponentsClientHttpRequestFactory
but most-likely you might also need to configure some settings to prevent requests from timing out.
I have blogged about this issue at Troubleshooting Spring's RestTemplate Requests Timeout
You can create a Bean for RestTemplate
and config there :
@Bean
public RestTemplate restTemplate() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(20);
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectionRequestTimeout(5000) // timeout to get connection from pool
.setSocketTimeout(5000) // standard connection timeout
.setConnectTimeout(5000) // standard connection timeout
.build();
HttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig).build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(requestFactory);
}
And there are a lot config you can do. Refer to https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html
EDIT
If you want to use micrometer metrics you should also use a RestTemplateBuilder for constructing the RestTemplate.
By default RestTemplate creates new Httpconnection every time and closes the connection once done.
If you need to have a connection pooling under rest template then you may use different implementation of the ClientHttpRequestFactory that pools the connections.
new RestTemplate(new HttpComponentsClientHttpRequestFactory())
In case of using Spring Boot configured with Apache HttpClient (having org.apache.httpcomponents:httpclient
library in dependencies)
There is a default connection pool configured by PoolingHttpClientConnectionManager
Default concurrent settings for connections (you can find more about default settings here https://hc.apache.org/httpcomponents-client-4.5.x/current/tutorial/html/connmgmt.html):
PoolingHttpClientConnectionManager maintains a maximum limit of connections on a per route basis and in total. Per default this implementation will create no more than 2 concurrent connections per given route and no more 20 connections in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services.
We can use okhttpclient underneath spring's rest template to use connection pooling. A detailed blog on this below
https://www.bytesville.com/changing-httpclient-in-spring-resttemplate/
© 2022 - 2024 — McMap. All rights reserved.