微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不推荐使用 HttpServer tcpConfiguration

如何解决不推荐使用 HttpServer tcpConfiguration

我从 Spring 2.3.1.RELEASE 迁移到 2.4.5。

HttpServer tcpConfiguration 已弃用新版本。如何使用 Spring boot 2.4.5 配置 NioEventLoopGroup。

    public NettyReactiveWebServerFactory factory(NioEventLoopGroup eventLoopGroup) {
        NettyReactiveWebServerFactory factory = new NettyReactiveWebServerFactory();
        factory.setServerCustomizers(Collections.singletonList(new NettyServerCustomizer() {
            @Override
            public HttpServer apply(HttpServer httpServer) {
                return httpServer.tcpConfiguration(tcpserver ->
                        tcpserver.bootstrap(serverBootstrap -> serverBootstrap.group(eventLoopGroup)
                                .channel(NioServerSocketChannel.class)));
            }
        }));
        return factory;
    }

解决方法

您应该直接使用 HttpServer#runOn API。上面的代码片段应该是这样的:

public NettyReactiveWebServerFactory factory(NioEventLoopGroup eventLoopGroup) {
    NettyReactiveWebServerFactory factory = new NettyReactiveWebServerFactory();
    factory.setServerCustomizers(Collections.singletonList(new NettyServerCustomizer() {
        @Override
        public HttpServer apply(HttpServer httpServer) {
            return httpServer.runOn(eventLoopGroup);
        }
    }));
    return factory;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。