(1048条消息) ES异常:Connection reset by peer_浊酒入清梦的博客-CSDN博客https://blog.csdn.net/m0_37862405/article/details/108324096

ES High Level Rest Client 超时问题排查及解决 - 墨天轮 (modb.pro)https://www.modb.pro/db/388569记一次elasticsearch client的conncection reset 异常 - 小专栏 (xiaozhuanlan.com)https://xiaozhuanlan.com/topic/7350912846(1049条消息) Es 超时设置 high-level-client_小檗的博客-CSDN博客_es 设置超时时间https://blog.csdn.net/xiaobozhi1993/article/details/114260115

ES的high level查询超时设置失效问题 - 简书 (jianshu.com)https://www.jianshu.com/p/f781e38b3bb8

问题描述
es7.4.1客户端查询时报异常:

Caused by: java.io.IOException: Connection reset by peer
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:793)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:218)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:205)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1454)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1424)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1394)
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:930)
...
Caused by: java.io.IOException: Connection reset by peer
at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
at sun.nio.ch.IOUtil.read(IOUtil.java:197)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
at org.apache.http.impl.nio.reactor.SessionInputBufferImpl.fill(SessionInputBufferImpl.java:231)
...

原因排查

es7.4.1客户端(RestHighLevelClient)使用的apache httpclient 版本为4.1.3,keepAlive默认为-1(即无限)。在某些特殊情况下,ES服务端的keepAlive短于ES客户端的keepAlive,进而导致:ES服务端已经关闭了连接,但是客户端还继续复用该连接,从而抛出上述异常。

解决方法

手动设置RestHighLevelClient的keepAlive,通过KeepAliveStrategy手动配置keepAlive代码如下:

public static RestHighLevelClient createRestHighLevelClient(String esUrl, Long keepAlive) {RestClientBuilder clientBuilder = RestClient.builder(createHttpHost(URI.create(esUrl))).setHttpClientConfigCallback(requestConfig -> requestConfig.setKeepAliveStrategy((response, context) -> keepAlive));return new RestHighLevelClient(clientBuilder);
}private static HttpHost createHttpHost(URI uri) {if (StringUtils.isEmpty(uri.getUserInfo())) {return HttpHost.create(uri.toString());}try {return HttpHost.create(new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(),uri.getQuery(), uri.getFragment()).toString());} catch (URISyntaxException ex) {throw new IllegalStateException(ex);}}

源码可参考

原理

RestHighLevelClient设置http配置的源码:

public RestClientBuilder setHttpClientConfigCallback(HttpClientConfigCallback httpClientConfigCallback) {Objects.requireNonNull(httpClientConfigCallback, "httpClientConfigCallback must not be null");this.httpClientConfigCallback = httpClientConfigCallback;return this;
}public interface HttpClientConfigCallback {HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder);}

根据HttpAsyncClientBuilder中的部分源码可知,RestHighLevelClient默认keepAliveStrategy为DefaultConnectionKeepAliveStrategy:

ConnectionKeepAliveStrategy keepAliveStrategy = this.keepAliveStrategy;
if (keepAliveStrategy == null) {keepAliveStrategy = DefaultConnectionKeepAliveStrategy.INSTANCE;
}

DefaultConnectionKeepAliveStrategy源码,如果没有在http表头中设置,默认返回-1:

@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class DefaultConnectionKeepAliveStrategy implements ConnectionKeepAliveStrategy {public static final DefaultConnectionKeepAliveStrategy INSTANCE = new DefaultConnectionKeepAliveStrategy();@Overridepublic long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {Args.notNull(response, "HTTP response");final HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));while (it.hasNext()) {final HeaderElement he = it.nextElement();final String param = he.getName();final String value = he.getValue();if (value != null && param.equalsIgnoreCase("timeout")) {try {return Long.parseLong(value) * 1000;} catch(final NumberFormatException ignore) {}}}return -1;}}

参考文档:

https://juejin.im/post/6844904069442568205

https://stackoverflow.com/questions/52997697/how-to-get-around-connection-reset-by-peer-when-using-elasticsearchs-restclie

ES High Level Rest Client 超时问题排查及解决相关推荐

  1. 一次redis诡异的连接超时问题排查与解决

    文章目录 1 问题分析 2 难道是zone_reclaim_mode? 2.1 NUMA是什么? 2.2 zone_reclaim_mode=0管用么? 3 内存回收 3.1 内存回收的触发路径(mi ...

  2. Elasticsearch教程(4) High Level REST Client API 查询 聚合 分组

    ES High Level REST Client API 查询 聚合 1 准备数据 1.1 插入测试数据 2 Maven引入ES 3 创建Client 4 查询API 4.1 根据id查询单条数据 ...

  3. ElasticSearch Java High level Rest Client 官方文档中文翻译(一)

    ElasticSearch Java High level Rest Client 官方文档中文翻译 一 纯粹记录自己在看官网的es rest high level api 时的翻译笔记,可以对照着官 ...

  4. Java High Level REST Client 中文API(仅供参考)

    1.初始化 兼容性 Java High Level REST Client需要Java 1.8,并依赖于Elasticsearch核心项目,客户端版本与客户端开发的Elasticsearch版本相同, ...

  5. Elasticsearch Java Low Level REST Client(通用配置)

    Elasticsearch Java Low Level REST Client(通用配置) 通用配置 正如初始化中所解释的,RestClientBuilder支持提供RequestConfigCal ...

  6. 【flink】Flink常见Checkpoint超时问题排查思路

    1.概述 转载:Flink常见Checkpoint超时问题排查思路 这里仅仅是自己学习. 在日常flink应用中,相信大家经常会遇到checkpoint超时失败这类的问题,遇到这种情况的时候仅仅只会在 ...

  7. java bulk_Elasticsearch Java High Level REST Client(Bulk API)

    Bulk API Java High Level REST Client提供了Bulk处理器来帮助处理批量请求. Bulk请求 BulkRequest可以使用一个请求执行多个索引.更新和/或删除操作. ...

  8. ElasticSearch 使用 High Level REST Client 实现搜索等功能实战

    点击关注公众号,实用技术文章及时了解 ES 全称 Elasticsearch 是一款分布式的全文搜索引擎,在互联网公司中,这款搜索引擎一直被程序员们所推崇.常见的使用场景如ELK日志分析,电商APP的 ...

  9. Elasticsearch java api操作(一)(Java Low Level Rest Client)

    一.说明: 一.Elasticsearch提供了两个JAVA REST Client版本: 1.java low level rest client: 低级别的rest客户端,通过http与集群交互, ...

最新文章

  1. 是什么轮胎_为什么现在的车轮胎轮毂尺寸越来越大
  2. 浏览是不是计算机网络功能,什么是因特网——计算机网络是怎么为你服务的?...
  3. Linux装多个apache,windows linux如何安装多个apache?
  4. MySQL数据库服务器的架设
  5. ICCV 2019 COCO Mapillary挑战赛冠军团队技术分享
  6. 注册注解BeanDefinition
  7. redis 流 stream的使用总结 - 消费者组
  8. GLTF格式——关系描述
  9. Python3小程序:把字符串转换成连续的UTF8编码(16位)
  10. eeupdate 更新MAC地址
  11. BP算法推导-softmax层+交叉熵(logloss)
  12. 华为云跨云迁移工具案例实践:阿里云迁移到华为云
  13. IT连创业系列:说说苹果商店AppStore上架App应用前后遇到的那些神坑
  14. k-9 邮箱添加 qq、163、gmail 帐号
  15. 电脑文件怎么显示后缀名
  16. Sql Server 时间格式转换
  17. 安装pytorch报错torch.cuda.is_available()=false的解决方法
  18. NOI Online 2020 Round3 滚粗记
  19. S2B2C模式是什么?与其他电商模式有何区别?
  20. 莫比乌斯带catia建模_神奇的莫比乌斯带(mobius)

热门文章

  1. 初使用maven遇到各种问题记录
  2. HP收购3Com在2009年全球最佳品牌100强中,惠普排名11位,思科14位
  3. CL3D: Camera-LiDAR 3D Object Detection With Point Feature Enhancement and Point-Guided Fusion 阅读笔记
  4. 翻译:PlaneRCNN: 3D Plane Detection and Reconstruction from a Single Image
  5. 【学习OpenCV4】键盘鼠标操作总结
  6. Log4j 重写 RollingFileAppender 实现按日期生成文件夹文件名
  7. 25家往昔明星网站“血泪史” 教你如何过冬
  8. [机缘参悟-43]:鬼谷子-第六忤合篇 - 合则聚,不合则散,顺势而为
  9. 虹科Automation softPLC | 虹科KPA MoDK运行环境与搭建步骤(3)——MoDK例程测试
  10. Elasticsearch SSL认证/证书制作