学习第22节
Netty启动过程主线
启动服务的本质:
Selector selector = sun.nio.ch.SelectorProviderImpl.openSelector()
ServerSocketChannel serverSocketChannel = provider.openServerSocketChannel()
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
javaChannel().bind(localAddress, config.getBacklog());
selectionKey.interestOps(OP_ACCEPT);
Selector 是在 new NioEventLoopGroup()(创建一批 NioEventLoop)时创建。
• 第一次 Register 并不是监听 OP_ACCEPT,而是 0:
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this) 。
• 最终监听 OP_ACCEPT 是通过 bind 完成后的 fireChannelActive() 来触发的。
• NioEventLoop 是通过 Register 操作的执行来完成启动的。
• 类似 ChannelInitializer,一些 Hander 可以设计成一次性的,用完就移除,例如授权
经过以上过程Netty就启动起来。
注:内容参考极客时间相关课程