Netty的基本概念和架构

今天我开始了学习Netty的第一天,学习了Netty的基本概念和架构,了解了Netty的核心组件,包括ChannelEventLoopChannelHandler以及编解码器等。下面是一个简单的Netty服务器:

public class Server {

 

    public static void main(String[] args) throws Exception {

        EventLoopGroup bossGroup = new NioEventLoopGroup();

        EventLoopGroup workerGroup = new NioEventLoopGroup();

 

        try {

            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(bossGroup, workerGroup)

                    .channel(NioServerSocketChannel.class)

                    .childHandler(new ChannelInitializer<SocketChannel>() {

                        @Override

                        protected void initChannel(SocketChannel ch)

                                throws Exception {

                            ch.pipeline().addLast(new ServerHandler());

                        }

                    });

 

            ChannelFuture future = bootstrap.bind(8080).sync();

            future.channel().closeFuture().sync();

        } finally {

            bossGroup.shutdownGracefully();

            workerGroup.shutdownGracefully();

        }

    }

}

posted @ 2023-05-22 21:19  ITJAMESKING  阅读(4)  评论(0编辑  收藏  举报