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

简单的 Web CRUD WebFlux 应用程序:Netty 如何有用?

如何解决简单的 Web CRUD WebFlux 应用程序:Netty 如何有用?

我从这里了解到:What's the difference between Jetty and Netty? Netty 根本不是服务器。 但从这里开始:https://stackoverflow.com/a/57297055/10894456 我看到它是服务器。

无论如何,我想运行 Web 应用程序应该有一个服务器。那么 Netty 是否有助于解决它呢?或者无论如何需要某种服务器(Tomcat或Jetty或其他)? 但是从这里:Don't spring-boot-starter-web and spring-boot-starter-webflux work together? 我了解到 Netty 与 Tomcat 不兼容...

请澄清一下,创建反应式 WebFlux crud 应用程序的最简单方法是什么? Netty 如何提供帮助?如果使用 Netty,服务器会是什么? Netty 如何与之兼容?

编辑: 好的,我看到 Netty 本身不是服务器,需要编写如下内容

public class NettyServer {

    private int port;

    // constructor

    public static void main(String[] args) throws Exception {
 
        int port = args.length > 0
          ? Integer.parseInt(args[0]);
          : 8080;
 
        new NettyServer(port).run();
    }

    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup,workerGroup)
              .channel(NioServerSocketChannel.class)
              .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) 
                  throws Exception {
                    ch.pipeline().addLast(new RequestDecoder(),new ResponseDataEncoder(),new ProcessingHandler());
                }
            }).option(ChannelOption.so_BACKLOG,128)
              .childOption(ChannelOption.so_KEEPALIVE,true);

            ChannelFuture f = b.bind(port).sync();
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}

但我也相信在创建简单的 WebFlux CRUD 服务时没有人会这样做。所以问题仍然存在:Netty 如何提供帮助?如果使用 Netty,服务器会是什么? Netty 如何与之兼容?

编辑 2: 经过数小时的浏览,我意识到:Netty - 不是服务器,它只是一个使用通道/NIO2 的框架,但 spring-boot-starter-reactor-netty 提供非阻塞和基于 Netty 框架的背压就绪 TCP/HTTP/UDP 客户端和服务器。 但可以以这种方式使用 spring-boot-starter-tomcatspring-boot-starter-jettyspring-boot-starter-undertow

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <exclusions>
            <!-- Exclude the Netty dependency -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-reactor-netty</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Use Jetty instead -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

所以只有 2 个概念相互混淆和混乱:Nettyspring-boot-starter-reactor-netty

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