问题描述:项目集成了ElasticSearch实现全文检索,项目启动可正常进行创建索引、检索等操作,长时间无操作后再次请求ES服务报错远程主机强迫关闭了一个现有的连接。调整ElasticSearchConfig配置,问题解决。
调整前代码:文章来源:https://www.toymoban.com/news/detail-740420.html
package com.dmp.framework.config;
import lombok.Setter;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author daixin
* @version 1.0
* @description: TODO
* @date 2022/11/23 15:08
*/
@Setter
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
public class ElasticSearchConfig {
private String host;
private Integer port;
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(this.host, this.port)));
return client;
}
}
调整后代码:文章来源地址https://www.toymoban.com/news/detail-740420.html
package com.dmp.framework.config;
import lombok.Setter;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
/**
* @author daixin
* @version 1.0
* @description: TODO
* @date 2022/11/23 15:08
*/
@Setter
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
public class ElasticSearchConfig {
private String host;
private Integer port;
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(this.host,this.port,"http")
).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder.setConnectTimeout(90000000)//25hours
.setSocketTimeout(90000000);
}
}).setHttpClientConfigCallback((httpAsyncClientBuilder -> {
httpAsyncClientBuilder.disableAuthCaching();//禁用身份验证缓存
//显式设置keepAliveStrategy
httpAsyncClientBuilder.setKeepAliveStrategy((httpResponse,httpContext) -> TimeUnit.MINUTES.toMillis(3));
//显式开启tcp keepalive
httpAsyncClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(true).build());
return httpAsyncClientBuilder;
}))
);
return client;
}
}
到了这里,关于ElasticSearch远程主机强迫关闭了一个现有的连接的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!