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

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 三驾马车

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网络应用程序的基础,它们的设计反映了客户端和服务端在网络编程中的不同角色和需求。