ElasticSearch SocketTimeoutException解决

ElasticSearch SocketTimeoutException解决,第1张

ElasticSearch SocketTimeoutException解决

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

ES版本如下


    org.springframework.data
    spring-data-elasticsearch
    4.2.6

对应ES版本

    org.elasticsearch
    elasticsearch
    7.12.1

异常如下 

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(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();
        }

        
        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;
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5710813.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存