Apache Thrift 在 CentOS 上的源码编译安装完整指南
【免费下载链接】thriftApache Thrift项目地址: https://gitcode.com/gh_mirrors/thrift2/thrift
导读
本文基于仓库中的官方安装文档 doc/install/centos.md,系统梳理在 CentOS 6.5 最小化安装环境下从源码构建 Apache Thrift 的完整流程:从系统更新、平台工具链准备、autoconf/automake/bison 升级,到 C++ 库依赖(Boost/libevent/OpenSSL/zlib)安装,再到 IDL 编译器与各语言库的编译、安装与验证。读完本文,你将掌握在 RHEL/CentOS 系发行版上独立完成 Thrift 全量构建、仅构建编译器(--enable-libs=no)以及运行make check测试的能力,并能结合仓库源码理解每一步背后的构建系统原理。
一、为什么在 CentOS 上从源码构建 Apache Thrift
Apache Thrift 的构建系统以 GNU autotools(autoconf + automake + libtool)为基础,对工具链版本有明确要求。以 CentOS 6.5 为代表的旧发行版自带的 autoconf/automake/bison 版本往往偏低,无法满足仓库构建脚本的最低版本检查,因此官方安装指南建议先升级这些工具,再进入 Thrift 本身的构建流程。
从仓库源码可以印证这些版本约束:
- configure.ac 声明
AC_PREREQ(2.65)(要求 autoconf ≥ 2.65),并使用AM_INIT_AUTOMAKE([1.13 ...])(要求 automake ≥ 1.13); - 顶层 bootstrap.sh 在执行
aclocal/autoconf/automake之前会先检查 automake 版本:automake version ... is too old (need 1.13 or later); - configure.ac 调用
AC_PROG_BISON(2.5),要求 bison ≥ 2.5 来生成语法解析器; - 编译器的词法/语法分析器由 flex 与 bison 从 compiler/cpp/src/thrift/thriftl.ll 和 compiler/cpp/src/thrift/thrifty.yy 生成(见 compiler/cpp/CMakeLists.txt),因此 lex/yacc(flex/bison)是构建编译器的硬性前置条件。
这也解释了为什么本文档(以及 doc/install/README.md 中的 "Requirements for building from source" 一节)会把 autoconf 2.65、automake 1.13、libtool、flex/bison 列为构建前提。
适用范围说明:文档针对 CentOS 6.5 最小化安装编写,同时说明这些步骤同样适用于 Apache Thrift 0.9.2 及之后的发布版本;仓库当前版本为 0.21.0(见 configure.ac 的
AC_INIT([thrift], [0.21.0]))。CentOS 6 系列已于 2020 年停止维护,如需在更新的 CentOS/RHEL 版本上安装,可使用yum install直接安装新版工具链,其余流程相同。
二、更新系统与安装平台开发工具
2.1 更新系统
以 root 或具有 sudo 权限的用户执行,先同步系统软件包索引并升级:
sudo yum -y update2.2 安装开发工具组
CentOS 通过groupinstall安装整套编译工具链(gcc、g++、make、libtool 等):
sudo yum -y groupinstall "Development Tools"该软件组提供 GNU 编译器套件与 make,是后续源码编译的基础。若你的发行版使用 dnf(CentOS 8+),对应命令为sudo dnf groupinstall "Development Tools"。
三、升级 autoconf / automake / bison
CentOS 6.5 自带的 autoconf(2.63)、automake(1.11)与 bison 版本均低于 Thrift 构建要求,需要从源码安装新版。仓库构建脚本的最低版本要求可参考前文:
| 工具 | Thrift 要求 | 本文档安装版本 | 检查位置 |
|---|---|---|---|
| autoconf | ≥ 2.65 | 2.69 | configure.ac |
| automake | ≥ 1.13 | 1.14 | bootstrap.sh |
| bison | ≥ 2.5 | 2.5.1 | configure.ac |
3.1 安装 wget
后续下载源码包需要 wget:
sudo yum install -y wget3.2 升级 autoconf
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz tar xvf autoconf-2.69.tar.gz cd autoconf-2.69 ./configure --prefix=/usr make sudo make install cd ..--prefix=/usr将新版 autoconf 安装到系统路径,覆盖旧版本,避免 PATH 冲突。
3.3 升级 automake
wget http://ftp.gnu.org/gnu/automake/automake-1.14.tar.gz tar xvf automake-1.14.tar.gz cd automake-1.14 ./configure --prefix=/usr make sudo make install cd ..automake 1.14 满足 bootstrap.sh 中 "we require automake 1.13 or later" 的硬性检查。若安装后automake --version仍显示旧版本,请确认PATH中/usr/bin优先于旧安装目录。
3.4 升级 bison
wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz tar xvf bison-2.5.1.tar.gz cd bison-2.5.1 ./configure --prefix=/usr make sudo make install cd ..bison 负责从 compiler/cpp/src/thrift/thrifty.yy 生成 Thrift IDL 语法解析器,版本不足将直接导致configure失败(AC_PROG_BISON(2.5)会拒绝旧版本)。
3.5 验证工具链
autoconf --version | head -1 automake --version | head -1 bison --version | head -1确认三个版本号分别不低于 2.65、1.13、2.5 后,再继续后续步骤。
四、安装 C++ 语言库依赖(可选)
所有语言代码生成都依赖 Apache Thrift IDL 编译器,而前三节已把编译编译器所需的全部工具装齐。如果你只需要编译器,可以跳过本节,直接进入第五章的构建步骤。
但若要在 C++ 中开发 Thrift 客户端/服务器(构建libthrift共享库),还需要以下依赖:
4.1 安装 libevent / zlib / openssl 开发包
sudo yum -y install libevent-devel zlib-devel openssl-devel- libevent-devel:构建非阻塞服务器(TNonblockingServer)所需,见 doc/install/README.md 中 C++ 一节 "libevent (optional, to build the nonblocking server)";
- zlib-devel:启用压缩传输/协议支持;
- openssl-devel:启用 TLS/SSL 安全传输支持。
这些库在 configure.ac 对应的 CMake 构建中同样被检测(WITH_LIBEVENT AND WITH_ZLIB AND WITH_OPENSSL),缺失时对应特性会被禁用。
4.2 升级 Boost ≥ 1.56
CentOS 6.5 自带 Boost 版本过旧,无法满足 C++ 库与测试的编译要求(doc/install/README.md 明确要求 Boost 1.56.0),需从源码安装:
wget http://sourceforge.net/projects/boost/files/boost/1.56.0/boost_1_56_0.tar.gz tar xvf boost_1_56_0.tar.gz cd boost_1_56_0 ./bootstrap.sh sudo ./b2 install./b2 install默认将 Boost 头文件安装到/usr/local/include、库文件安装到/usr/local/lib。若后续./configure无法找到 Boost,可通过CPPFLAGS/LDFLAGS或--with-boost指定路径;仓库 aclocal/ax_boost_base.m4 会依次在/usr、/usr/local、/opt、/opt/local下探测 Boost 安装位置。
五、构建并安装 Apache Thrift IDL 编译器
5.1 获取源码并引导构建系统
git clone https://github.com/apache/thrift.git cd thrift ./bootstrap.sh- 也可以直接使用本仓库(当前版本 0.21.0);
./bootstrap.sh是 Thrift 特有的"引导脚本",它依次执行autoscan、libtoolize、aclocal -I ./aclocal、autoheader、autoconf、automake --copy --add-missing等步骤(见 bootstrap.sh),把仓库中的configure.ac、Makefile.am等元数据展开成可执行的configure脚本与 Makefile;- 运行前请确认
libtoolize/glibtoolize可用,脚本在找不到时会直接报错退出(bootstrap.sh)。
5.2 配置构建
./configure --with-lua=no--with-lua=no用于显式禁用 Lua 语言库的构建。仓库 configure.ac 显示 Lua 库默认启用(AX_THRIFT_LIB(lua, [Lua], yes)),但要求 Lua ≥ 5.2(AX_PROG_LUA(5.2,...))并需要头文件与链接库;在 CentOS 6.5 上 Lua 版本通常不满足要求,因此文档统一使用--with-lua=no关闭,避免configure探测失败或构建中断。
这一步还会完成所有语言库的可用性探测(C++、Java、Python、Ruby、Go、Node.js、Rust、Erlang、PHP、Perl、D、Haxe、Delphi、NetStd、Swift 等),configure结束时输出的摘要(如 "Building Lua Library ......... : no")会逐项列出各语言库是否将被构建。
5.3 编译与安装
make sudo make install- 顶层 Makefile.am 定义
SUBDIRS = compiler/cpp lib,即一次构建同时产出编译器与各语言库; - 编译完成后,编译器可执行文件为
compiler/cpp/thrift(位于compiler/cpp/目录下,即 compiler/cpp/CMakeLists.txt 中add_executable生成的thrift); sudo make install会将编译器安装到系统路径:/usr/local/bin/thrift;- 安装后可用如下命令验证版本:
thrift --version六、常用构建变体与测试
6.1 仅构建 IDL 编译器
如果只需要thrift编译器(用于生成各语言代码),不需要任何语言运行时库,可跳过第四节依赖并改用:
./configure --enable-libs=no make sudo make install--enable-libs=no关闭所有语言库的构建。仓库 configure.ac 显示该开关默认值为yes([default=yes]),一旦置为no,C++、C_glib、Java、Python、Ruby、PHP、Go、Node.js、Dart、Erlang、Lua、Rust、NetStd、Kotlin、Swift 等库的构建条件会被统一置为no,make只产出编译器本身,显著缩短构建时间。
6.2 运行测试
make checkmake check会执行仓库内置的测试套件:顶层 Makefile.am 在启用测试(WITH_TESTS)时会把test目录纳入构建,测试资源位于 test 目录(含ThriftTest.thrift、DebugProtoTest.thrift等 IDL 定义及各语言客户端/服务器测试)。注意make check需要各语言运行环境与库已正确构建,若使用--enable-libs=no或缺少某语言运行时,对应语言的测试会被跳过或失败。
七、安装后的验证与常见问题
7.1 验证安装
which thrift # 应输出 /usr/local/bin/thrift thrift --version # 应输出 Thrift version x.y.z7.2 常见问题排查
| 现象 | 原因与对策 |
|---|---|
bootstrap.sh报 "automake ... is too old" | automake < 1.13,按第三节重新安装 automake 1.14 |
configure报 bison 版本错误 | bison < 2.5,按 3.4 升级 |
configure找不到 Boost | Boost 未安装或不在默认路径,安装 1.56+ 或用CPPFLAGS/LDFLAGS指定路径 |
| Lua 相关探测失败导致 configure 报错 | 使用--with-lua=no或安装 Lua 5.2+ 开发包 |
make报缺少 libevent/zlib/openssl 头文件 | 执行sudo yum -y install libevent-devel zlib-devel openssl-devel |
7.3 下一步
安装完成后,可以:
- 用
thrift --gen <语言> <xxx.thrift>为任意支持的语言生成代码(各语言生成器源码位于 compiler/cpp/src/thrift/generate); - 参考 tutorial 目录下的各语言客户端/服务器示例(如 tutorial/cpp/CppClient.cpp、tutorial/py/PythonServer.py)编写第一个 Thrift 服务;
- 阅读 doc/install/debian.md 了解 Debian/Ubuntu 系的对应安装方式,以及各语言库的可选依赖包清单(Java 需 Gradle 8.4、Rust 需 rustc/cargo、Lua 需 lua5.3 等)。
【免费下载链接】thriftApache Thrift项目地址: https://gitcode.com/gh_mirrors/thrift2/thrift
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考