杭州建设企业网站的,2023年楼市将迎来抛售潮,产品设计品牌有哪些,企业网站建立策划书java springboot 使用ES客户端连接
一段时间闲置后#xff0c;首次调用es命令会报SocketTimeOutException问题#xff0c;再次调用不会报错
问题出现原因#xff1a;
Elasticsearch 客户端会根据服务器返回的HTTP报文内容#xff0c;来决定客户端保持HTTP连接Keep-Alive…java springboot 使用ES客户端连接
一段时间闲置后首次调用es命令会报SocketTimeOutException问题再次调用不会报错
问题出现原因
Elasticsearch 客户端会根据服务器返回的HTTP报文内容来决定客户端保持HTTP连接Keep-Alive状态的策略。 如果结果如下,那么保持HTTP连接 Keep-Alive状态为120s
Connection: Keep-Alive
Keep-Alive: max5, timeout 120如果不包含上述内容那么客户端将保持Keep-Alive状态的时间为永久。 事实上Elasticsearch服务器返回的报文并没有上述HTTP头内容所以客户端所有的HTTP连接都为永久保持Keep-Alive。 如果客户端长时间没有发送请求服务器或者防火墙已经close了HTTP底层的TCP链接但是此时客户端并不知道由于Keep Alive是无限期那么并不会重新建立连接而是直接发送请求此时就会得到SocketTimeout异常。
个人解决方案 定期发类似ping的操作 保活
配置方式1
Configuration
public class EsConf {AutowiredLocalProperties localProperties;BeanRestHighLevelClient elasticsearchClient() {ClientConfiguration configuration ClientConfiguration.builder().connectedTo( 210.72.13.245:9200).withHttpClientConfigurer(httpClientBuilder - {// httpclient保活策略httpClientBuilder.setKeepAliveStrategy(((response, context) - Duration.ofMinutes(2).toMillis()));return httpClientBuilder;})//.withConnectTimeout(Duration.ofSeconds(5)).withSocketTimeout(Duration.ofSeconds(600))//.useSsl()//.withDefaultHeaders(defaultHeaders)//.withBasicAuth(username, password)// ... other options.build();RestHighLevelClient client RestClients.create(configuration).rest();return client;}
}配置方式2 Bean(destroyMethod close)public RestHighLevelClient client() {RestClientBuilder builder RestClient.builder(new HttpHost(hostName, Integer.parseInt(port), scheme));builder.setHttpClientConfigCallback(httpClientBuilder - {httpClientBuilder.setKeepAliveStrategy((response, context) - Duration.ofMinutes(3).toMillis());httpClientBuilder.disableAuthCaching();return httpClientBuilder;});return new RestHighLevelClient(builder);}参考文章 https://www.dandelioncloud.cn/article/details/1578623608362856450