【问题标题】:How to customize Netty with Spring Boot 2.1 Webflux?如何使用 Spring Boot 2.1 Webflux 自定义 Netty?
【发布时间】:2019-05-25 15:49:36
【问题描述】:

我想在我的 Spring Boot Webflux 项目中自定义 Netty。在我的 POM 中,我添加了 Spring Boot Webflux 和 Spring Boot Actuator 依赖项。接下来我重写了Spring documentation中描述的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() 会导致相同的异常。有人知道如何使用 Spring Boot 自定义 Netty 吗?

【问题讨论】:

    标签: java spring-boot spring-webflux reactor-netty


    【解决方案1】:

    如果要替换成自己的服务器,需要从pom.xml中删除一些netty的依赖。但是,如果您想配置管道或类似的东西,我想您可以在 Spring Boot 中操作 Web 服务器配置,如下所示;

    @Override
    public void customize(WebServerFactory factory) {
        NettyReactiveWebServerFactory nettyReactiveWebServerFactory = (NettyReactiveWebServerFactory) factory;
        nettyReactiveWebServerFactory.addServerCustomizers(builder -> builder
                .tcpConfiguration(tcpServer -> tcpServer.bootstrap(i -> i.handler(new ObjectEchoServerHandler("1")))));
    }
    

    【讨论】:

      【解决方案2】:

      the Spring Boot reference documentation 中所述,您可以通过定义自己的ReactorResourceFactory 来自定义运行Netty 服务器和客户端的资源,如下所示:

      @Bean
      public ReactorResourceFactory resourceFactory() {
          ReactorResourceFactory reactorResourceFactory = new ReactorResourceFactory();
          reactorResourceFactory.setLoopResourcesSupplier(() -> LoopResources.create("custom-prefix"));
          return reactorResourceFactory;
      } 
      

      请注意,我们在这里使用LoopResources abstraction provided by Reactor Netty。这允许在客户端和服务器之间共享资源以提高效率。

      如果您无法实现您想要的,请随时在 Reactor Netty 或 Spring Boot 中打开增强请求。

      【讨论】:

      • 嗯...这不是我想做的。那么有没有办法用 Spring Boot Webflux 自定义 Netty 的 bootstrap 属性呢?
      • 您好,只是想知道方法“resourceFactory()”的名称是否重要?我可以使用不同的名称来实现相同的目标吗?
      • 是的,您可以使用其他名称
      猜你喜欢
      • 1970-01-01
      • 2022-06-05
      • 2018-07-26
      • 2018-02-06
      • 2021-05-16
      • 1970-01-01
      • 2019-01-11
      • 2019-02-16
      • 2019-03-18
      相关资源
      最近更新 更多