连接被对等点重置 io.netty.channel.unix.Errors$NativeIoException

Vel*_*Vel 5 spring-boot reactor-netty spring-webflux

我正在使用 spring-boot-starter-webflux 中的 WebClient。我在生产环境中不断看到来自 Reactor Netty 的对等错误连接重置。然后,reactor netty 在几秒钟(约 10-20 秒)后重试此失败的请求。我没有看到或无法在较低的环境中重现此错误。我无法确定此错误的根本原因,这里我提供了我的 ClientHelper 实现和错误日志,

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;

public class ClientHelper {

  protected WebClient webClient;

  public <T> T post(Object request, TypeReference typeReference,
                    String uri) {
    try {
      ObjectMapper objectMapper = new ObjectMapper();
      String body = objectMapper.writeValueAsString(request);
      ClientResponse clientResponse =
              getWebClient()
                      .post()
                      .uri(uri)
                      .body(BodyInserters.fromObject(body))
                      .exchange()
                      .block();
      return prepareResponse(clientResponse, typeReference, objectMapper);
    } catch (Exception e) {
      return handleException(e);
    }
  }

  protected <T> T prepareResponse(ClientResponse clientResponse, TypeReference typeReference,
                                  ObjectMapper objectMapper) throws Exception {
    String responseText = clientResponse.bodyToMono(String.class).block();
    if (clientResponse.statusCode() == HttpStatus.OK) {
      return objectMapper.readValue(responseText, typeReference);
    } else {
      throwNewException("Remote service returned a message with statusCode = "
              + clientResponse.statusCode() + "; response = " + responseText,  null);
      return null;
    }
  }

  protected <T> T handleException(Exception e) {
    throwNewException("Communication error. Cause: " + e.getMessage(), e);
    return null;
  }

  protected void throwNewException(String message, Throwable cause) {
    throw new RuntimeException(message, cause);
  }

  public WebClient getWebClient() {
    return WebClient.builder().baseUrl("http://app.corp.com/").build();
  }
Run Code Online (Sandbox Code Playgroud)
2019-10-29 20:56:20,383 DEBUG r.u.Loggers$Slf4JLogger [reactor-http-epoll-8] [] [id: 0x65cf7989, L:/xx.xx.xxx.xxx:xxxxx - R:app.corp.com/xx.xx.xx.xx:xxx] The connection observed an error, the request will be retried
io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection reset by peer
Run Code Online (Sandbox Code Playgroud)

spring-boot-starter-webflux:2.1.9.RELEASE,反应器-netty:0.8.12.RELEASE

如果此实施存在问题和/或如何进一步分类此问题,请告诉我。

kar*_*agh 0

您在生产中看到此问题的原因之一是 SSL。尝试配置 SSL 超时

例如,下面的设置是针对使用netty的spring cloud gateway:

https://cloud.spring.io/spring-cloud-gateway/multi/multi__tls_ssl.html

spring:
  cloud:
    gateway:
      httpclient:
        ssl:
          handshake-timeout-millis: 10000
          close-notify-flush-timeout-millis: 3000
          close-notify-read-timeout-millis: 0
Run Code Online (Sandbox Code Playgroud)

https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto- webclient-reactor-netty-customization