我尝试过重现该问题,但无法重现。使用reactor-netty 0.7.5.RELEASE。
我不确定你说的是哪个超时。
连接超时可以配置为ChannelOption.CONNECT_TIMEOUT_MILLIS
。我在“正在连接”日志消息和实际错误之间间隔了 10 秒:
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(options -> options
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)))
.build();
webClient.get().uri("http://10.0.0.1/resource").exchange()
.doOnSubscribe(subscription -> logger.info("connecting"))
.then()
.doOnError(err -> logger.severe(err.getMessage()))
.block();
如果你谈论的是读/写超时,那么你可以看看Netty的ReadTimeoutHandler
and WriteTimeoutHandler
.
完整的示例可能如下所示:
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(options ->
options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10)
.onChannelInit(channel -> {
channel.pipeline().addLast(new ReadTimeoutHandler(10))
.addLast(new WriteTimeoutHandler(10));
return true;
}).build());
从 Reactor Netty 0.8 和 Spring Framework 5.1 开始,配置现在如下所示:
TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
.doOnConnected(connection ->
connection.addHandlerLast(new ReadTimeoutHandler(10))
.addHandlerLast(new WriteTimeoutHandler(10)));
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.build();
也许将以下内容添加到您的application.properties
将提供有关 HTTP 级别发生的情况的更多信息:
logging.level.reactor.ipc.netty.channel.ContextHandler=debug
logging.level.reactor.ipc.netty.http.client.HttpClient=debug