[英]Unable to set WriteTimeout on reactor-netty version 0.9.10
I have written a Reactive API using Spring WebFlux version 2.3.0.RELEASE having reactor-netty version 0.9.10 .我已经使用 Spring WebFlux 版本2.3.0.RELEASE和reactor-netty 版本 0.9.10 编写了反应式 API 。 As part of the API's SLA, I want to timeout the request if the Server takes more than the stipulated configured WriteTimeout.
作为 API 的 SLA 的一部分,如果服务器占用的时间超过规定的配置 WriteTimeout,我想使请求超时。
Sharing the code snipped below where I have implemented a customizer for NettyReactiveWebServerFactory.分享下面我为 NettyReactiveWebServerFactory 实现定制器的代码。
@Bean
public WebServerFactoryCustomizer serverFactoryCustomizer() {
return new NettyTimeoutCustomizer();
}
class NettyTimeoutCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
@Override
public void customize(NettyReactiveWebServerFactory factory) {
int connectionTimeout = 1000;
int writeTimeout = 1;
factory.addServerCustomizers(server -> server.tcpConfiguration(tcp ->
tcp.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
.doOnConnection(connection ->
connection.addHandlerLast(new WriteTimeoutHandler(writeTimeout)))));
}
}
In spite of the Customizer, the WriteTimeout is Not Working for the API.尽管有定制器,WriteTimeout 对 API 不起作用。
Instead of defining a WebServerFactoryCustomizer
bean, create a bean of NettyReactiveWebServerFactory
to override Spring's auto-configuration.与其定义
WebServerFactoryCustomizer
bean,不如创建一个NettyReactiveWebServerFactory
的 bean 来覆盖 Spring 的自动配置。
@Bean
public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() {
NettyReactiveWebServerFactory webServerFactory = new NettyReactiveWebServerFactory();
webServerFactory.addServerCustomizers(new MyCustomizer());
return webServerFactory;
}
Now the MyCustomizer
will look something like this:现在
MyCustomizer
看起来像这样:
public class MyCustomizer implements NettyServerCustomizer {
@Override
public HttpServer apply(HttpServer httpServer) {
return httpServer.tcpConfiguration(tcpServer -> tcpServer.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
.bootstrap(serverBootstrap -> serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast("writeTimeoutHandler", new WriteTimeoutHandler(1));
}
}))
);
}
}
This is the way suggested in the official API doc这是官方 API 文档中建议的方式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.