18

【开发小记】ES RestHighLevelClient 请求报错:Connection reset by peer

 3 years ago
source link: https://ricstudio.top/archives/es-client-error-connection-reset-by-peer
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用 Spring boot 服务使用 ES RestHighLevelClient 连接 ES 运行一段时间就会出现Connection reset by peer的异常。这个异常稍微一查就知道,原因是因为 TCP 连接中断,业务数据写入失败。

而连接关闭有很多原因,是 ES 服务器端不能完全控制的。有可能关闭了连接,有可能有防火墙,交换机,vpn 等,也有可能是 keepalive 设置问题,更换了连接服务器节点,网络不稳定等。

TCP 长连接和短连接

TCP 协议中有长连接和短连接之分。短连接在数据包发送完成后就会自己断开,长连接在发包完毕后,会在一定的时间内保持连接,即我们通常所说的 Keepalive(存活定时器)功能。

TCP 保活机制

保活机制是由一个保活计时器实现的。当计时器被激发,连接一段将发送一个保活探测报文,
另一端接收报文的同时会发送一个 ACK 作为响应。
如果客户端无响应服务器将中断连接,否则会重置保活计时器。
服务器端 Linux 有三个参数可以控制保活时间

  1. tcp_keepalive_time(开启 keepalive 的闲置时长)
  2. tcp_keepalive_intvl(keepalive 探测包的发送间隔)
  3. tcp_keepalive_probes (如果对方不予应答,探测包的发送次数)

TCP 保活机制详见:https://segmentfault.com/a/1190000021057175

本人实际采用了方式 3 和方式 4

方式 1:修改 RestHighLevelClient 连接请求的超时时间

  1. // 默认 1000ms 可以尝试增加到 10000ms
  2. RestClientBuilder builder = RestClient.builder(new HttpHost(endpoint, port))
  3. .setHttpClientConfigCallback(httpClientBuilder->
  4. httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
  5. .setRequestConfigCallback(requestConfigBuilder ->
  6. requestConfigBuilder.setConnectTimeout(10000).setSocketTimeout(60000));
  7. return new RestHighLevelClient(builder);
  8. // 单个请求修改:
  9. request.timeout(TimeValue.timeValueSeconds(60));

方式 2:创建一个定时器定期探测下 es 保持 keepAlive

  1. // 可以尝试在 Spring boot 创建一个定时器定期探测下 es 保持 keepAlive
  2. @Scheduled(fixedRate = 60000, initialDelay = 60000)
  3. public void keepConnectionAlive() {
  4. log.debug("Trying to ping Elasticsearch");
  5. try {
  6. final long noOfSportsFacilities = restHighLevelClient.status();
  7. log.debug("Ping succeeded for SportsFacilityViewRepository, it contains {} entities", noOfSportsFacilities);
  8. } catch (Exception e) {
  9. log.debug("Ping failed for SportsFacilityViewRepository");
  10. }
  11. }

方式 3:设置 RestHighLevelClient keepalive 时间

  1. RestClientBuilder builder = RestClient.builder(hosts);
  2. builder.setHttpClientConfigCallback(callback -> callback.setDefaultCredentialsProvider(credentialsProvider)
  3. .setKeepAliveStrategy((response, context) -> KEEP_ALIVE_MS));

方式 4:代码层面捕获异常,重试请求

本文章只是为了简要记录,具体问题的解析可以详见。

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK