Caused by: java.lang.RuntimeException: Connection is closed at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.translateException (ElasticsearchRestTemplate.java:425) ~[spring-data-elasticsearch-4.3.2.jar:4.3.2] ... 54 common frames omitted
大概就是说:连接已在org.springframework.data.elasticsearch.core处被关闭。
个人觉得是长时间没使用导致连接断开。
解决方法在elasticsearch配置类加上长连接
.setHttpClientConfigCallback(httpClientBuilder ->{
httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom()
.setSoKeepAlive(true)
.build());
return httpClientBuilder;
})
这是我完整的配置类:
@Component
@ConfigurationProperties("es")
@Data
public class ESConfig {
@Value("${es.ip}")
private String ip;
@Value("${es.port}")
private int port;
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(ip,port,"http")
)
//配置长连接setHttpClientConfigCallback 默认 net.ipv4.tcp_keepalive_time = 7200s
.setHttpClientConfigCallback(httpClientBuilder ->{
httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom()
.setSoKeepAlive(true)
.build());
return httpClientBuilder;
})
);
return client;
}
}



