【问题标题】:Spring webflux WebClient logs 'Connection reset by peer'Spring webflux WebClient 记录“对等方重置连接”
【发布时间】:2019-08-09 12:30:09
【问题描述】:

我有以下代码,它使用 WebClient 进行 HTTP 调用。

        webClient.post()
                 .uri("/users/track")
                 .body(BodyInserters.fromObject(getUserTrackPayload(selection, customAttribute, partyId).toString()))
                 .header(CONTENT_TYPE, APPLICATION_JSON)
                 .retrieve()
                 .onStatus(httpStatus -> !CREATED.equals(httpStatus),
                           response -> response.bodyToMono(String.class)
                                               .flatMap(body -> buildErrorMessage(response.statusCode().value(), body, partyId,
                                                                                  customAttribute)
                                                   .flatMap(e -> Mono.error(new MyException(e)))))
                 .bodyToMono(Object.class)
                 .map(o -> (JsonObject)new Gson().toJsonTree(o))
                 .flatMap(body -> body.get("message") != null && body.get("message").getAsString().equalsIgnoreCase("success")
                                  && body.get("attributes_processed") != null && body.get("attributes_processed").getAsInt() == 1
                     ? Mono.just(body)
                     : buildErrorMessage(CREATED.value(), body.toString(), partyId, customAttribute)
                         .flatMap(e -> Mono.error(new MyException(e))));

一段时间后(例如 10 分钟)第一次调用此代码时,我会收到以下日志。但是,调用成功,输出正确。

io.netty.channel.unix.Errors$NativeIoException: syscall:read(..) failed: Connection reset by peer at io.netty.channel.unix.FileDescriptor.readAddress(..)(Unknown Source)
2019-03-19 03:11:45,625 WARN  [:::] [reactor-http-epoll-8] reactor.netty.http.client.HttpClientConnect : [id: 0x2e3252c0, L:/172.18.0.125:42956 - R:my-endpoint.com/151.101.53.208:443] The connection observed an error

不确定为什么会生成这些日志。当我使用 SpringBoot 2.1.0 时,它正在登录 ERROR 级别,现在我升级到 2.1.3 版本(reactor netty 版本 - 0.8.5)并且它正在登录 WARN 级别。我应该担心这些日志吗?

【问题讨论】:

  • 这个运气好吗?我有点面临类似的问题。

标签: java reactive-programming spring-webflux spring-webclient


【解决方案1】:

当客户端在连接一次后离开(反弹)并且下一个请求失败 - 然后重试时,我看到了类似的行为。

尝试在创建 WebClient 时禁用连接池。像这样的:

@Bean
public WebClient webClient() {
    return WebClient.builder()
             .clientConnector(connector())
             .build();
}

private ClientHttpConnector connector() {
    return new ReactorClientHttpConnector(HttpClient.from(TcpClient.newConnection()));
}

【讨论】:

猜你喜欢
  • 2018-06-14
  • 2018-05-09
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多