Netty 源码版本:4.1.25
为了照顾一些初学者,我们就从Netty的启动类开始 ServerBootstrap,如果基于Netty 开发高性能的服务来说,ServerBootStrap 一般是我们的第一个接触的类。
使用ServerBootstrap 启动服务的例子

ServerBootStrap 构造方法

AbstractBootstrap

ServerBootStrap 第一个重要参数?EventLoopGroup?
从 ServerBootStrap 的构造方法我们可以看出,该类主要用来进行参数配置,其中包括两个
EventLoopGroup .一个就是大家经常听到的Boss 线程,另外一个叫做work线程,借图一张,相信大家都很熟悉。

ServerBootStrap 第二个重要参数?Channel
?源码可以看到Channel是一个类的类型,在Netty 中主要有一下几个ServerSocket类型,接下来我们也主要会围绕这几种类型进行详细的源码解读。

EpollServerSocketChannel: 运行环境在Linux 下使用的一种模式
NioServerSocketChannel: 这是大家可能最熟悉的一种NIO模型,底层依赖JDK的源码实现。
KQueueServerSocketChannel: 是Mac os提供的一种NIO模式,因为基本没有人会选择使用mac作为服务器运行环境,所以对于这一类我们不做过多介绍。
? 接下来几个篇章我们将主要围绕 EpollServerSocketChannel 和?NioServerSocketChannel 两种模式进行源码阅读
ServerBootStrap 第三个重要参数 option
ChannelOption.SO_BACKLOG
具体可以参见linux 系统参数 somax,实际运行中会取两个参数中的最小值(min(SO_BACKLOG,somax))
参数表示TCP连接中同时进行三次握手,还没有被应用程序Accept 的最大个数,
该值要设置合理,太大容易因为 SYN 攻击,太小会对高并发程序来说又有影响。
这个后面我会针对linux内核参数调优进行详细的解释。
ChannelOption.ALLOCATOR
接受消息的Buffer处理类型,默认使用PooledByteBufAllocator
ServerBootStrap 的启动过程,主要有一些几步,每步骤的具体实现我们会在接下来的文章中一一介绍

ChannelRegistry 继承自 ChannelInitializer

?
|