核心概念

Netty 的核心组件构成了其高性能网络框架的基础架构,主要包括:

  1. Channel:网络连接通道
  2. EventLoop:事件循环处理器
  3. ChannelPipeline:责任链处理管道
  4. ChannelHandler:业务逻辑处理器
  5. ByteBuf:字节缓冲区
  6. Bootstrap/ServerBootstrap:启动引导类

1. Channel(通道)

Channel 是 Netty 对网络连接的抽象,类似于 Java NIO 的 SocketChannel

核心接口

public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparable<Channel> {
    ChannelId id();                    // 全局唯一标识
    EventLoop eventLoop();             // 绑定的 EventLoop
    Channel parent();                  // 父 Channel(服务端)
    ChannelConfig config();            // 配置参数
    ChannelPipeline pipeline();        // 处理链
    boolean isActive();                // 是否激活
    ChannelFuture write(Object msg);   // 写数据
    ChannelFuture close();             // 关闭连接
}

常用实现

  • NioSocketChannel:NIO TCP 客户端 Channel
  • NioServerSocketChannel:NIO TCP 服务端 Channel
  • EpollSocketChannel:Linux Epoll 优化版本

2. EventLoop(事件循环)

EventLoop 负责处理 Channel 上的所有 I/O 事件和任务。

核心特性

public interface EventLoop extends OrderedEventExecutor, EventLoopGroup {
    EventLoopGroup parent();
}

关键机制

  • 一个 EventLoop 可以处理多个 Channel
  • 一个 Channel 只绑定到一个 EventLoop
  • 保证线程安全,无需加锁

工作流程

// NioEventLoop 核心循环
protected void run() {
    for (;;) {
        select(wakenUp.getAndSet(false));  // 1. 检测 I/O 事件
        processSelectedKeys();              // 2. 处理 I/O 事件
        runAllTasks();                      // 3. 处理异步任务
    }
}

3. ChannelPipeline(管道)

ChannelPipeline 是责任链模式的实现,管理 ChannelHandler 的执行顺序。

结构示意

Head → Handler1 → Handler2 → Handler3 → Tail
 ↓         ↓         ↓          ↓        ↓
Inbound  Inbound  Outbound  Outbound  Outbound

代码示例

ch.pipeline()
  .addLast("decoder", new StringDecoder())        // 入站处理
  .addLast("encoder", new StringEncoder())        // 出站处理
  .addLast("handler", new BusinessHandler());     // 业务处理

事件传播

  • 入站事件fireChannelRead() → 从 Head 到 Tail
  • 出站事件write() → 从 Tail 到 Head

4. ChannelHandler(处理器)

ChannelHandler 负责处理具体的业务逻辑。

核心接口

public interface ChannelHandler {
    void handlerAdded(ChannelHandlerContext ctx);
    void handlerRemoved(ChannelHandlerContext ctx);
    void exceptionCaught(ChannelHandlerContext ctx, Throwable cause);
}

两大类型

ChannelInboundHandler(入站处理)

public class MyInboundHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        // 处理接收到的数据
        ByteBuf buf = (ByteBuf) msg;
        System.out.println(buf.toString(CharsetUtil.UTF_8));
        ctx.fireChannelRead(msg);  // 传递给下一个 Handler
    }
}

ChannelOutboundHandler(出站处理)

public class MyOutboundHandler extends ChannelOutboundHandlerAdapter {
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
        // 处理发送数据
        ctx.write(msg, promise);
    }
}

5. ByteBuf(字节缓冲区)

ByteBuf 是 Netty 对 Java NIO ByteBuffer 的增强版本。

优势对比

| 特性 | ByteBuf | ByteBuffer | |——|———|———–| | 读写索引 | readerIndex/writerIndex 分离 | position 共用,需手动 flip() | | 容量扩展 | 自动扩容 | 固定容量 | | 池化支持 | 支持内存池化 | 不支持 | | 零拷贝 | CompositeByteBuf、slice | 不支持 | | 引用计数 | 支持(防止内存泄漏) | 不支持 |

使用示例

// 创建 ByteBuf
ByteBuf buf = Unpooled.buffer(256);

// 写入数据
buf.writeInt(100);
buf.writeBytes("Hello".getBytes());

// 读取数据
int value = buf.readInt();
byte[] data = new byte[5];
buf.readBytes(data);

// 释放资源
buf.release();  // 引用计数 -1

内存管理

// 池化 ByteBuf(推荐)
ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer(1024);

// 非池化 ByteBuf
ByteBuf buf = Unpooled.buffer(1024);

6. Bootstrap(启动引导类)

ServerBootstrap(服务端)

ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
 .channel(NioServerSocketChannel.class)
 .option(ChannelOption.SO_BACKLOG, 128)          // 服务端选项
 .childOption(ChannelOption.SO_KEEPALIVE, true)  // 客户端连接选项
 .childHandler(new ChannelInitializer<SocketChannel>() {
     @Override
     protected void initChannel(SocketChannel ch) {
         ch.pipeline().addLast(new MyHandler());
     }
 });
b.bind(8080).sync();

Bootstrap(客户端)

Bootstrap b = new Bootstrap();
b.group(group)
 .channel(NioSocketChannel.class)
 .handler(new ChannelInitializer<SocketChannel>() {
     @Override
     protected void initChannel(SocketChannel ch) {
         ch.pipeline().addLast(new MyHandler());
     }
 });
b.connect("localhost", 8080).sync();

组件协作流程

服务端接收数据流程

1. Selector 检测到读事件
         ↓
2. NioEventLoop 调用 Channel.read()
         ↓
3. 数据读入 ByteBuf
         ↓
4. 触发 Pipeline.fireChannelRead()
         ↓
5. 依次执行 ChannelHandler 链
         ↓
6. 业务处理完成

客户端发送数据流程

1. 调用 ctx.writeAndFlush(msg)
         ↓
2. Pipeline 从 Tail 向 Head 传播
         ↓
3. 经过 Encoder 编码
         ↓
4. 写入 Channel 的发送缓冲区
         ↓
5. Selector 检测到写事件
         ↓
6. 数据发送到网卡

面试答题总结

Netty 的六大核心组件

  1. Channel:网络连接抽象,支持 NIO、Epoll 等多种实现
  2. EventLoop:事件循环,处理 I/O 事件和任务,一个 Channel 绑定一个 EventLoop
  3. ChannelPipeline:责任链模式,管理 Handler 的执行顺序
  4. ChannelHandler:业务逻辑处理器,分为入站和出站两类
  5. ByteBuf:增强版字节缓冲区,支持池化、零拷贝、引用计数
  6. Bootstrap:启动引导类,简化服务端和客户端的配置

核心设计思想

  • EventLoop 与 Channel 绑定,保证线程安全
  • Pipeline 责任链,灵活处理业务逻辑
  • ByteBuf 池化和零拷贝,提升性能

典型应用:理解这些组件是掌握 Netty 的基础,也是框架扩展的关键。