相关文章:
写在开头:本文为学习后的总结,可能有不到位的地方,错误的地方,欢迎各位指正。
在前文中,我们对Netty的内容做了简单的介绍,本文我们会结合Netty的流程图相对深入一些的介绍下其中的重要组件。

目录
事件循环对象 EventLoop本质是一个单线程执行器(同时维护了一个 Selector),支持异步提交执行任务,线程启动时会调用 NioEventLoop 的 run 方法,执行 I/O 任务和非 I/O 任务:
它的继承关系如下:
事件循环组EventLoopGroup是一组 EventLoop,Channel 一般会调用 EventLoopGroup 的 register 方法来绑定其中一个 EventLoop,后续这个 Channel 上的 io 事件都由此 EventLoop 来处理(保证了 io 事件处理时的线程安全)。
继承自 netty 自己的 EventExecutorGroup,实现了 Iterable 接口提供遍历 EventLoop 的,并提供了next方法获取集合中下一个 EventLoop。

EventLoop初始化时可以指定线程数,可以不指定,如果不指定,这里会传0用来调用父类MultithreadEventLoopGroup中得构造方法。
- public class NioEventLoopGroup extends MultithreadEventLoopGroup {
- public NioEventLoopGroup() {
- this(0);
- }
- public NioEventLoopGroup(int nThreads) {
- this(nThreads, (Executor) null);
- }
- ...
- }
MultithreadEventLoopGroup类提供1个默认线程数DEFAULT_EVENT_LOOP_THREADS,比较1与Netty系统参数"io.netty.eventLoopThreads"(如未设置则使用CPU核心数*2)中得最大值。当传入得初始化线程数为0时就使用这个默认线程数。
- public abstract class MultithreadEventLoopGroup extends MultithreadEventExecutorGroup implements EventLoopGroup {
-
- private static final InternalLogger logger = InternalLoggerFactory.getInstance(MultithreadEventLoopGroup.class);
-
- // 默认线程数
- private static final int DEFAULT_EVENT_LOOP_THREADS;
-
- static {
- // 静态代码块中,给默认线程数DEFAULT_EVENT_LOOP_THREADS 赋值
- // 比较1与Netty系统参数"io.netty.eventLoopThreads"(如未设置则使用CPU核心数*2)中得最大值
- DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(
- "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
-
- if (logger.isDebugEnabled()) {
- logger.debug("-Dio.netty.eventLoopThreads: {}", DEFAULT_EVENT_LOOP_THREADS);
- }
- }
-
- // 如果传入得线程数为0,则使用默认线程数
- protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) {
- super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args);
- }
- ...
- }
EventLoop处理普通与定时任务
- public class TestEventLoop {
- public static void main(String[] args) {
- // 创建拥有两个EventLoop的NioEventLoopGroup,对应两个线程
- EventLoopGroup group = new NioEventLoopGroup(2);
- // 通过next方法可以获得下一个 EventLoop
- System.out.println(group.next());
- System.out.println(group.next());
-
- // 通过EventLoop执行普通任务
- group.next().execute(()->{
- System.out.println(Thread.currentThread().getName() + " hello");
- });
-
- // 通过EventLoop执行定时任务(表示立即执行(0),每次间隔1秒(1))
- group.next().scheduleAtFixedRate(()->{
- System.out.println(Thread.currentThread().getName() + " hello2");
- }, 0, 1, TimeUnit.SECONDS);
-
- // 优雅地关闭
- group.shutdownGracefully();
- }
- }
输出结果如下
- io.netty.channel.nio.NioEventLoop@7bb11784
- io.netty.channel.nio.NioEventLoop@33a10788
- nioEventLoopGroup-2-1 hello
- nioEventLoopGroup-2-2 hello2
- nioEventLoopGroup-2-2 hello2
- nioEventLoopGroup-2-2 hello2
优雅关闭 shutdownGracefully 方法。该方法会首先切换 EventLoopGroup 到关闭状态从而拒绝新的任务的加入,然后在任务队列的任务都处理完成后,停止线程的运行。从而确保整体应用是在正常有序的状态下退出的。
下面演示下处理IO任务:
服务器代码
- public class MyServer {
- public static void main(String[] args) {
- new ServerBootstrap()
- // 添加EventLoop
- .group(new NioEventLoopGroup())
- // 选择服务端Channel实现
- .channel(NioServerSocketChannel.class)
- // 添加处理器
- .childHandler(new ChannelInitializer
() { - @Override // 连接建立后调用
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- // 将服务端收到的数据转bytebuf后再转String
- ByteBuf buf = (ByteBuf) msg;
- log.debug(buf.toString(Charset.defaultCharset()));
-
- }
- });
- }
- })
- .bind(8080);
- }
- }
客户端代码
- public class MyClient {
- public static void main(String[] args) throws IOException, InterruptedException {
- Channel channel = new Bootstrap()
- // 添加EventLoop
- .group(new NioEventLoopGroup())
- // 选择客户端Channel实现
- .channel(NioSocketChannel.class)
- // 添加处理器
- .handler(new ChannelInitializer
() { - @Override // 连接建立后调用
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- socketChannel.pipeline().addLast(new StringEncoder());
- }
- })
- // 连接到服务器
- .connect(new InetSocketAddress("localhost", 8080))
- .sync()
- .channel();
- System.out.println(channel);
- System.in.read();
- }
- }
Bootstrap的group()方法可以传入两个EventLoopGroup参数,分别负责处理Accept事件(boss线程)与Read/Write(worker线程)(参考下我们前一篇文章中介绍得流程图)

- public class MyServer {
- public static void main(String[] args) {
- new ServerBootstrap()
- // 两个Group,分别为Boss(负责Accept事件),Worker(负责读写事件)
- .group(new NioEventLoopGroup(1), new NioEventLoopGroup(2))
- // ...
- // 上文中处理IO任务得代码
- }
- }
使用上文中多个客户端分别发送 hello 结果
- nioEventLoopGroup-3-1 hello1
- nioEventLoopGroup-3-2 hello2
- nioEventLoopGroup-3-1 hello3
- nioEventLoopGroup-3-2 hello4
- nioEventLoopGroup-3-2 hello4
可以看出,一个EventLoop可以负责多个Channel,且EventLoop一旦与Channel绑定,则一直负责处理该Channel中的事件。

当有的任务需要较长的时间处理时,可以再添加一个非NioEventLoopGroup,避免同一个NioEventLoop中的其他Channel在较长的时间内都无法得到处理。
- public class MyServer {
- public static void main(String[] args) {
- // 增加自定义的非NioEventLoopGroup
- EventLoopGroup group = new DefaultEventLoopGroup();
-
- new ServerBootstrap()
- .group(new NioEventLoopGroup(1), new NioEventLoopGroup(2))
- .channel(NioServerSocketChannel.class)
- .childHandler(new ChannelInitializer
() { - @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- // 增加两个handler,第一个使用NioEventLoopGroup处理,第二个使用自定义EventLoopGroup处理
- socketChannel.pipeline().addLast("nioHandler",new ChannelInboundHandlerAdapter() {
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- ByteBuf buf = (ByteBuf) msg;
- System.out.println(Thread.currentThread().getName() + " " + buf.toString(StandardCharsets.UTF_8));
- // 调用下一个handler
- ctx.fireChannelRead(msg);
- }
- })
- // 该handler绑定自定义的Group
- .addLast(group, "myHandler", new ChannelInboundHandlerAdapter() {
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- ByteBuf buf = (ByteBuf) msg;
- System.out.println(Thread.currentThread().getName() + " " + buf.toString(StandardCharsets.UTF_8));
- }
- });
- }
- })
- .bind(8080);
- }
- }
启动四个客户端发送数据
- nioEventLoopGroup-4-1 hello1
- defaultEventLoopGroup-2-1 hello1
- nioEventLoopGroup-4-2 hello2
- defaultEventLoopGroup-2-2 hello2
- nioEventLoopGroup-4-1 hello3
- defaultEventLoopGroup-2-3 hello3
- nioEventLoopGroup-4-2 hello4
- defaultEventLoopGroup-2-4 hello4
可以看出,客户端与服务器之间的事件,被nioEventLoopGroup和defaultEventLoopGroup分别处理。

不同的EventLoopGroup切换的实现原理如下:由上面的图可以看出,当handler中绑定的Group不同时,需要切换Group来执行不同的任务。
- static void invokeChannelRead(final AbstractChannelHandlerContext next, Object msg) {
- final Object m = next.pipeline.touch(ObjectUtil.checkNotNull(msg, "msg"), next);
- // 获取下一个handler得EventLoop
- EventExecutor executor = next.executor();
-
- // 判断当前handler中得线程与executor(下一个handler得EventLoop)是否为同一个
- if (executor.inEventLoop()) {
- // 使用当前EventLoopGroup中的EventLoop来处理任务
- next.invokeChannelRead(m);
- } else {
- // 将要执行得代码作为任务交给executor处理
- executor.execute(new Runnable() {
- public void run() {
- next.invokeChannelRead(m);
- }
- });
- }
- }
Channel在Netty中与NIO中一样,是数据的通道,Channel 的常用方法包括:
在客户端代码中,我们使用connect来连接服务端,从而返回一个供我们用作数据传输的channel,建立连接(connect)的过程是异步非阻塞的,主线程调用后不会被阻塞,真正去执行连接操作的是NIO线程(即NioEventLoop 中的线程)。
- public class MyClient {
- public static void main(String[] args) throws IOException, InterruptedException {
- ChannelFuture channelFuture = new Bootstrap()
- .group(new NioEventLoopGroup())
- .channel(NioSocketChannel.class)
- .handler(new ChannelInitializer
() { - @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- socketChannel.pipeline().addLast(new StringEncoder());
- }
- })
- // 该方法为异步非阻塞方法,主线程调用后不会被阻塞,真正去执行连接操作的是NIO线程
- // NIO线程:NioEventLoop 中的线程
- .connect(new InetSocketAddress("localhost", 8080));
-
-
- // 获取客户端-服务器之间的Channel对象
- Channel channel = channelFuture.channel();
- channel.writeAndFlush("hello world");
- System.in.read();
- }
- }
这样将会导致一个问题,即代码虽然继续向下运行到了channelFuture.channel(),但此时我们并没有获取到channel,所以下文中的hello world是无法发送的。
为了解决这个问题,Netty分别提供了同步与异步的解决方案。
在connect后,使用channelFuture.sync()方法,阻塞主线程,同步处理结果,等待连接真正建立好以后,再去获得 Channel 传递数据。使用该方法,获取 Channel 和发送数据的线程都是主线程。
- // 该方法用于等待连接真正建立
- channelFuture.sync();
-
- // 获取客户端-服务器之间的Channel对象
- Channel channel = channelFuture.channel();
异步获取建立连接后的 Channel 和发送数据,使得执行这些操作的线程是 NIO 线程。
- // 当connect方法执行完毕后,也就是连接真正建立后
- // 会在NIO线程中调用operationComplete方法
- channelFuture.addListener(new ChannelFutureListener() {
- @Override
- public void operationComplete(ChannelFuture channelFuture) throws Exception {
- Channel channel = channelFuture.channel();
- channel.writeAndFlush("hello world");
- }
- });
当我们要关闭channel时,可以调用channel.close()方法进行关闭。但是该方法也是一个异步方法。真正的关闭操作并不是在调用该方法的线程中执行的,而是在NIO线程中执行真正的关闭操作。
同连接的建立一样,如果我们想在channel真正关闭以后,执行一些额外的操作,可以选择同于与异步两种方法实现。
通过channel.closeFuture()方法获得对应的ChannelFuture对象,然后调用sync()方法阻塞执行操作的线程,等待channel真正关闭后,再执行其他操作。
- // 获得closeFuture对象
- ChannelFuture closeFuture = channel.closeFuture();
-
- // 同步等待NIO线程执行完close操作
- closeFuture.sync();
调用closeFuture.addListener方法,添加close的后续操作
- closeFuture.addListener(new ChannelFutureListener() {
- @Override
- public void operationComplete(ChannelFuture channelFuture) throws Exception {
- // 等待channel关闭后才执行的操作
- System.out.println("关闭之后执行一些额外操作...");
- // 关闭EventLoopGroup
- group.shutdownGracefully();
- }
- });
下面展示输入q后连接关闭的样例:
- public class ReadClient {
- public static void main(String[] args) throws InterruptedException {
- // 创建EventLoopGroup,使用完毕后关闭
- NioEventLoopGroup group = new NioEventLoopGroup();
-
- ChannelFuture channelFuture = new Bootstrap()
- .group(group)
- .channel(NioSocketChannel.class)
- .handler(new ChannelInitializer
() { - @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- socketChannel.pipeline().addLast(new StringEncoder());
- }
- })
- .connect(new InetSocketAddress("localhost", 8080));
- channelFuture.sync();
-
- Channel channel = channelFuture.channel();
- Scanner scanner = new Scanner(System.in);
-
- // 创建一个线程用于输入并向服务器发送
- new Thread(()->{
- while (true) {
- String msg = scanner.next();
- if ("q".equals(msg)) {
- // 关闭操作是异步的,在NIO线程中执行
- channel.close();
- break;
- }
- channel.writeAndFlush(msg);
- }
- }, "inputThread").start();
-
- // 获得closeFuture对象
- ChannelFuture closeFuture = channel.closeFuture();
- System.out.println("waiting close...");
-
- // 同步等待NIO线程执行完close操作
- closeFuture.sync();
-
- // 关闭之后执行一些操作,可以保证执行的操作一定是在channel关闭以后执行的
- System.out.println("关闭之后执行一些额外操作...");
-
- // 关闭EventLoopGroup
- group.shutdownGracefully();
- }
- }
JDK创建了Future来提供主线程与异步线程之间的交互功能,netty 中的Future与 jdk 中的 Future 一样,但又有所不同。
通过向线程池中提交任务,可以获得一个线程池的返回的future结果,主线程通过future来获取异步线程的执行结果。缺点在于,JDK中的future只能同步的获取结果,即必须要等到异步线程执行完。
- public class JdkFuture {
- public static void main(String[] args) throws ExecutionException, InterruptedException {
- // 创建线程池
- ExecutorService service = Executors.newFixedThreadPool(2);
-
- // 提交任务,获得Future对象
- Future
future = service.submit(new Callable() { - @Override
- public Integer call() throws Exception {
- log.debug("开始计算");
- Thread.sleep(1000);
- return 50;
- }
- });
- // 主线程通过future来获取结果
- log.debug("等待结果");
- log.debug("结果是{}",future.get());
- }
- }
在Netty中,Future可以异步的获取结果。
- public class NettyFuture {
- public static void main(String[] args) throws ExecutionException, InterruptedException {
- NioEventLoopGroup group = new NioEventLoopGroup();
-
- // 获得 EventLoop 对象
- EventLoop eventLoop = group.next();
- Future
future = eventLoop.submit(new Callable() { - @Override
- public Integer call() throws Exception {
- log.debug("开始计算");
- Thread,sleep(1000);
- return 50;
- }
- });
-
- // NIO线程中异步获取结果
- future.addListener(new GenericFutureListener
super Integer>>() { - @Override
- public void operationComplete(Future super Integer> future) throws Exception {
- log.debug("接收结果:{}",future.getNow());
- }
- });
- }
- }
Netty中的Future对象,可以通过EventLoop的sumbit()方法得到
Promise相当于一个容器,可以用于存放各个线程中的结果,然后让其他线程去获取该结果,这就类似消息队列的功能。
- public class NettyPromise {
- public static void main(String[] args) throws ExecutionException, InterruptedException {
- // 创建EventLoop
- EventLoop eventLoop = new NioEventLoopGroup().next();
-
- // 创建Promise对象,用于存放结果
- DefaultPromise
promise = new DefaultPromise<>(eventLoop); -
- new Thread(()->{
- // 任意一个线程执行计算,计算完毕后向promise中填充结果
- log.debug("开始计算");
- try {...} catch (InterruptedException e) {
- e.printStackTrace();
- }
- // 自定义线程向Promise中存放结果
- promise.setSuccess(50);
- }).start();
-
- // 接收结果的线程
- log.debug("等待结果");
- log.debug("结果是:{}",promise.get());
- }
- }
ChannelPipeline是Netty处理请求的责任链,ChannelHandler则是具体处理请求的处理器。实际上每一个channel都有一个处理器的流水线。
ChannelHandler用来处理Channel.上的各种事件,分为入站、出站两种。
所有ChannelHandler被连成一串,这就是Pipeline。打个比喻,每个Channel是一个产品的加工车间,Pipeline 是车间中的流水线,ChannelHandler 就是流水线上的各道工序,而后面要讲的ByteBuf是原材料,经过很多工序的加工:先经过一道道入站工序,再经过一道道出站工序最终变成产品。
pipeline相当于处理器的容器。初始化channel时,把channelHandler按顺序装在pipeline中,就可以实现按序执行channelHandler了。

在一个Channel中,只有一个ChannelPipeline。该pipeline在Channel被创建的时候创建。ChannelPipeline包含了一个ChannelHander形成的列表,且所有ChannelHandler都会注册到ChannelPipeline中。
- public class PipeLineServer {
- public static void main(String[] args) {
- new ServerBootstrap()
- .group(new NioEventLoopGroup())
- .channel(NioServerSocketChannel.class)
- .childHandler(new ChannelInitializer
() { - @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- // 在socketChannel的pipeline中添加handler
- // pipeline中handler是带有head与tail节点的双向链表,的实际结构为
- // head <-> handler1 <-> ... <-> handler4 <->tail
- // Inbound主要处理入站操作,一般为读操作,发生入站操作时会触发Inbound方法
- // 入站时,handler是从head向后调用的
- socketChannel.pipeline().addLast("handler1" ,new ChannelInboundHandlerAdapter() {
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- System.out.println(Thread.currentThread().getName() + " Inbound handler 1");
- // 父类该方法内部会调用fireChannelRead
- // 将数据传递给下一个handler
- super.channelRead(ctx, msg);
- }
- });
- socketChannel.pipeline().addLast("handler2", new ChannelInboundHandlerAdapter() {
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- System.out.println(Thread.currentThread().getName() + " Inbound handler 2");
- // 执行write操作,使得Outbound的方法能够得到调用
- socketChannel.writeAndFlush(ctx.alloc().buffer().writeBytes("Server...".getBytes(StandardCharsets.UTF_8)));
- super.channelRead(ctx, msg);
- }
- });
- // Outbound主要处理出站操作,一般为写操作,发生出站操作时会触发Outbound方法
- // 出站时,handler的调用是从tail向前调用的
- socketChannel.pipeline().addLast("handler3" ,new ChannelOutboundHandlerAdapter(){
- @Override
- public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
- System.out.println(Thread.currentThread().getName() + " Outbound handler 1");
- super.write(ctx, msg, promise);
- }
- });
- socketChannel.pipeline().addLast("handler4" ,new ChannelOutboundHandlerAdapter(){
- @Override
- public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
- System.out.println(Thread.currentThread().getName() + " Outbound handler 2");
- super.write(ctx, msg, promise);
- }
- });
- }
- })
- .bind(8080);
- }
- }
pipeline是结构是一个带有head与tail指针的双向链表,其中的节点为handler,要通过ctx.fireChannelRead(msg)等方法,将当前handler的处理结果传递给下一个handler。
具体结构如下

调用顺序如下

出站处理器ChannelOutboundHandler常用的事件有端口绑定bind、连接服务端connect、写事件write、刷新时间flush、读事件read、主动断开连接disconnect、关闭channel事件close。
出站处理器中的消息流转则主要有socketChannel.writeAndFlush()与ctx.writeAndFlush()。
当handler中调用该方法进行写操作时,会触发Outbound操作,此时是从tail向前寻找OutboundHandler

当handler中调用该方法进行写操作时,会触发Outbound操作,此时是从当前handler向前寻找OutboundHandler

和java NIO一样,ByteBuf是对字节的封装,我们可以使用如下代码池化一个基于直接内存的ByteBuf,初始容量为16。
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16);
为了方便调试,这里提供一个工具方法,下文我们会使用到。
- private static void log(ByteBuf buffer) {
- int length = buffer.readableBytes();
- int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
- StringBuilder buf = new StringBuilder(rows * 80 * 2)
- .append("read index:").append(buffer.readerIndex())
- .append(" write index:").append(buffer.writerIndex())
- .append(" capacity:").append(buffer.capacity())
- .append(NEWLINE);
- appendPrettyHexDump(buf, buffer);
- System.out.println(buf.toString());
- }
当ByteBuf的容量不够时,便会自动扩容。我们创建一个初始容量为16的ByteBuf,并向其中加入超过初始容量的字符。
- public class ByteBufStudy {
- public static void main(String[] args) {
- // 创建ByteBuf
- ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16);
- ByteBufUtil.log(buffer);
-
- // 向buffer中写入数据
- StringBuilder sb = new StringBuilder();
- for(int i = 0; i < 20; i++) {
- sb.append("a");
- }
- buffer.writeBytes(sb.toString().getBytes(StandardCharsets.UTF_8));
-
- // 查看写入结果
- ByteBufUtil.log(buffer);
- }
- }
运行结果如下
- read index:0 write index:0 capacity:16
-
- read index:0 write index:20 capacity:64
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
- |00000010| 61 61 61 61 |aaaa |
- +--------+-------------------------------------------------+----------------+
ByteBuf通过ByteBufAllocator选择allocator并调用对应的buffer()方法来创建的,默认使用直接内存作为ByteBuf,容量为256个字节,可以指定初始容量的大小。
当ByteBuf的容量无法容纳所有数据时,ByteBuf会进行扩容操作。
如果在handler中创建ByteBuf,建议使用ChannelHandlerContext ctx.alloc().buffer()来创建
通过该方法创建的ByteBuf,使用的是基于直接内存的ByteBuf
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16);
可以使用下面的代码来创建池化基于堆的 ByteBuf
ByteBuf buffer = ByteBufAllocator.DEFAULT.heapBuffer(16);
也可以使用下面的代码来创建池化基于直接内存的 ByteBuf
ByteBuf buffer = ByteBufAllocator.DEFAULT.directBuffer(16);
最后我们来验证下创建出的ByteBuf是哪种类型的
- public class ByteBufStudy {
- public static void main(String[] args) {
- ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16);
- System.out.println(buffer.getClass());
-
- buffer = ByteBufAllocator.DEFAULT.heapBuffer(16);
- System.out.println(buffer.getClass());
-
- buffer = ByteBufAllocator.DEFAULT.directBuffer(16);
- System.out.println(buffer.getClass());
- }
- }
结果如下
- // 使用池化的直接内存
- class io.netty.buffer.PooledUnsafeDirectByteBuf
-
- // 使用池化的堆内存
- class io.netty.buffer.PooledUnsafeHeapByteBuf
-
- // 使用池化的直接内存
- class io.netty.buffer.PooledUnsafeDirectByteBuf
和其他资源一样,ByteBuf的创建也是需要消耗不少资源的,于是我们便想到了池化技术(类似数据库连接池、线程池等)。池化技术的最大意义在于可以重用 ByteBuf,优点有
池化功能是否开启,可以通过下面的系统环境变量来设置(Netty4.1 之前,池化功能还不成熟,默认是非池化实现)
-Dio.netty.allocator.type={unpooled|pooled}
ByteBuf主要有以下几个组成部分:
在构造ByteBuf时,可传入两个参数,分别代表初始容量和最大容量,若未传入第二个参数(最大容量),最大容量默认为Integer.MAX_VALUE。
当ByteBuf容量无法容纳所有数据时,会进行扩容操作,若超出最大容量,会抛出java.lang.IndexOutOfBoundsException异常。
读写操作不同于ByteBuffer只用position进行控制,ByteBuf分别由读指针和写指针两个指针控制。进行读写操作时,无需进行模式的切换。

主要的写方法展示如下
- public class ByteBufStudy {
- public static void main(String[] args) {
- // 创建ByteBuf
- ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16, 20);
- ByteBufUtil.log(buffer);
-
- // 向buffer中写入数据
- buffer.writeBytes(new byte[]{1, 2, 3, 4});
- ByteBufUtil.log(buffer);
-
- // 写入一个数值
- buffer.writeInt(5);
- ByteBufUtil.log(buffer);
-
- // 和writeInt一样,但是小端写入
- buffer.writeIntLE(6);
- ByteBufUtil.log(buffer);
-
- buffer.writeLong(7);
- ByteBufUtil.log(buffer);
- }
- }
结果如下
- read index:0 write index:0 capacity:16
-
- read index:0 write index:4 capacity:16
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 01 02 03 04 |.... |
- +--------+-------------------------------------------------+----------------+
-
- read index:0 write index:8 capacity:16
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 01 02 03 04 00 00 00 05 |........ |
- +--------+-------------------------------------------------+----------------+
-
- read index:0 write index:12 capacity:16
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 01 02 03 04 00 00 00 05 06 00 00 00 |............ |
- +--------+-------------------------------------------------+----------------+
-
- read index:0 write index:20 capacity:20
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 01 02 03 04 00 00 00 05 06 00 00 00 00 00 00 00 |................|
- |00000010| 00 00 00 07 |.... |
- +--------+-------------------------------------------------+----------------+
还有一类方法是 set 开头的一系列方法,也可以写入数据,但不会改变写指针位置。
读取主要是通过一系列read方法进行读取,读取时会根据读取数据的字节数移动读指针,如果需要重复读取,需要调用buffer.markReaderIndex()对读指针进行标记,并通过buffer.resetReaderIndex()将读指针恢复到mark标记的位置。
- public class ByteBufStudy {
- public static void main(String[] args) {
- // 创建ByteBuf
- ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16, 20);
-
- // 向buffer中写入数据
- buffer.writeBytes(new byte[]{1, 2, 3, 4});
- buffer.writeInt(5);
-
- // 读取4个字节
- System.out.println(buffer.readByte());
- System.out.println(buffer.readByte());
- System.out.println(buffer.readByte());
- System.out.println(buffer.readByte());
- ByteBufUtil.log(buffer);
-
- // 通过mark与reset实现重复读取
- buffer.markReaderIndex();
- System.out.println(buffer.readInt());
- ByteBufUtil.log(buffer);
-
- // 恢复到mark标记处
- buffer.resetReaderIndex();
- ByteBufUtil.log(buffer);
- }
- }
结果如下
- 1
- 2
- 3
- 4
- read index:4 write index:8 capacity:16
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 00 00 00 05 |.... |
- +--------+-------------------------------------------------+----------------+
- 5
- read index:8 write index:8 capacity:16
-
- read index:4 write index:8 capacity:16
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 00 00 00 05 |.... |
- +--------+-------------------------------------------------+----------------+
还有以 get 开头的一系列方法,这些方法不会改变读指针的位置。
当ByteBuf中的容量无法容纳写入的数据时,会进行扩容操作
- buffer.writeLong(7);
- ByteBufUtil.log(buffer);
扩容前后的效果展示如下:
- // 扩容前
- read index:0 write index:12 capacity:16
- ...
-
- // 扩容后
- read index:0 write index:20 capacity:20
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 01 02 03 04 00 00 00 05 06 00 00 00 00 00 00 00 |................|
- |00000010| 00 00 00 07 |.... |
- +--------+-------------------------------------------------+----------------+
扩容规则
- Exception in thread "main" java.lang.IndexOutOfBoundsException: writerIndex(20) + minWritableBytes(8) exceeds maxCapacity(20): PooledUnsafeDirectByteBuf(ridx: 0, widx: 20, cap: 20/20)
- ...
由于 Netty 中有堆外内存(直接内存)的 ByteBuf 实现,堆外内存最好是手动来释放,而不是等 GC 垃圾回收。
Netty 这里采用了引用计数法来控制回收内存,每个 ByteBuf 都实现了 ReferenceCounted 接口。
因为 pipeline 的存在,一般需要将 ByteBuf 传递给下一个 ChannelHandler,如果在每个 ChannelHandler 中都去调用 release ,就失去了传递性(如果在这个 ChannelHandler 内这个 ByteBuf 已完成了它的使命,那么便无须再传递)。
基本规则是,谁是最后使用者,谁负责 release:
(1)起点,对于 NIO 实现来讲,在 io.netty.channel.nio.AbstractNioByteChannel.NioByteUnsafe.read 方法中首次创建 ByteBuf 放入 pipeline(line 163 pipeline.fireChannelRead(byteBuf))
(2)入站 ByteBuf 处理原则
(3)出站 ByteBuf 处理原则
出站消息最终都会转为 ByteBuf 输出,一直向前传,由 HeadContext flush 后 release
(4)异常处理原则
有时候不清楚 ByteBuf 被引用了多少次,但又必须彻底释放,可以循环调用 release 直到返回 true
while (!buffer.release()) {}
当ByteBuf被传到了pipeline的head与tail时,ByteBuf会被其中的方法彻底释放,但前提是ByteBuf被传递到了head与tail中。TailConext中释放ByteBuf的源码如下:
- protected void onUnhandledInboundMessage(Object msg) {
- try {
- logger.debug("Discarded inbound message {} that reached at the tail of the pipeline. Please check your pipeline configuration.", msg);
- } finally {
- // 具体的释放方法
- ReferenceCountUtil.release(msg);
- }
- }
判断传过来的是否为ByteBuf,是的话才需要释放
- public static boolean release(Object msg) {
- return msg instanceof ReferenceCounted ? ((ReferenceCounted)msg).release() : false;
- }
ByteBuf切片是【零拷贝】的体现之一,对原始 ByteBuf 进行切片成多个 ByteBuf,切片后的 ByteBuf 并没有发生内存复制,还是使用原始 ByteBuf 的内存,切片后的 ByteBuf 维护独立的 read,write 指针。
得到分片后的buffer后,要调用其retain方法,使其内部的引用计数加一。避免原ByteBuf释放,导致切片buffer无法使用。修改原ByteBuf中的值,也会影响切片后得到的ByteBuf。

- public class TestSlice {
- public static void main(String[] args) {
- // 创建ByteBuf
- ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(16, 20);
-
- // 向buffer中写入数据
- buffer.writeBytes(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
-
- // 将buffer分成两部分
- ByteBuf slice1 = buffer.slice(0, 5);
- ByteBuf slice2 = buffer.slice(5, 5);
-
- // 需要让分片的buffer引用计数加一
- // 避免原Buffer释放导致分片buffer无法使用
- slice1.retain();
- slice2.retain();
-
- ByteBufUtil.log(slice1);
- ByteBufUtil.log(slice2);
-
- // 更改原始buffer中的值
- System.out.println("===========修改原buffer中的值===========");
- buffer.setByte(0,5);
-
- System.out.println("===========打印slice1===========");
- ByteBufUtil.log(slice1);
- }
- }
结果如下
- read index:0 write index:5 capacity:5
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 01 02 03 04 05 |..... |
- +--------+-------------------------------------------------+----------------+
- read index:0 write index:5 capacity:5
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 06 07 08 09 0a |..... |
- +--------+-------------------------------------------------+----------------+
- ===========修改原buffer中的值===========
- ===========打印slice1===========
- read index:0 write index:5 capacity:5
- +-------------------------------------------------+
- | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
- +--------+-------------------------------------------------+----------------+
- |00000000| 05 02 03 04 05 |..... |
- +--------+-------------------------------------------------+----------------+