栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

netty 线程模型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

netty 线程模型


netty 线程模型

        

             

                               

单线程模型

        

连接、业务处理均使用同一个线程处理

                  

使用场景:并发量小的业务场景,不适合高并发的业务场景

        

NettySingleThreadServer

public class NettySingleThreadServer {

    public static void startServer(int port){
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup(1);  //线程池只有一个线程

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(eventLoopGroup)  //主从线程池使用同一个线程池
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer() {

                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline channelPipeline = socketChannel.pipeline();
                            channelPipeline.addLast(new StringDecoder());
                            channelPipeline.addLast(new StringEncoder());
                            channelPipeline.addLast(new CustomServerHandler());
                        }
                    });

            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            eventLoopGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        startServer(8000);
    }
}

           

                      

                               

多线程模型

    

单独的线程处理客户端连接、多线程处理业务逻辑

                  

acceptor:单线程处理所有客户端的连接

reactor thread pool:线程池处理业务逻辑

使用场景:适用于并发量小的业务场景,不适合高并发的业务场景

        

NettyMultiThreadServer

public class NettyMultiThreadServer {

    public static void startServer(int port){
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);   //处理客户端连接
        EventLoopGroup workerGroup = new NioEventLoopGroup(8); //处理业务逻辑

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)  //设置主从线程池
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer() {

                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline channelPipeline = socketChannel.pipeline();
                            channelPipeline.addLast(new StringDecoder());
                            channelPipeline.addLast(new StringEncoder());
                            channelPipeline.addLast(new CustomServerHandler());
                        }
                    });

            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        startServer(8000);
    }
}

        

             

                               

主从多线程模型

   

客户端连接、业务逻辑分别用单独的线程池处理

                  

main reactor thread pool:线程池处理客户端连接(请求认证、握手等)

sub reactor thread pool:线程池处理业务逻辑

适用场景:适用于并发量比较大的业务场景

        

NettyMasterSlaveMultiThreadServer

public class NettyMasterSlaveMultiThreadServer {

    public static void startServer(int port){
        EventLoopGroup bossGroup = new NioEventLoopGroup();   //多线程处理客户端连接
        EventLoopGroup workerGroup = new NioEventLoopGroup(); //多线程处理业务逻辑

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)   //设置主从多线程池
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer() {

                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline channelPipeline = socketChannel.pipeline();
                            channelPipeline.addLast(new StringDecoder());
                            channelPipeline.addLast(new StringEncoder());
                            channelPipeline.addLast(new CustomServerHandler());
                        }
                    });

            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        startServer(8000);
    }
}

       

            

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930608.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号