如何使用Spring Boot 2.1 Webflux自定义Netty? [英] How to customize Netty with Spring Boot 2.1 Webflux?

查看:1105
本文介绍了如何使用Spring Boot 2.1 Webflux自定义Netty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Spring Boot Webflux项目中自定义Netty.在我的POM中,我添加了Spring Boot Webflux和Spring Boot Actuator依赖项.接下来,我重写了

I would like to customize Netty in my Spring Boot Webflux project. In my POM I added Spring Boot Webflux and Spring Boot Actuator dependencies. Next I overwrote the customize() method of WebServerFactoryCustomizer as described in the Spring documentation.

@Component
public class NettyConfiguration implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Override
    public void customize(NettyReactiveWebServerFactory factory) {
        factory.addServerCustomizers(new NettyCustomizer());
    }
}

然后,我在NettyCustomizer中实现了Netty引导:

Then I implemented the Netty bootstrapping in my NettyCustomizer:

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"错误.

Now if I start the Spring Boot application I get a "Unable to start Netty" error.

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吗?

So it seems there is no way to override Netty bootstrapping if using Webflux. Changing the addServerCustomizers() method to setServerCustomizers() in the customize() method leads to the same exceptions unfortunately. Does anybody know how to customize Netty together with Spring Boot?

推荐答案

如果要用自己的服务器替换它,则需要从pom.xml中删除一些netty的依赖项.但是,如果您要配置管道或类似的东西,我想您可以按以下方式在春季启动中操纵Web服务器配置;

If you want to replace it with your own server, you need to delete some of netty's dependencies from pom.xml. However if you want to configure pipeline or stuff like that i guess you can manipulate web server configuration in spring boot as follows;

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

这篇关于如何使用Spring Boot 2.1 Webflux自定义Netty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆