Netty 学习(五):服务端启动核心流程源码说明
Netty 学习(五):服务端启动核心流程源码说明
作者: Grey
原文地址:
CSDN:Netty 学习(五):服务端启动核心流程源码说明
说明
本文使用的 Netty 版本是 4.1.82.Final,
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.82.Final</version>
</dependency>
服务端在启动的时候,主要流程有如下几个
创建服务端的 Channel
初始化服务端的 Channel
注册 Selector
端口绑定
我们可以写一个简单的服务端代码,通过 Debug 的方式查看这几个关键流程的核心代码。
package source;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* 代码阅读
*
* @author <a href="mailto:410486047@qq.com">Grey</a>
* @date 2022/9/12
* @since
*/
public final class SimpleServer {
public static void main(String[] args) throws InterruptedException {
// EventLoopGroup: 服务端的线程模型外观类。这个线程要做的事情
// 就是不停地检测IO事件,处理IO事件,执行任务。
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
// 服务端的一个启动辅助类。通过给它设置一系列参数来绑定端口启动服务。
ServerBootstrap b = new ServerBootstrap();
b
// 设置服务端的线程模型。
// bossGroup 负责不断接收新的连接,将新的连接交给 workerGroup 来处理。
.group(bossGroup, workerGroup)
// 设置服务端的 IO 类型是 NIO。Netty 通过指定 Channel 的类型来指定 IO 类型。
.channel(NioServerSocketChannel.class)
// 服务端启动过程中,需要经过哪些流程。
.handler(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("channelActive");
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) {
System.out.println("channelRegistered");
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
System.out.println("handlerAdded");
}
})
// 用于设置一系列 Handler 来处理每个连接的数据
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
}
});
// 绑定端口同步等待。等服务端启动完毕,才会进入下一行代码
ChannelFuture f = b.bind(8888).sync();
// 等待服务端关闭端口绑定,这里的作用是让程序不会退出
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
通过
ChannelFuture f = b.bind(8888).sync();
的bind
方法,进入源码进行查看。
首先,进入的是AbstractBootstrap
中,调用的最关键的方法是如下两个:
……
private ChannelFuture doBind(final SocketAddress localAddress) {
……
final ChannelFuture regFuture = initAndRegister();
……
doBind0(regFuture, channel, localAddress, promise);
……
}
……
进入initAndResgister()
方法中
……
final ChannelFuture initAndRegister() {
……
// channel 的新建
channel = channelFactory.newChannel();
// channel 的初始化
init(channel);
……
}
……
这里完成了 Channel 的新建和初始化,Debug 进去,发现channelFactory.newChannel()
实际上是调用了ReflectiveChannelFactory
的newChannel
方法,
public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> {
……
private final Constructor<? extends T> constructor;
public ReflectiveChannelFactory(Class<? extends T> clazz) {
……
this.constructor = clazz.getConstructor();
……
}
@Override
public T newChannel() {
……
return constructor.newInstance();
……
}
……
}
这里调用了反射方法,其实就是将服务端代码中的这一行.channel(NioServerSocketChannel.class)
中的NioServerSocketChannel.class
传入进行对象创建,创建一个NioServerSocketChannel
实例。
在创建NioServerSocketChannel
的时候,调用了NioServerSocketChannel
的构造方法,构造方法的主要逻辑如下
……
public NioServerSocketChannel(SelectorProvider provider, InternetProtocolFamily family) {
this(newChannel(provider, family));
}
public NioServerSocketChannel(ServerSocketChannel channel) {
super(null, channel, SelectionKey.OP_ACCEPT);
config = new NioServerSocketChannelConfig(this, javaChannel().socket());
}
private static ServerSocketChannel newChannel(SelectorProvider provider, InternetProtocolFamily family) {
……
ServerSocketChannel channel =
SelectorProviderUtil.newChannel(OPEN_SERVER_SOCKET_CHANNEL_WITH_FAMILY, provider, family);
return channel == null ? provider.openServerSocketChannel() : channel;
……
}
……
其中provider.openServerSocketChannel()
就是调用底层 JDK 的 API,获取了 JDK 底层的java.nio.channels.ServerSocketChannel
。
通过super(null, channel, SelectionKey.OP_ACCEPT);
一路跟踪进去,进入AbstractNioChannel
中,
protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
super(parent);
……
ch.configureBlocking(false);
……
}
关键代码是ch.configureBlocking(false)
,设置 I/O 模型为非阻塞模式。
通过super(parent)
跟踪上去,
protected AbstractChannel(Channel parent) {
this.parent = parent;
id = newId();
unsafe = newUnsafe();
pipeline = newChannelPipeline();
}
其中 id 是 Netty 中每条 Channel 的唯一标识。
以上就是服务端 Channel 的创建过程。
接下来是服务端 Channel 的初始化过程,回到AbstractBootstrap.initAndResgister()
方法
……
final ChannelFuture initAndRegister() {
……
// channel 的新建
channel = channelFactory.newChannel();
// channel 的初始化
init(channel);
……
}
……
其中的init(channel)
方法就是服务端的 Channel 的初始化过程,Debug 进入,发现是调用了ServerBootstrap.init(channel)
方法,
@Override
void init(Channel channel) {
……
// 设置一些 Channel 的属性和配置信息
……
p.addLast(new ChannelInitializer<Channel>() {
@Override
public void initChannel(final Channel ch) {
final ChannelPipeline pipeline = ch.pipeline();
ChannelHandler handler = config.handler();
if (handler != null) {
pipeline.addLast(handler);
}
ch.eventLoop().execute(new Runnable() {
@Override
public void run() {
pipeline.addLast(new ServerBootstrapAcceptor(
ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});
}
});
}
其核心代码如上,主要用于定义服务端启动过程中需要执行哪些逻辑。主要分为两块:
一块是添加用户自定义的处理逻辑到服务端启动流程。
另一块是添加一个特殊的处理逻辑,ServerBootstrapAcceptor 是一个接入器,接受新请求,把新的请求传递给某个事件循环器。
以上就是服务端的 Channel 的初始化过程。接下来是服务端 Channel 的注册 Selector 的过程。
@Override
protected void doRegister() throws Exception {
boolean selected = false;
for (;;) {
try {
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
return;
} catch (CancelledKeyException e) {
if (!selected) {
// Force the Selector to select now as the "canceled" SelectionKey may still be
// cached and not removed because no Select.select(..) operation was called yet.
eventLoop().selectNow();
selected = true;
} else {
// We forced a select operation on the selector before but the SelectionKey is still cached
// for whatever reason. JDK bug ?
throw e;
}
}
}
}
在这个步骤中,我们可以看到关于 JDK 底层的操作
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
首先拿到在前面过程中创建的 JDK 底层的 Channel,然后调用 JDK 的 register() 方法,将 this 也即 NioServerSocketChannel 对象当作 attachment 绑定到 JDK 的 Selector 上,这样后续从 Selector 拿到对应的事件之后,就可以把 Netty 领域的 Channel 拿出来。
接下来是服务端绑定端口的逻辑,见AbstractBootstrap
中的doBind0
方法
private static void doBind0(
final ChannelFuture regFuture, final Channel channel,
final SocketAddress localAddress, final ChannelPromise promise) {
// This method is invoked before channelRegistered() is triggered. Give user handlers a chance to set up
// the pipeline in its channelRegistered() implementation.
channel.eventLoop().execute(new Runnable() {
@Override
public void run() {
if (regFuture.isSuccess()) {
channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
} else {
promise.setFailure(regFuture.cause());
}
}
});
}
图例
本文所有图例见:processon: Netty学习笔记
代码
更多内容见:Netty专栏
参考资料
Netty 学习(五):服务端启动核心流程源码说明的更多相关文章
- [Android]从Launcher开始启动App流程源码分析
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5017056.html 从Launcher开始启动App流程源码 ...
- Netty服务端启动过程相关源码分析
1.Netty 是怎么创建服务端Channel的呢? 我们在使用ServerBootstrap.bind(端口)方法时,最终调用其父类AbstractBootstrap中的doBind方法,相关源码如 ...
- Spring IOC容器核心流程源码分析
简单介绍 Spring IOC的核心方法就在于refresh方法,这个方法里面完成了Spring的初始化.准备bean.实例化bean和扩展功能的实现. 这个方法的作用是什么? 它是如何完成这些功能的 ...
- Netty源码解析 -- 服务端启动过程
本文通过阅读Netty源码,解析Netty服务端启动过程. 源码分析基于Netty 4.1 Netty是一个高性能的网络通信框架,支持NIO,OIO等多种IO模式.通常,我们都是使用NIO模式,该系列 ...
- (二)Netty源码学习笔记之服务端启动
尊重原创,转载注明出处,原文地址:http://www.cnblogs.com/cishengchongyan/p/6129971.html 本文将不会对netty中每个点分类讲解,而是一个服务端启 ...
- Netty 学习(一):服务端启动 & 客户端启动
Netty 学习(一):服务端启动 & 客户端启动 作者: Grey 原文地址: 博客园:Netty 学习(一):服务端启动 & 客户端启动 CSDN:Netty 学习(一):服务端启 ...
- Netty之旅三:Netty服务端启动源码分析,一梭子带走!
Netty服务端启动流程源码分析 前记 哈喽,自从上篇<Netty之旅二:口口相传的高性能Netty到底是什么?>后,迟迟两周才开启今天的Netty源码系列.源码分析的第一篇文章,下一篇我 ...
- Netty 4源码解析:服务端启动
Netty 4源码解析:服务端启动 1.基础知识 1.1 Netty 4示例 因为Netty 5还处于测试版,所以选择了目前比较稳定的Netty 4作为学习对象.而且5.0的变化也不像4.0这么大,好 ...
- 原理剖析-Netty之服务端启动工作原理分析(下)
一.大致介绍 1.由于篇幅过长难以发布,所以本章节接着上一节来的,上一章节为[原理剖析(第 010 篇)Netty之服务端启动工作原理分析(上)]: 2.那么本章节就继续分析Netty的服务端启动,分 ...
随机推荐
- 西文字符与中文GBK编码的区别
一般来讲二者读取的时候西文字符的数值是正,而中文字符的数值是负的,此时读取的是中文字符的前一半,需要再读取一个char类型的数据,在大多数运行环境下这个规则都是用. ps:转自算法竞赛的笔记,要注意在 ...
- Nuget打包并上传教程
一.准备 1 . 下载 Download NuGet.exe 2 . windows 系统下设置环境变量 path中 或者 在dos 命令窗口下cd转到 nuget.exe 所在目录,这里为了每次使用 ...
- Python3.7+jieba(结巴分词)配合Wordcloud2.js来构造网站标签云(关键词集合)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_138 其实很早以前就想搞一套完备的标签云架构了,迫于没有时间(其实就是懒),一直就没有弄出来完整的代码,说到底标签对于网站来说还是 ...
- 关于hive分区,你知道多少呢?
文末查看关键字,回复赠书 一.理论基础 1.Hive分区背景 在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入 ...
- java-servlet-转发AND路径
转发: a) 什么是转发?一个web组件将未完成的任务交给另一个web组件继续做.通常是一个servlet将数据获取之后转交给jsp进行展现.注:web组件值得是servlet或者jsp b) 如何转 ...
- 删除MySQL数据用户
mysql删除用户的方法: 1.使用"drop user 用户名;"命令删除: 2.使用"delete from user where user='用户名' and ho ...
- 解决git报错
解决git报错:fatal: unable to access "https://github.com/.../.git/" 1.在git中执行(记得分开执行) git confi ...
- 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)
0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...
- RTMP播放器开发填坑之道
好多开发者提到,在目前开源播放器如此泛滥的情况下,为什么还需要做自研框架的RTMP播放器,自研和开源播放器,到底好在哪些方面?以下大概聊聊我们的一点经验,感兴趣的,可以关注 github: 1. 低延 ...
- KingbaseES R3 集群主备切换信号量(semctl)错误故障分析案例
案例说明: 某项目KingbaseES R3 一主一备流复制集群在主备切换测试中出现故障,导致主备无法正常切换:由于bm要求,数据库相关日志无法从主机中获取,只能在现场进行分析:通过对比主备切换时的时 ...