# Apache Thrift

**Apache Thrift** 是一个开源的跨语言远程过程调用（RPC）与序列化框架。开发者在一个语言中立的接口定义文件中定义数据类型和服务接口，再由代码生成器为每种目标语言生成客户端和服务端代码。它由 Facebook 开发，后由 Apache 软件基金会管理，将分层软件栈与代码生成引擎相结合，用于构建可跨 C++、Java、Python、PHP、Ruby、Erlang、Perl、Haskell、C#、Cocoa、JavaScript、Node.js、[Smalltalk](https://www.edgepedia.org/zh/smalltalk)、OCaml、Delphi 及其他语言工作的服务。<sup>[1](https://thrift.apache.org/index.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 高性能跨平台 RPC 与序列化解决方案，结合软件栈与代码生成引擎<sup>[2](https://apache.org/foundation/board/minutes/Thrift.html)</sup> |
| 起源 | 创建于 Facebook，2007 年 4 月开源，2008 年 5 月进入 Apache 孵化器，2010-10-20 成为 Apache 顶级项目<sup>[3](https://thrift.apache.org/about)</sup><sup> • </sup><sup>[4](https://incubator.apache.org/projects/thrift.html)</sup> |
| 最新版本 | 0.25.0，发布于 2026-09-30，以源码归档、Windows 编译器构建和 Maven 构件形式分发，采用《Apache 许可证》2.0 版<sup>[5](https://thrift.apache.org/download)</sup> |
| 语言支持 | 据项目仓库，支持 28 种编程语言<sup>[6](https://github.com/apache/thrift)</sup> |
| 核心构建块 | 由传输、协议、处理器和服务端抽象组成的分层栈<sup>[7](https://thrift.apache.org/docs/concepts)</sup> |
| 核心线格式 | TBinary 协议，除 JavaScript 外所有语言均支持；TSocket 是所有语言均支持的核心传输<sup>[8](https://thrift.apache.org/docs/Languages.html)</sup> |
| 社区规模 | 据 2025 年向 Apache 董事会报告，有 41 名提交者和 22 名 PMC 成员<sup>[2](https://apache.org/foundation/board/minutes/Thrift.html)</sup> |

## 历史

Thrift 创建于 Facebook。Mark Slee、Aditya Agarwal 和 Marc Kwiatkowski 撰写了原始设计论文《Thrift：可扩展的跨语言服务实现》（*Thrift: Scalable Cross-Language Services Implementation*），描述了一个库和一套代码生成工具，用于在该网站的发展超出 LAMP 框架之后，加速构建高效、可扩展的后端服务。<sup>[9](https://github.com/apache/thrift/blob/master/doc/specs/thrift.tex)</sup><sup> • </sup><sup>[3](https://thrift.apache.org/about)</sup> Facebook 于 2007 年 4 月将 Thrift 开源，项目于 2008 年 5 月进入 Apache 孵化器。2010-10-20，它毕业成为 Apache 顶级项目。<sup>[4](https://incubator.apache.org/projects/thrift.html)</sup><sup> • </sup><sup>[3](https://thrift.apache.org/about)</sup>

Facebook 在 Apache 之外另行推进其内部演进。2014 年 2 月，它将其演进后的内部分支重新发布为 **fbthrift**，该公司称该项目并非 Apache Thrift 的一个发行版；其中的编译器从头重写，实现中增加了完全异步的服务端。<sup>[10](https://github.com/facebook/fbthrift)</sup><sup> • </sup><sup>[11](https://engineering.fb.com/2014/02/20/open-source/under-the-hood-building-and-open-sourcing-fbthrift/)</sup> 在宣布此事时，Facebook 报告称 Thrift 支撑着其 100 多个生产服务，大多用 C++、Java、PHP 或 Python 编写。<sup>[11](https://engineering.fb.com/2014/02/20/open-source/under-the-hood-building-and-open-sourcing-fbthrift/)</sup>

## 工作原理

开发者编写一个 `.thrift` 文件，即由 Thrift 类型和服务声明组成的接口定义，编译器则为客户端库和服务端生成源代码。命令 `thrift --gen <language> <Thrift filename>` 生成各语言代码。<sup>[1](https://thrift.apache.org/index.html)</sup> 接口定义语言支持常量、类型定义、枚举、结构体、联合体、异常和服务；服务是一组函数，并且可以扩展另一个服务，从而将所扩展服务的函数加入自身。<sup>[12](https://thrift.apache.org/docs/idl)</sup> 服务在语义上等同于定义一个接口或纯虚抽象类，编译器会生成实现该接口的、功能完整的客户端和服务端桩代码。<sup>[13](https://thrift.apache.org/docs/types.html)</sup>

**分层栈。** 生成的代码建立在四个可独立混用的抽象之上：<sup>[7](https://thrift.apache.org/docs/concepts)</sup>

1. **传输（Transport）**，即双向原始字节接口，具有 open、close、read、write 和 flush 方法，另有 ServerTransport 用于在服务端接受传入连接。大多数语言可用的传输包括基于文件的 I/O 和 HTTP；所有语言均支持的核心传输是 TSocket。<sup>[7](https://thrift.apache.org/docs/concepts)</sup><sup> • </sup><sup>[8](https://thrift.apache.org/docs/Languages.html)</sup>
2. **协议（Protocol）**，将内存中的数据结构映射为线格式，并负责编码和解码。示例包括相当简单的二进制编码、紧凑二进制编码和 JSON。<sup>[7](https://thrift.apache.org/docs/concepts)</sup>
3. **处理器（Processor）**，按服务生成，从输入协议读取数据，委托给用户实现的处理程序，并通过输出协议写出响应。<sup>[7](https://thrift.apache.org/docs/concepts)</sup>
4. **服务端（Server）**，将各部分整合起来：它创建传输，据此构建输入/输出协议和处理器，并为传入连接提供服务。<sup>[7](https://thrift.apache.org/docs/concepts)</sup>

这种分层是原始论文中有意做出的设计选择：传输与代码生成解耦，因此同一应用代码可以运行在 TCP 套接字、内存缓冲区或磁盘文件之上，Thrift 强制采用一种消息结构，但不依赖于所使用的协议编码。<sup>[9](https://github.com/apache/thrift/blob/master/doc/specs/thrift.tex)</sup>

**类型与版本管理。** 类型系统提供基础类型、容器（如 `list` 和 `map<type1,type2>`，视情况转换为 STL vector、Java ArrayList 或原生数组和字典）、结构体、继承各语言原生异常基类的异常，以及服务。<sup>[13](https://thrift.apache.org/docs/types.html)</sup> 字段可声明为可选，从而在未设置时排除在序列化输出之外；void 函数可接受 `oneway` 修饰符，生成的代码不等待响应；对于 oneway 调用，客户端只能得到请求在传输层成功的保证，此类调用在服务端可能乱序执行。<sup>[1](https://thrift.apache.org/index.html)</sup><sup> • </sup><sup>[13](https://thrift.apache.org/docs/types.html)</sup>

## 语言支持与测试

项目仓库列出支持 28 种编程语言。<sup>[6](https://github.com/apache/thrift)</sup> 每种语言绑定都有最低所需版本和可选支持库，记录在各语言的 README 以及语言与特性矩阵中。决定性的集成测试是“cross”测试，它执行一个由数千种可能组合构成的矩阵，让客户端和服务端跨不同语言通信。<sup>[8](https://thrift.apache.org/docs/Languages.html)</sup> Kotlin 支持通过 THRIFT-5548 在 0.17.0 版本中加入。<sup>[14](https://www.mail-archive.com/notifications%40thrift.apache.org/msg05923.html)</sup>

项目使用两套构建系统：autoconf，是更完整的构建，覆盖所有受支持语言，但不支持 Windows；以及 cmake，可在 Linux 和 Windows 上工作，但尚不支持所有语言。<sup>[8](https://thrift.apache.org/docs/Languages.html)</sup> 绑定覆盖范围随时间变化：近期版本的变更日志记录了放弃 Swift 支持（THRIFT-5864），同时还有加固工作，例如限制 Rust 和 Smalltalk 库中的结构体递归深度，以及为协议读取器增加字节数限制。<sup>[15](https://thrift.apache.org/changelog)</sup> 项目的威胁模型记录道，通过网络到达的字节，或从保存持久化 Thrift 记录的文件和队列中读取的字节，都是不可信输入；未配置 `maxMessageSize` 的无帧传输可能导致服务端一直读取直到分配失败。<sup>[16](https://github.com/apache/thrift/blob/master/doc/thrift-threat-model.md)</sup>

## 发布与项目状态

Apache 董事会报告将 Thrift 描述为一个持续进行、活跃度中等的项目。0.21.0 版本于 2024-09-21 发布，0.22.0 于 2025-05-23 发布，据描述主要是维护版本。<sup>[2](https://apache.org/foundation/board/minutes/Thrift.html)</sup> 项目不维护固定的发布日历，但力求每年发布两次。<sup>[6](https://github.com/apache/thrift)</sup> 0.6.0 及以后的版本保存在 Apache Thrift Archive 中，更早的孵化器版本另行归档。<sup>[5](https://thrift.apache.org/download)</sup> 所有代码均按《Apache 许可证》2.0 版许可。<sup>[6](https://github.com/apache/thrift)</sup> GitHub 仓库显示约 11,000 个星标和 4,100 个复刻。<sup>[6](https://github.com/apache/thrift)</sup>

对某些绑定而言，维护能力薄弱。2025 年董事会报告指出，尤其是 Swift，似乎已没有活跃的维护者，而在 Swift 论坛上征集维护者的呼吁收效甚微。<sup>[2](https://apache.org/foundation/board/minutes/Thrift.html)</sup>

## 性能及与 gRPC 的比较

原始论文指出项目的优先级是性能第一、优雅第二，其设计使生成的代码不含不必要的依赖。<sup>[3](https://thrift.apache.org/about)</sup><sup> • </sup><sup>[9](https://github.com/apache/thrift/blob/master/doc/specs/thrift.tex)</sup> 实测结果支持这一取向：特文特大学（University of Twente）一项关于计算卸载场景中多语言工具的比较研究发现，使用 [Protocol Buffers](https://www.edgepedia.org/zh/protocol-buffers) 的 gRPC 在 81% 的情况下是最慢的框架，在 78% 的情况下是最耗能的；而 Apache Thrift 在 83% 的场景中是最快的框架，在 66% 的场景中是最节能的。<sup>[17](https://research.utwente.nl/en/publications/a-comparative-study-about-the-performance-of-multi-language-tools/)</sup>

Thrift 与 gRPC 共享若干特性：两者都提供多语言支持、代码生成和面向性能的二进制序列化。<sup>[18](https://recipp.ipp.pt/bitstreams/6694c6c0-47d8-47c5-a634-879c0bc83191/download)</sup> 定性比较往往在生态和工具方面偏向 gRPC，并指出 Thrift 的实际差距，例如异步通信无法使用 TLS。<sup>[19](https://odr.chalmers.se/bitstreams/b7d99f83-f626-4b3d-84fa-cfb5c64e0b4e/download)</sup> 历史上的特性比较还指出了数据模型上的差异：Thrift 在其 IDL 中支持异常定义，而 Protocol Buffers 不支持；Protocol Buffers 对扩展类型处理得更好，文档也更为详尽。<sup>[20](https://repositum.tuwien.at/bitstream/20.500.12708/5713/2/Schoba%20Joerg%20-%202017%20-%20Mobile%20peer%20model%20a%20mobile%20peer-to-peer%20communication%20and...pdf)</sup> 与围绕单一格式构建的系统相比，Thrift 还允许多种序列化格式。<sup>[21](https://repositum.tuwien.at/bitstream/20.500.12708/2049/2/Cejka%20Stephan%20-%202019%20-%20Enabling%20scalable%20collaboration%20by%20introducing...pdf)</sup>

Thrift 仍在大规模运行的后端和嵌入式系统中投入生产使用，Facebook 的工程博客在 2014 年描述了这一状况。<sup>[11](https://engineering.fb.com/2014/02/20/open-source/under-the-hood-building-and-open-sourcing-fbthrift/)</sup>

## References

1. [Apache Thrift - Home](https://thrift.apache.org/index.html)
2. [Board history: Thrift, Apache Software Foundation](https://apache.org/foundation/board/minutes/Thrift.html)
3. [Apache Thrift - About](https://thrift.apache.org/about)
4. [Apache Thrift Incubation Status, Apache Incubator](https://incubator.apache.org/projects/thrift.html)
5. [Apache Thrift - Download](https://thrift.apache.org/download)
6. [GitHub - apache/thrift](https://github.com/apache/thrift)
7. [Apache Thrift - Concepts](https://thrift.apache.org/docs/concepts)
8. [Apache Thrift - Language and Feature Matrix](https://thrift.apache.org/docs/Languages.html)
9. [Mark Slee, Aditya Agarwal, Marc Kwiatkowski. Thrift: Scalable Cross-Language Services Implementation](https://github.com/apache/thrift/blob/master/doc/specs/thrift.tex)
10. [GitHub - facebook/fbthrift](https://github.com/facebook/fbthrift)
11. [Under the Hood: Building and open-sourcing fbthrift, Engineering at Meta (2014)](https://engineering.fb.com/2014/02/20/open-source/under-the-hood-building-and-open-sourcing-fbthrift/)
12. [Apache Thrift - Interface Description Language (IDL)](https://thrift.apache.org/docs/idl)
13. [Apache Thrift - Thrift Type system](https://thrift.apache.org/docs/types.html)
14. [PR #3820: Update LANGUAGES.md with current versions and supported features](https://www.mail-archive.com/notifications%40thrift.apache.org/msg05923.html)
15. [Apache Thrift - Changelog](https://thrift.apache.org/changelog)
16. [Apache Thrift threat model documentation](https://github.com/apache/thrift/blob/master/doc/thrift-threat-model.md)
17. [A comparative study about the performance of multi-language tools in computation offloading scenarios, University of Twente](https://research.utwente.nl/en/publications/a-comparative-study-about-the-performance-of-multi-language-tools/)
18. [Comparing REST and gRPC, thesis repository, Porto](https://recipp.ipp.pt/bitstreams/6694c6c0-47d8-47c5-a634-879c0bc83191/download)
19. [Qualitative comparison of RPC frameworks, Chalmers thesis](https://odr.chalmers.se/bitstreams/b7d99f83-f626-4b3d-84fa-cfb5c64e0b4e/download)
20. [Mobile peer model thesis, TU Wien](https://repositum.tuwien.at/bitstream/20.500.12708/5713/2/Schoba%20Joerg%20-%202017%20-%20Mobile%20peer%20model%20a%20mobile%20peer-to-peer%20communication%20and...pdf)
21. [Platform-independent communication for the peer model, TU Wien](https://repositum.tuwien.at/bitstream/20.500.12708/2049/2/Cejka%20Stephan%20-%202019%20-%20Enabling%20scalable%20collaboration%20by%20introducing...pdf)

---
*Initially written Oct 26, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/apache-thrift

English original: https://www.edgepedia.org/apache-thrift
