惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 三驾马车

Claude Code 官宣:可以在 IDEA 用了! idea gitee 更新已取消 解决方案 ByteBuffer和ByteBuf区别 Marshalling.getProvidedMarshallerFactory("serial") 参数有那些 ProtobufVarint32FrameDecoder和ProtobufDecoder区别 protobuf 的 Varint 编码规范 netty initChannel ch.pipeline().addLast 先后顺序很重要 ChannelInboundHandlerAdapter 的channelRead和channelReadComplete的区别 Unpooled.buffer()和Unpooled.copiedBuffer区别 childhandler 和 handler 区别 ChannelInitializer<SocketChannel> 的作用详解 ChannelHandlerAdapter 和 ChannelInboundHandlerAdapter 的区别 SimpleChannelInboundHandler 中的 messageReceived 和 channelRead0 ChannelHandlerAdapter 与 ChannelInboundHandler 的区别 Application run failed .ParserException: while parsing a block mapping in 'reader' openssl genrsa 自签名ssl证书 上传本地项目到新建git项目 save download pdf
ServerBootstrap 和Bootstrap 区别
三驾马车 · 2025-05-14 · via 博客园 - 三驾马车

Netty 中的 ServerBootstrap 和 Bootstrap 是网络应用程序的启动类,它们的主要区别在于用途和功能:

1. 基本用途

用途对应网络角色
Bootstrap 用于启动客户端应用程序 客户端(Client)
ServerBootstrap 用于启动服务端应用程序 服务器(Server)

2. 核心区别

线程模型差异

Bootstrap (客户端):

  • 通常只需要一个 EventLoopGroup

  • 处理单个到服务器的连接

EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group) 

ServerBootstrap (服务端):

  • 需要两个 EventLoopGroup:

    • bossGroup: 接受传入连接

    • workerGroup: 处理已接受的连接

  • 处理多个客户端连接

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup) 

Channel 类型差异

典型 Channel 类型
Bootstrap NioSocketChannel
ServerBootstrap NioServerSocketChannel

Handler 配置差异

Bootstrap:

  • 只有 handler() 方法

  • 配置客户端自身的处理器

ServerBootstrap:

  • handler(): 配置服务端自身的处理器(如监听端口状态)

  • childHandler(): 配置每个新接受的客户端连接的处理器

3. 生命周期对比

Bootstrap:

  • 创建单个连接

  • 连接断开后通常需要重新创建Bootstrap实例

ServerBootstrap:

  • 绑定到端口后持续监听

  • 可以接受多个客户端连接

  • 生命周期通常与应用程序相同

4. 典型使用场景

Bootstrap 用例:

ServerBootstrap 用例:

5. 总结对比表

特性Bootstrap (客户端)ServerBootstrap (服务端)
线程组数量 1个 2个(boss+worker)
主要Channel类型 NioSocketChannel NioServerSocketChannel
Handler配置 handler() handler()+childHandler()
连接数量 单个连接 多个客户端连接
典型用途 发起连接 接受连接

正确理解和使用这两个类是开发Netty网络应用程序的基础,它们的设计反映了客户端和服务端在网络编程中的不同角色和需求。