Netty服务端启动流程图
2022-11-22 15:56:01 1 举报
netty服务器端启动流程图。一边debug源码,一边结合本流程图,效果更好。 由于本人水平有限,图中错漏缺点在所难免,希望读者批评指正。
作者其他创作
大纲/内容
extends
NioEventLoop
doBind
initAndRedister
font color=\"#2196f3\
group
NioServerSocketChannel
构造方法NioServerSocketChannel
1.初始化ExecutorExecutor executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());2.初始下EventExecutor数组childrenEventExecutor[] children = new EventExecutor[nThreads];for (int i = 0; i < nThreads; i ++) { //newChild方法调用子类NioEventLoopGroup的实现 children[i] = font color=\"#ff0000\
ServerBootstrap
register
this就是当前的NioEventLoop对象channel.unsafe().font color=\"#ff0000\
public Channel read() { //执行pipeline.read()方法 pipeline.read(); return this; }pipelie.read()流程:DefaultChannelPipeline.read()->tail.read()->找到需要执行read()方法的handler->DefaultChannelPipeline$HeadContext.read()->AbstractUnsafe.beginRead()->AbstractNioChannel.doBeginRead()
构造方法NioEventLoopGroup
AbstractNioChannel
Netty Server 创建和启动流程图
channel
newChild
调用ServerBootStrap的init方法
this.channelFactory= newReflectiveChannelFactory(NioServerSocketChannel.class)用于后续创建Channel使用。
内部抽象类AbstractUnsafe
read
1.创建一个java的ServerSocketChannel对象,使用newSocket方法SelectorProvider provider = SelectorProvider.provider() ServerSocketChannel channel = provider.openServerSocketChannel();2.调用父类构造方法,font color=\"#ff0000\
eventLoop
1.创建channel实例Channel channel = channelFactory.newChannel();2.初始化channel属性,调用子类的init方法init(channel)3.将channel绑定到group中的一个NioEventLoop上config().group().register(channel)
MultithreadEventLoopGroupMultithreadEventExecutorGroup
channel注册到NioEventLoop中的selector之后,NioEventLoop会循环调用selector.select()和selectedKeys()执行请求accept事件处理逻辑。
channel.font color=\"#ff0000\
readInterestOp来源
javaCahnnel()方法返回的就是构造方法赋值的ch
//重新设置关注事件 此处的readInterestOp=16是SelectionKey.OP_ACCEPT。selectionKey.interestOps(interestOps | readInterestOp);
bind(port)
channelfactory来自于channel方法创建的
//设置group=new NioEventLoopGroup(1)this.group = group;
NioEventLoopGroup
config.group就是group方法配置的group
//netty服务端启动示例 EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); //配置server的boosGroup和workerGroup b.font color=\"#ff0000\
newChannel
return constructor.newInstance();等价于return new NioServerSocketChannel();
AbstractNioMessageChannel
init(channel)
bind
constructor来自于构造发方法创建的
doRegister
构造方法MultithreadEventExecutorGroup
1.调用子类NioServerSocketChannel的doBind方法doBind(localAddress)2.invokeLater触发channelActive事件span style=\
构造方法
AbstractChannel
super.group(parentGroup);//childGroup=new NioEventLoopGroup()this.childGroup = childGroup;
bind结束
AbstractBootstrap
public ReflectiveChannelFactory(Class<? extends T> clazz) { //初始化构造方法Constructor对象 this.constructor =clazz.getConstructor(); }
核心流程://openSelector方法,生成java的SelectorSelectorTuple selectorTuple = openSelector(); this.selector = selectorTuple.selector; this.unwrappedSelector = selectorTuple.unwrappedSelector;
SingleThreadEventLoop
ReflectiveChannelFactory
doBind0
//调用javaChannel得到java的Channel对象,然后执行其bind方法。javaChannel().font color=\"#ff0000\
doBeginRead
收藏
0 条评论
下一页