我想在我的 Spring Boot Webflux 项目中自定义 Netty。在我的 POM 中,我添加了 Spring Boot Webflux 和 Spring Boot Actuator 依赖项。接下来我重写了Spring 文档中描述的 WebServerFactoryCustomizer 的 customize() 方法。
@Component
public class NettyConfiguration implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
@Override
public void customize(NettyReactiveWebServerFactory factory) {
factory.addServerCustomizers(new NettyCustomizer());
}
}
然后我在我的 NettyCustomizer 中实现了 Netty 引导:
public class NettyCustomizer implements NettyServerCustomizer {
private final EventLoopGroup bossGroup = new NioEventLoopGroup(22);
private final EventLoopGroup workerGroup = new NioEventLoopGroup();
@Override
public HttpServer apply(HttpServer httpServer) {
return httpServer.tcpConfiguration(tcpServer ->
tcpServer.bootstrap(serverBootstrap ->
serverBootstrap
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(final SocketChannel socketChannel) {
socketChannel.pipeline().addLast(new BufferingInboundHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true))
.port(8899)
);
}
}
现在,如果我启动 Spring Boot 应用程序,我会收到“无法启动 Netty”错误。
org.springframework.boot.web.server.WebServerException: Unable to start Netty
Caused by: java.lang.IllegalStateException: group set already
因此,如果使用 Webflux,似乎没有办法覆盖 Netty 引导。不幸的是,将 custom() 方法中的 addServerCustomizers() 方法更改为 setServerCustomizers() 会导致相同的异常。有人知道如何将 Netty 与 Spring Boot 一起定制吗?