乐于分享
好东西不私藏

Netty 服务端启动源码:bind 之后做了什么(源码系列第2篇)

Netty 服务端启动源码:bind 之后做了什么(源码系列第2篇)

ServerBootstrap.bind() 不是一次简单的端口绑定,而是一条“创建 Channel → 初始化组件 → 注册 EventLoop → 异步执行 bind”的装配链。注册必须先于绑定,是因为 Netty 要先为 Channel 确立唯一的线程归属,之后所有生命周期事件才能在正确的 EventLoop 中有序发生。

Netty 服务端启动代码通常只有十几行。真正让服务开始监听端口的,也只是:

ChannelFuture future = bootstrap.bind(8080).sync();

但这行代码背后至少解决了六个问题:使用哪个 Channel 实现、怎样创建底层 JDK ServerSocketChannel、父 Pipeline 如何初始化、监听 Channel 归属哪个 EventLoop、端口绑定在哪个线程执行、绑定成功如何通知调用者。

如果不理解这些步骤,就很容易把启动阶段的几个概念混为一谈。例如有人认为 channel(NioServerSocketChannel.class) 已经创建了 Channel;有人认为 register() 就是注册端口;也有人认为 bind() 在主线程同步完成。实际上:

  • • channel(...) 只配置 Channel 工厂。
  • • initAndRegister() 才真正创建并初始化 Channel。
  • • register() 是把 Channel 注册到 EventLoop 和 Selector。
  • • bind() 是把底层 ServerSocket 绑定到本地地址。
  • • 返回的 ChannelFuture 表达异步完成结果。

本文沿 AbstractBootstrap.doBind() 这一条主链展开。

源码基线:

  • • Netty netty-4.1.135.Final
  • • Commit:f05f765d81460799c53123a207f665bf3b465171
  • • 重点文件:transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java
  • • 重点类:AbstractBootstrap、ServerBootstrap、AbstractChannel.AbstractUnsafe
  • • 官方源码:https://github.com/netty/netty/tree/netty-4.1.135.Final

1. 启动参数先被保存,bind 时才集中生效

一个典型服务端配置如下:

ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .childOption(ChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() {     @Override     protected void initChannel(SocketChannel ch) {         ch.pipeline().addLast(new FrameDecoder());         ch.pipeline().addLast(new BusinessHandler());     } });

这些链式调用大多没有立即操作 Socket。它们把配置保存进 Bootstrap:

  • • group 保存 parentGroup 和 childGroup。
  • • channel 包装出 ReflectiveChannelFactory。
  • • option 保存父 Channel 参数。
  • • childOption 保存子 Channel 参数。
  • • handler 保存父 Channel Handler。
  • • childHandler 保存未来每个连接的初始化器。

延迟应用配置有两个好处。第一,Bootstrap 可以在真正启动前做完整校验,例如是否设置 EventLoopGroup、ChannelFactory 和 childHandler。第二,Bootstrap 可以被 clone(),用相同模板创建多个监听 Channel,而不需要重新编写配置。

不过这也意味着 Bootstrap 是有状态的启动模板,不宜在多个线程中边修改边绑定。生产代码通常应该先完成配置,再执行一次或少量几次 bind。

2. doBind() 先调用 initAndRegister()

AbstractBootstrap.bind(SocketAddress) 完成地址校验后进入 doBind()。核心代码可简化为:

private ChannelFuture doBind(final SocketAddress localAddress) {    final ChannelFuture regFuture = initAndRegister();    final Channel channel = regFuture.channel();    if (regFuture.cause() != null) {        return regFuture;    }    if (regFuture.isDone()) {        ChannelPromise promise = channel.newPromise();        doBind0(regFuture, channel, localAddress, promise);        return promise;    }    final PendingRegistrationPromise promise =        new PendingRegistrationPromise(channel);    regFuture.addListener(future -> {        if (future.cause() != null) {            promise.setFailure(future.cause());        } else {            promise.registered();            doBind0(regFuture, channel, localAddress, promise);        }    });    return promise;}

源码位置:

  • • 文件:transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java
  • • 方法:doBind(SocketAddress)
  • • 链接:https://github.com/netty/netty/blob/netty-4.1.135.Final/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java

这段代码最重要的不是 Promise 细节,而是顺序:先初始化和注册,再绑定。

为什么不能先把 ServerSocket 绑定端口,再注册到 EventLoop?因为 Channel 的后续生命周期事件,例如 channelRegistered、channelActive、异常通知和关闭,都应该在其所属 EventLoop 中执行。如果先绑定,Channel 可能已经变成 Active,却还没有线程归属和 Pipeline 执行环境,事件顺序会变得难以保证。

所以 Netty 把“注册成功”作为 bind 的前置条件。注册一旦完成,Channel 知道自己的 EventLoop,之后的 bind 会被安全地投递到这个 EventLoop。

3. ChannelFactory 才在此刻创建 NioServerSocketChannel

initAndRegister() 的第一步是:

Channel channel = null;try {    channel = channelFactory.newChannel();    init(channel);} catch (Throwable t) {    if (channel != null) {        channel.unsafe().closeForcibly();        return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE)            .setFailure(t);    }    return new DefaultChannelPromise(new FailedChannel(), GlobalEventExecutor.INSTANCE)        .setFailure(t);}ChannelFuture regFuture = config().group().register(channel);

channel(NioServerSocketChannel.class) 配置的工厂通常通过反射调用无参构造器。创建 NioServerSocketChannel 时,它还会创建底层 JDK ServerSocketChannel,设置为非阻塞,并构造 ChannelConfig、Unsafe 和 Pipeline 等基础对象。

这一步要区分“Netty Channel”与“JDK Channel”:

对象
职责
NioServerSocketChannel
Netty 运行时对象,拥有 Pipeline、EventLoop、Config、Promise
java.nio.channels.ServerSocketChannel
JDK NIO 对象,负责注册 Selector、bind、accept

Netty 没有替代操作系统和 JDK NIO,而是在其上建立了稳定的事件模型。NioServerSocketChannel 把 JDK Channel 包装进统一的 Netty Channel 抽象,使 Pipeline、Future、Allocator 和跨平台 Transport 可以复用。

构造阶段还会指定关注的 SelectionKey 操作。Server Channel 关注 OP_ACCEPT;普通 Socket Channel 关注 OP_READ。这种差异不是 Bootstrap 决定的临时参数,而是各类 Channel 的传输职责决定的。

4. ServerBootstrap.init() 初始化的是父 Channel

Channel 创建后,init(channel) 动态分派到 ServerBootstrap.init(Channel)。这一阶段主要处理父 Channel:

  1. 1. 应用 option() 配置。
  2. 2. 应用父 Channel Attribute。
  3. 3. 获取父 Channel Pipeline。
  4. 4. 加入用户配置的 handler()。
  5. 5. 加入一个延迟安装 ServerBootstrapAcceptor 的初始化 Handler。

简化后的关键逻辑是:

ChannelPipeline p = channel.pipeline();final EventLoopGroup currentChildGroup = childGroup;final ChannelHandler currentChildHandler = childHandler;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(() ->            pipeline.addLast(new ServerBootstrapAcceptor(                ch, currentChildGroup, currentChildHandler,                currentChildOptions, currentChildAttrs,                extensions, connectionMetadata)));    }});

源码位置:

  • • 文件:transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java
  • • 方法:init(Channel)
  • • 链接:https://github.com/netty/netty/blob/netty-4.1.135.Final/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java

ServerBootstrapAcceptor 是连接接入主链的关键 Handler。Boss EventLoop 从 ServerSocket 接收新连接后,会把新建的子 Channel 作为入站消息沿父 Pipeline 传播;Acceptor 收到它,再应用 childHandler、childOption 和 childAttr,并注册到 Worker Group。

这解释了 handler() 与 childHandler() 的本质区别:

  • • handler() 属于监听 Channel,通常处理日志、异常或连接接入层事件。
  • • childHandler() 属于每条已接入的 SocketChannel,处理真正的协议和业务数据。

如果误把业务 Decoder 放进 handler(),它不会处理客户端请求,因为父 Channel 不承载每条连接的数据流。

5. 注册过程为 Channel 选择唯一 EventLoop

config().group().register(channel) 会由 EventLoopGroup 选择一个 EventLoop,再调用该 EventLoop 的 register()。对于 NioEventLoopGroup,选择策略通常是轮询或经过优化的幂次选择。

注册最终进入 AbstractChannel.AbstractUnsafe.register():

public final void register(EventLoop eventLoop, final ChannelPromise promise) {    if (isRegistered()) {        promise.setFailure(new IllegalStateException("registered already"));        return;    }    if (!isCompatible(eventLoop)) {        promise.setFailure(new IllegalStateException("incompatible event loop type"));        return;    }    AbstractChannel.this.eventLoop = eventLoop;    if (eventLoop.inEventLoop()) {        register0(promise);    } else {        eventLoop.execute(() -> register0(promise));    }}

这段源码证明了三个关键事实。

第一,一条 Channel 只能注册一次,不能随意在多个 EventLoop 之间迁移。第二,Channel 类型必须与 EventLoop 兼容,例如 NIO Channel 应注册到 NIO EventLoop。第三,如果当前线程不是目标 EventLoop,真正的 register0() 会被封装成任务投递过去。

register0() 会调用传输实现的 doRegister()。对 NIO Channel 而言,核心动作是把 JDK Channel 注册到 EventLoop 持有的 Selector:

selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);

注意初始 interestOps 是 0,不是马上关注 OP_ACCEPT。注册解决的是 Channel 与 Selector 的关联,真正读取兴趣事件会在 Channel Active 和 read 逻辑中逐步设置。附件 this 使 SelectionKey 就绪后能反查到对应的 Netty Channel。

6. 注册成功会触发 Pipeline 生命周期事件

注册不仅是底层 Selector 操作,还会触发 Pipeline 事件。register0() 大致完成:

  • • 执行 doRegister()。
  • • 标记 registered = true。
  • • 调用 pipeline.invokeHandlerAddedIfNeeded()。
  • • Promise 成功。
  • • 触发 pipeline.fireChannelRegistered()。
  • • 若 Channel 已 Active,则触发 fireChannelActive() 或安排首次读取。

这就是为什么 Handler 的 handlerAdded()、channelRegistered()、channelActive() 存在明确时序。它们不是任意回调:

handlerAdded  -> channelRegistered  -> channelActive  -> channelRead ...  -> channelInactive  -> channelUnregistered  -> handlerRemoved

在服务端启动场景中,注册时通常还没有绑定端口,因此 Channel 尚未 Active。等后面的 bind 成功,状态从未激活变为激活,才触发 channelActive。

如果业务在 handlerAdded() 中假定连接已经可用,可能拿不到有效地址或错误发起写操作。需要使用哪一个生命周期回调,取决于逻辑依赖的是“Handler 已进入 Pipeline”“Channel 已注册线程”还是“连接已经激活”。

7. doBind0() 确保 bind 在 EventLoop 中执行

注册 Future 完成后,doBind0() 不直接调用底层 bind,而是:

private static void doBind0(        final ChannelFuture regFuture,        final Channel channel,        final SocketAddress localAddress,        final ChannelPromise promise) {    channel.eventLoop().execute(() -> {        if (regFuture.isSuccess()) {            channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);        } else {            promise.setFailure(regFuture.cause());        }    });}

为什么已经知道注册成功,还要再次 eventLoop().execute()?因为调用 bind() 的线程通常是用户启动线程,而 Netty 希望 Channel 生命周期操作统一在 EventLoop 中串行执行。这样 bind、active 事件、后续 accept 之间不会因为多线程竞争而打乱顺序。

channel.bind() 会进入 Pipeline 的出站路径。默认从 TailContext 开始向 HeadContext 传播,最终由 HeadContext 调用 unsafe.bind()。这意味着 bind 和 write 一样,都是 Channel 的出站操作,也可以被 ChannelOutboundHandler 拦截。

在 AbstractUnsafe.bind() 中,Netty记录 bind 前后的 Active 状态:

boolean wasActive = isActive();try {    doBind(localAddress);} catch (Throwable t) {    safeSetFailure(promise, t);    closeIfClosed();    return;}if (!wasActive && isActive()) {    invokeLater(() -> pipeline.fireChannelActive());}safeSetSuccess(promise);

真正的平台调用落在 NioServerSocketChannel.doBind(),最终调用 JDK ServerSocketChannel.bind(localAddress, backlog)。到这里,端口才真正处于监听状态。

8. ChannelFuture 为什么必须贯穿启动链

启动过程跨越用户线程与 Boss EventLoop,注册和绑定都可能异步完成,因此无法用普通返回值表达结果。ChannelFuture 让调用者既可以同步等待,也可以注册监听:

ChannelFuture bindFuture = bootstrap.bind(8080);bindFuture.addListener(f -> {    if (f.isSuccess()) {        log.info("server started: {}", f.channel().localAddress());    } else {        log.error("bind failed", f.cause());    }});

示例里的 .sync() 只是启动线程选择阻塞等待,并没有把 Netty 底层变成同步模型:

ChannelFuture bindFuture = bootstrap.bind(8080).sync();bindFuture.channel().closeFuture().sync();

第一处 sync 等待端口绑定成功;第二处 sync 等待服务端 Channel 被关闭。真正的 I/O 仍然由 EventLoop 异步处理。

需要避免在 EventLoop 中调用可能等待同一 EventLoop 的 sync()。Netty 的 Promise 实现会尽力检测死锁,但架构上更正确的方式是使用 Listener 串联异步步骤。

9. 启动失败应该按阶段定位

bind() 失败不能一概归因为“端口占用”。按调用链至少可以分成四类:

阶段
常见失败
排查方向
创建 Channel
反射构造失败、Native 库缺失
ChannelFactory、传输依赖、构造异常
初始化
Option 不支持、Handler 配置错误
init()
、自定义 ChannelInitializer
注册
EventLoop 已关闭、类型不兼容
Group 生命周期、NIO/Epoll 是否匹配
绑定
地址占用、权限不足、地址不可用
BindException
、端口和网卡配置

如果 Future 的 cause 是 RejectedExecutionException,问题往往不是端口,而是 EventLoopGroup 已经 shutdown,注册或 bind 任务无法提交。如果是 ChannelPipelineException,需要检查 Handler 是否可共享、初始化期间是否抛异常。如果是 Native Transport 初始化失败,则应确认分类器、操作系统和架构是否匹配。

还有一个容易忽略的问题:用户只调用 bind() 却不检查 Future。启动线程继续打印“启动成功”,实际端口并未监听。生产启动逻辑必须以 Future 成功为准,再对外报告 Ready。

10. 从 bind 主链反推配置规则

理解源码后,许多配置规则不需要死记。

为什么 option() 和 childOption() 分开?因为前者在 ServerBootstrap.init() 中应用到父 Channel,后者由 ServerBootstrapAcceptor 在每次 Accept 后应用到子 Channel。

为什么 childHandler() 必填?因为监听 Channel 只负责产生新连接,真正的业务 Pipeline 要在每条子 Channel 创建后初始化。

为什么通常创建两个 EventLoopGroup?因为父 Channel 的 Accept 与子 Channel 的读写职责不同。小型服务可以复用同一个 Group,但职责仍然存在,性能隔离也会减弱。

为什么 bind Future 成功后才能接流量?因为成功意味着 Channel 已创建、初始化、注册并完成底层地址绑定,Pipeline 生命周期也进入 Active 阶段。

为什么优雅停机要关闭 EventLoopGroup,而不仅是关闭 ServerChannel?因为 ServerChannel 只停止接收新连接,已注册的子 Channel、任务队列、定时任务和 EventLoop 线程仍可能存活。

11. 总结:注册先于绑定,是启动链的设计支点

ServerBootstrap.bind() 看似是入口,真正的设计支点却是 initAndRegister()。Netty 先创建 Channel 和 Pipeline,再为 Channel 确定 EventLoop,最后把 bind 投递到该 EventLoop。这个顺序建立了后续所有事件的线程边界。

可以把整个过程记成四句话:

  1. 1. ChannelFactory 创建运行时 Channel。
  2. 2. ServerBootstrap 把配置和 Acceptor 装进父 Pipeline。
  3. 3. EventLoopGroup 选择 EventLoop,并把 JDK Channel 注册到 Selector。
  4. 4. EventLoop 执行真正 bind,状态转为 Active,Future 完成。

下一篇将继续沿这条链向前:客户端完成 TCP 握手后,Boss EventLoop 如何把 OP_ACCEPT 转换成一个 NioSocketChannel,ServerBootstrapAcceptor 又如何把它交给 Worker EventLoop。那一步才是“监听服务端”变成“管理大量连接”的关键。

参考资料

  1. 1. AbstractBootstrap.java:https://github.com/netty/netty/blob/netty-4.1.135.Final/transport/src/main/java/io/netty/bootstrap/AbstractBootstrap.java
  2. 2. ServerBootstrap.java:https://github.com/netty/netty/blob/netty-4.1.135.Final/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java
  3. 3. AbstractChannel.java:https://github.com/netty/netty/blob/netty-4.1.135.Final/transport/src/main/java/io/netty/channel/AbstractChannel.java
  4. 4. NioServerSocketChannel.java:https://github.com/netty/netty/blob/netty-4.1.135.Final/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java
  5. 5. Netty 4.1 用户指南:https://netty.io/wiki/user-guide-for-4.x.html

相关学习资料