Environment: SpringBoot 2.7.10, Elasticsearch, Java 11.
I'm using springboot autoconfigured org.elasticsearch.client.RestHighLevelClient, it uses httpclient's connection pool to send requests.
The maximum number of connections in the connection pool is fixed at 30 and is not configurable. When it accesses an elasticsearch cluster with more than 30 nodes, the connection cannot be reused correctly. It leads to too many connections in the TIME_WAIT state and increase server load.
My temporary solution is to use java reflection to modify the maximum number of connections in the connection pool:
@Configuration
public class ElasticsearchConfig {
ElasticsearchConfig(RestHighLevelClient restHighLevelClient) throws Exception {
HttpAsyncClient httpClient = restHighLevelClient.getLowLevelClient().getHttpClient();
Field field = httpClient.getClass().getDeclaredField("connmgr");
field.setAccessible(true);
PoolingNHttpClientConnectionManager mgr = (PoolingNHttpClientConnectionManager) field.get(httpClient);
mgr.setMaxTotal(100);
}
}
I want to know why there is no configurable elasticsearch client connection pool provided. It is obvious that large-scale elasticsearch clusters have to adjust the connection pool size as needed.
Will you support it in future versions? Thanks.
Comment From: wilkinsona
OSS support for Spring Boot 2.7.x is no longer available. Please upgrade to Spring Boot 3.1.x or later. Also, please note that Spring Boot 3.x no longer supports the high-level REST client with support for co.elastic.clients:elasticsearch-java and org.elasticsearch.client:elasticsearch-rest-client being provided instead.
If you're stuck on 2.7.x, you can use a RestClientBuilderCustomizer and its customize(HttpAsyncClientBuilder builder) method to customize the underlying HTTP client to meet your needs.
Comment From: Sycamore-M
Limited by the existing ES cluster version, upgrading is difficult.
Thank you for your guidance, I will try to use RestClientBuilderCustomizer.