我使用 Web-flux/Reactive 和 Webclient,在 tomcat 和 spring-boot 上运行它。
一切正常。我阅读了很多关于它的信息。问题似乎是每当你使用 webclient 时,你必须返回或使用响应,否则它会关闭连接而你还没有使用它,你会看到很多日志消息说the connection close prematurely,如果我有404 状态代码是一个错误的场景,我可以使用OnStatus并抛出异常,但我的情况是:当上游服务返回 404 时,我必须手动返回单声道空。所以我不使用来自 Web 客户端请求的响应,我只是使用来自 .exchange() 的 ClientResponse 来检查状态并处理它。我最初的问题是日志消息,因为这只是“垃圾”,您不希望在日志消息中看到很多。我在某处读到,如果发生这种情况,连接也不能重复使用,所以这听起来很糟糕,但我不知道......如果没有找到,我只会收到这条消息,如果响应是 200它返回对象并且不打印日志消息。
我尝试使用 clientResponse.BodyToMono(Void.Class) 但它也不起作用。保持出现的日志消息
@Bean
public WebClient webClient(
@Value("${http.client.connection-timeout-millis}") final int connectionTimeoutMillis,
@Value("${http.client.socket-timeout-millis}") final int socketTimeoutMillis,
@Value("${http.client.wire-tap-enabled}") final boolean wireTapEnabled,
final ObjectMapper objectMapper) {
Consumer<Connection> doOnConnectedConsumer = connection ->
connection
.addHandler(new ReadTimeoutHandler(socketTimeoutMillis, MILLISECONDS))
.addHandler(new WriteTimeoutHandler(connectionTimeoutMillis, MILLISECONDS));
TcpClient tcpClient = TcpClient.newConnection()
.wiretap(wireTapEnabled)
.option(CONNECT_TIMEOUT_MILLIS, connectionTimeoutMillis)
.doOnConnected(doOnConnectedConsumer);
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient).compress(true)))
.exchangeStrategies(customExchangeStrategies(objectMapper))
.build();
}
// .......... …Run Code Online (Sandbox Code Playgroud)