ElasticSearch SocketTimeoutException解决

ElasticSearch SocketTimeoutException解决ES 用了目前最新的版本 测试环境一直没有发现问题 一到生产就出现了 SocketTimeou 异常 而且很诡异 不是一直报 而是隔一段时间就报一下 谷歌了很久没有找到原因 最后加了一个配置后 就没有再报了 ES 版本如下 dependency dependency

大家好,我是讯享网,很高兴认识大家。

ES用了目前最新的版本,测试环境一直没有发现问题,一到生产就出现了SocketTimeoutException异常,而且很诡异,不是一直报,而是隔一段时间就报一下,谷歌了很久没有找到原因,最后加了一个配置后,就没有再报了

ES版本如下

<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>4.2.6</version> </dependency> 对应ES版本 <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.12.1</version> </dependency>

讯享网

异常如下 


讯享网

讯享网Caused by: java.net.SocketTimeoutException: 30,000 milliseconds timeout on connection http-outgoing-19 [ACTIVE] at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:865) at org.elasticsearch.client.RestClient.performRequest(RestClient.java:283) at org.elasticsearch.client.RestClient.performRequest(RestClient.java:286) at org.elasticsearch.client.RestClient.performRequest(RestClient.java:270) at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1654) at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1624) at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1594) at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:1110) at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.lambda$searchScrollStart$12(ElasticsearchRestTemplate.java:311) at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.execute(ElasticsearchRestTemplate.java:383)

解决方法如下:

import org.apache.http.HttpResponse; import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy; import org.apache.http.protocol.HttpContext; import org.elasticsearch.client.RestClientBuilder; import org.springframework.boot.autoconfigure.elasticsearch.RestClientBuilderCustomizer; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @Component public class MyRestClientBuilderCustomizer implements RestClientBuilderCustomizer { @Override public void customize(RestClientBuilder builder) { // keep alive策略 builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setKeepAliveStrategy(CustomConnectionKeepAliveStrategy.INSTANCE)); } public static class CustomConnectionKeepAliveStrategy extends DefaultConnectionKeepAliveStrategy { public static final CustomConnectionKeepAliveStrategy INSTANCE = new CustomConnectionKeepAliveStrategy(); private CustomConnectionKeepAliveStrategy() { super(); } / * 最大keep alive的时间(分钟) * 这里默认为10分钟,可以根据实际情况设置。可以观察客户端机器状态为TIME_WAIT的TCP连接数,如果太多,可以增大此值。 */ private final long MAX_KEEP_ALIVE_MINUTES = 10; @Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { long keepAliveDuration = super.getKeepAliveDuration(response, context); // <0 为无限期keepalive // 将无限期替换成一个默认的时间 if(keepAliveDuration < 0){ return TimeUnit.MINUTES.toMillis(MAX_KEEP_ALIVE_MINUTES); } return keepAliveDuration; } } }

小讯
上一篇 2025-02-27 16:37
下一篇 2025-01-07 09:36

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/63708.html