Netty的性能调优技巧

今天我学习了Netty的性能调优技巧,掌握了Netty的一些优化建议,如启用内存池、调节socket接收和发送缓冲区等。下面是一个使用内存池的服务器:

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)

                    .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)

                    .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:21  ITJAMESKING  阅读(296)  评论(0)    收藏  举报