spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.19k stars 40.69k forks source link

Non-configurable elasticsearch client connection pool #40832

Closed Sycamore-M closed 5 months ago

Sycamore-M commented 5 months ago

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.

wilkinsona commented 5 months ago

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.

Sycamore-M commented 5 months ago

Limited by the existing ES cluster version, upgrading is difficult.

Thank you for your guidance, I will try to use RestClientBuilderCustomizer.