我正在使用Spring Webflux WebClient从我的Spring启动应用程序进行REST调用.并且每次在30秒内超时.
这是我尝试在Spring webfulx的WebClient中设置套接字超时的一些代码.
- ReactorClientHttpConnector connector = new ReactorClientHttpConnector(options -> options
.option(ChannelOption.SO_TIMEOUT, 600000).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 600000));
- ReactorClientHttpConnector connector = new ReactorClientHttpConnector(
options -> options.afterChannelInit(chan -> {
chan.pipeline().addLast(new ReadTimeoutHandler(600000));
}));
- ReactorClientHttpConnector connector1 = new ReactorClientHttpConnector(options -> options
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 600000).afterNettyContextInit(ctx -> {
ctx.addHandlerLast(new ReadTimeoutHandler(600000, TimeUnit.MILLISECONDS));
}));
Run Code Online (Sandbox Code Playgroud)
并尝试使用"clientConnector"方法在"WebClient"中添加上面的连接器设置.
并尝试设置超时如下:
webClient.get().uri(builder -> builder.path("/result/{name}/sets")
.queryParam("q", "kind:RECORDS")
.queryParam("offset", offset)
.queryParam("limit", RECORD_COUNT_LIMIT)
.build(name))
.header(HttpHeaders.AUTHORIZATION, accessToken)
.exchange().timeout(Duration.ofMillis(600000))
.flatMap(response -> handleResponse(response, name, offset));
Run Code Online (Sandbox Code Playgroud)
上述选项均不适合我.
我正在使用org.springframework.boot:spring-boot-gradle-plugin:2.0.0.M7,它们具有org.springframework的依赖性:spring-webflux:5.0.2.RELEASE.
请在这里建议,如果我在这里做错了,请告诉我.
如果通道在超时毫秒内没有收到读/响应,SO_TIMEOUT是否会使非阻塞通道失效?
bootstrap.group(workerGroup).channel(NioSocketChannel.class).
.handler(channelInitializer).option(ChannelOption.SO_TIMEOUT, 100);
Run Code Online (Sandbox Code Playgroud)
此外,该选项是否也适用于服务器频道?喜欢:
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).
localAddress(new InetSocketAddress(8800)).childHandler(serverChannelInitializer).
option(ChannelOption.SO_TIMEOUT, 100).bind().sync();
Run Code Online (Sandbox Code Playgroud) 为(默认)WebClient设置(连接)超时的正确方法是什么?
仅对Mono#timeout(Duration)所得的Mono(或Flux)使用方法就足够了吗?还是这会导致可能的内存/连接泄漏?
提前致谢!
(Spring 5 webflux如何在Webclient上设置超时的答案不起作用!)