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)
文章来源:https://www.toymoban.com/news/detail-505239.html
解决方法如下:文章来源地址https://www.toymoban.com/news/detail-505239.html
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;
}
}
}
到了这里,关于ElasticSearch SocketTimeoutException解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!