【发布时间】:2019-05-16 02:52:50
【问题描述】:
Spring Webclient 会引发大量读取超时(每秒加载 1000 个请求时)。我正在使用 Springboot 版本 2.1.1.RELEASE。请在下面找到代码,如果我缺少任何配置,请告诉我:
@Bean
public WebClient webClient() {
return WebClient.builder().build();
}
public Mono<String> post(String url, JSONObject body) {
Mono<String> result = webClient.post().uri(url)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON_UTF8)
.body(BodyInserters.fromObject(body))
.exchange()
.flatMap { clientResponse ->
return handleResponse(clientResponse)
}
return result;
}
private Mono<String> handleResponse(ClientResponse clientResponse) {
if (clientResponse.statusCode().is4xxClientError() || clientResponse.statusCode().is5xxServerError()) {
return clientResponse.bodyToMono(String.class)
.flatMap { errorBody ->
return Mono.error(new CustomException(errorBody, clientResponse.statusCode().value()))
}
} else {
return clientResponse.bodyToMono(String.class);
}
}
【问题讨论】:
-
您能提供更多信息吗?您是否为客户端配置了超时?你能分享错误的堆栈跟踪吗?使用 JMeter 请求远程服务时,您得到的最大延迟是多少?
-
@BrianClozel
@Bean public WebClient webClient() { TcpClient tcpClient = TcpClient.create() .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000) .doOnConnected { connection -> connection.addHandlerLast(new ReadTimeoutHandler(5)) .addHandlerLast(new WriteTimeoutHandler(30)) } ReactorClientHttpConnector httpConnector = new ReactorClientHttpConnector(HttpClient.from(tcpClient)); return WebClient.builder() .clientConnector(httpConnector) .build(); } -
@BrianClozel 我尝试了上面的超时配置以及默认的
WebClient.builder().build();。在这两种情况下,我都会收到 readtimeout 异常 -
您能否在交换后添加一个 « log() » 运算符并在此处与日志一起报告?
-
@BrianClozel
Pooled connection observed an error 2018-12-17T18:13:36.903185649Z io.netty.handler.timeout.ReadTimeoutException: null这是我得到的错误日志。
标签: spring-boot spring-webflux