ES用了目前最新的版本,测试环境一直没有发现问题,一到生产就出现了SocketTimeoutException异常,而且很诡异,不是一直报,而是隔一段时间就报一下,谷歌了很久没有找到原因,最后加了一个配置后,就没有再报了
ES版本如下
对应ES版本 org.springframework.data spring-data-elasticsearch4.2.6 org.elasticsearch elasticsearch7.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; } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)