Netty知识点(杂记)

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class Server {
     


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

        // 这1个线程用于接收客户端的连接
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        // 这4个线程用于处理IO读写
        EventLoopGroup workerGroup = new NioEventLoopGroup(4);
        // 这8个线程用于业务处理
        EventLoopGroup businessGroup1 = new NioEventLoopGroup(8);
        // 这8个线程也是用于业务处理
        EventLoopGroup businessGroup2 = new NioEventLoopGroup(8);

        ServerBootstrap serverBootstrap = new ServerBootstrap();

        try {
     
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
     
                        @Override
                        protected void initChannel(NioSocketChannel ch) {
     
                            ChannelPipeline channelPipeline = ch.pipeline();
                            channelPipeline.addLast(new StringEncoder());
                            channelPipeline.addLast(new StringDecoder());
                            // 这个ServerInHandler1是由哪类线程处理?
                            channelPipeline.addLast(new ServerInHandler1());
                            // 这个ServerInHandler2是由哪类线程处理?
                            channelPipeline.addLast(businessGroup2, new ServerInHandler2());
                        }
                    });

            ChannelFuture channelFuture = serverBootstrap.bind("127.0.0.1", 8080).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
     
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

以上代码是使用Netty创建服务端时的常规代码.
这里主要关注一个地方

// 这个ServerInHandler1是由哪类线程处理?
channelPipeline.addLast(new ServerInHandler1());
// 这个ServerInHandler2是由哪类线程处理?
channelPipeline.addLast(businessGroup2, new ServerInHandler2());

唯一不同的地方就是ServerInHandler2使用了一个businessGroup2.
那么ServerInHandler1是由EventLoopGroup workerGroup中的某一个线程来处理它的业务代码的.ServerInHandler2是由EventLoopGroup businessGroup2中的某一个线程来处理它的业务代码的.

如果把代码改成如下

// 这个ServerInHandler1是由哪类线程处理?
channelPipeline.addLast(businessGroup2, new ServerInHandler1());
// 这个ServerInHandler2是由哪类线程处理?
channelPipeline.addLast(businessGroup2, new ServerInHandler2());

那么ServerInHandler1和ServerInHandler2都是由EventLoopGroup businessGroup2中的某一个线程来处理它们的业务代码的.

如果再把代码改成如下

// 这个ServerInHandler1是由哪类线程处理?
channelPipeline.addLast(businessGroup1, new ServerInHandler1());
// 这个ServerInHandler2是由哪类线程处理?
channelPipeline.addLast(businessGroup2, new ServerInHandler2());

那么ServerInHandler1是由EventLoopGroup businessGroup1中的某一个线程来处理它的业务代码的.ServerInHandler2是由EventLoopGroup businessGroup2中的某一个线程来处理它的业务代码的.

程序代码是由线程来执行的,因此哪块代码是由哪个线程执行的需要很清楚,这样出现问题,也便于排查.

你可能感兴趣的:(Netty,Netty)