netty代码流程详解

Netty服务器启动与客户端连接处理流程解析

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.37.Final</version>
</dependency>

1、NioEventLoopGroup初始化流程

io.netty.channel.nio.NioEventLoopGroup#NioEventLoopGroup()
======================io.netty.channel.nio.NioEventLoopGroup#NioEventLoopGroup(int)
======================io.netty.channel.nio.NioEventLoopGroup#NioEventLoopGroup(int, java.util.concurrent.Executor)
======================io.netty.channel.nio.NioEventLoopGroup#NioEventLoopGroup(int, java.util.concurrent.Executor, java.nio.channels.spi.SelectorProvider)
======================io.netty.channel.nio.NioEventLoopGroup#NioEventLoopGroup(int, java.util.concurrent.Executor, java.nio.channels.spi.SelectorProvider, io.netty.channel.SelectStrategyFactory)
======================io.netty.channel.MultithreadEventLoopGroup#MultithreadEventLoopGroup(int, java.util.concurrent.Executor, java.lang.Object...)
======================io.netty.util.concurrent.MultithreadEventExecutorGroup#MultithreadEventExecutorGroup(int, java.util.concurrent.Executor, java.lang.Object...)
======================io.netty.util.concurrent.MultithreadEventExecutorGroup#MultithreadEventExecutorGroup(int, java.util.concurrent.Executor, io.netty.util.concurrent.EventExecutorChooserFactory, java.lang.Object...)
======================》
children[i] = newChild(executor, args);
======================io.netty.channel.nio.NioEventLoopGroup#newChild

2、NioServerSocketChannel初始化流程

io.netty.bootstrap.AbstractBootstrap#bind(int)
======================io.netty.bootstrap.AbstractBootstrap#bind(java.net.SocketAddress)
======================io.netty.bootstrap.AbstractBootstrap#doBind
======================io.netty.bootstrap.AbstractBootstrap#initAndRegister
======================》
channel = channelFactory.newChannel();
======================io.netty.channel.ReflectiveChannelFactory#newChannel
======================》
constructor.newInstance();
======================io.netty.channel.socket.nio.NioServerSocketChannel#NioServerSocketChannel()
======================io.netty.channel.socket.nio.NioServerSocketChannel#newSocket
======================io.netty.channel.socket.nio.NioServerSocketChannel#NioServerSocketChannel(java.nio.channels.ServerSocketChannel)
======================super(null, channel, SelectionKey.OP_ACCEPT);
======================io.netty.channel.nio.AbstractNioMessageChannel#AbstractNioMessageChannel
======================io.netty.channel.nio.AbstractNioChannel#AbstractNioChannel
======================》
ch.configureBlocking(false);

3、NioServerSocketChannel注册连接事件到Selector由独立线程轮询流程

io.netty.bootstrap.AbstractBootstrap#initAndRegister
======================ChannelFuture regFuture = config().group().register(channel);
======================io.netty.channel.SingleThreadEventLoop#register(io.netty.channel.Channel)
======================io.netty.channel.SingleThreadEventLoop#register(io.netty.channel.ChannelPromise)
======================》
promise.channel().unsafe().register(this, promise);
======================io.netty.channel.AbstractChannel.AbstractUnsafe#register
======================io.netty.channel.AbstractChannel.AbstractUnsafe#register0
======================io.netty.channel.nio.AbstractNioChannel#doRegister
======================》
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);

4、线程轮询NioServerSocketChannel的网络连接事件流程

io.netty.channel.nio.NioEventLoop#run
======================processSelectedKeys();
======================io.netty.channel.nio.NioEventLoop#processSelectedKeysOptimized
======================io.netty.channel.nio.NioEventLoop#processSelectedKey(java.nio.channels.SelectionKey, io.netty.channel.nio.AbstractNioChannel)
======================if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
    unsafe.read();
}
======================io.netty.channel.nio.AbstractNioMessageChannel.NioMessageUnsafe#read
======================io.netty.channel.socket.nio.NioServerSocketChannel#doReadMessages
======================SocketChannel ch = SocketUtils.accept(javaChannel());
======================io.netty.util.internal.SocketUtils#accept
======================》
serverSocketChannel.accept();

5、server端将建立好的客户端连接交给子线程组处理流程

io.netty.bootstrap.ServerBootstrap.ServerBootstrapAcceptor#channelRead
======================》
childGroup.register(child)
======================io.netty.channel.SingleThreadEventLoop#register(io.netty.channel.Channel)
======================io.netty.channel.SingleThreadEventLoop#register(io.netty.channel.ChannelPromise)
======================》
promise.channel().unsafe().register(this, promise);
======================register0(promise);
======================io.netty.channel.nio.AbstractNioChannel#doRegister
======================》
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值