Python-RPC: Thrift 的简介和安装

it2022-05-05  232

简介

Thrift是由Facebook为“大规模跨语言服务开发”而开发的,现在是Apache软件基金会的开源项目。

Thrift实现了一种接口描述语言和二进制通讯协议,用来定义和创建跨语言的服务。它被当作一个RPC框架来使用。

ubuntu 安装

安装依赖工具和库

sudo apt-get install automake bison flex g++ git libboost-all-dev libevent-dev \ libssl-dev libtool make pkg-config

安装Thrift编译器和程序库

可以从https://thrift.apache.org/download下载Thrift源文件:

thrift-0.11.0.tar.gz 是可以在Linux或Mac安装的源文件Thrift compiler for Windows (thrift-0.11.0.exe) 是Windows的安装文件

1)解压缩源文件

tar -zxvf thrift-0.11.0.tar.gz

2) 配置安装过程

cd thrift-0.11.0 ./configure --prefix=/usr/local/ --without-php --without-java --without-perl --without-nodejs –prefix表示安装到的路径–without-PACKAGE表示不安装PACKAGE语言的库,如–without-php表示不安装php的Thrift基础程序库其他configure选项参数可以通过 ./configure --help进行查看

3)解析来执行

sudo make

4) 安装

sudo make install

5)验证

执行如下命令:

thrift -version

如果出现了版本信息,则表示安装成功。

注意事项

安装成功后对于选择安装的语言,调用Thrift的程序库实际上也安装完成。但是对于Python语言,Thrift会附带安装适用于Python 2的程序库(包),缺少了Python 3的程序库;同时,对于Ubuntu系统(或Debian系统),默认python的搜索包路径在dist-packages子目录下,而Thrift安装的Python程序包路径在site-packages子目录下,python程序不能直接导入thrift包。所以,对于Python语言,我们可以使用下面的方法自己安装thrift包。

安装 Thrift 的 Python 包

pip install thrift

MacOS 的安装

以下安装过程不建议参考,造成不可逆损失请自己负责 ?

# 安装 bison brew install bison # 在没有 wget 的情况下先安装 wget wget http://mirrors.tuna.tsinghua.edu.cn/apache/thrift/0.12.0/thrift-0.12.0.tar.gz # 解压缩源文件 tar -zxvf thrift-0.12.0.tar.gz # 配置安装 cd thrift-0.12.0 ./configure --prefix=/usr/local/ --without-php --without-java --without-perl --without-nodejs #### 安装参数说明 * --prefix表示安装到的路径 * --without-PACKAGE表示不安装PACKAGE语言的库,如--without-php表示不安装php的Thrift基础程序库 * 其他configure选项参数可以通过 ./configure --help进行查看 # 报错: Bison version 2.5 or higher must be installed on the system! # 查看一下自己的 Bison 版本 Bison --version bison (GNU Bison) 2.3 # 尝试升级本机的 Bison 版本 brew upgrade bison # 显示已经安装了最新版本的 Bison Error: bison 3.4.1 already installed # 查看本机安装的全部版本,可以看到我确实已经安装了最新版本的 3.41 brew list bison /usr/local/Cellar/bison/3.4.1/bin/bison /usr/local/Cellar/bison/3.4.1/bin/yacc /usr/local/Cellar/bison/3.4.1/lib/liby.a /usr/local/Cellar/bison/3.4.1/share/aclocal/bison-i18n.m4 /usr/local/Cellar/bison/3.4.1/share/bison/ (28 files) /usr/local/Cellar/bison/3.4.1/share/doc/ (42 files) /usr/local/Cellar/bison/3.4.1/share/info/bison.info /usr/local/Cellar/bison/3.4.1/share/man/ (2 files) # 查看一下 bison 的位置 which bison /usr/bin/bison # 进入查看此处的版本是 2.3 bison --version bison (GNU Bison) 2.3 # 在zsh中添加配置路径或者 .bash_profile vim ~/.zshrc # 文件中添加 export PATH="/usr/local/opt/bison/bin:$PATH" source ~/.zshrc # 再次查看版本 bison -V bison (GNU Bison) 3.4.1 # 解决版本报错之后再次配置安装 ./configure --prefix=/usr/local/ --without-php --without-java --without-perl --without-nodejs # 解析执行 sudo make # 解析报错 fatal error: 'boost/tokenizer.hpp' file not found # 尝试安装一些依赖库解决报错 brew install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config # 然后报了更多的错误, hhhh ... # 报错的是 boost 文件 尝试安装 boost brew install boost # 尝试配置安装和解析执行 这一步比较漫长 ... ./configure --prefix=/usr/local/ --without-php --without-java --without-perl --without-nodejs sudo make # 然后报了另外一个错误 src/thrift/transport/TSSLSocket.cpp:43:10: fatal error: 'openssl/opensslv.h' file not found # 尝试安装 openssl 提示我已经安装过了 brew install openssl Updating Homebrew... Warning: openssl 1.0.2s is already installed and up-to-date To reinstall 1.0.2s, run `brew reinstall openssl` # 执行 brew link openssl --force # 安装提示更新 .zshrc 文件 echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc source ~/.zshrc # 检查一下 tail ~/.zshrc # 尝试配置安装和解析执行 ./configure --prefix=/usr/local/ --without-php --without-java --without-perl --without-nodejs sudo make # 同样的问题 # 尝试加入这几句 export LDFLAGS="-L/usr/local/opt/openssl/lib" export CPPFLAGS="-I/usr/local/opt/openssl/include" export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig" # 尝试执行 ./configure LDFLAGS='-L/usr/local/opt/openssl/lib' CPPFLAGS='-I/usr/local/opt/openssl/include' --prefix=/usr/local/ --without-php --without-java --without-perl --without-nodejs sudo make # 安装 sudo make install # 报错: error: could not create '/usr/lib/python2.7/site-packages': Operation not permitted # 看一下Python2.7 的位置 which python2.7 /usr/local/bin/python2.7 # 将这个路径导入(可选) export PYTHONPATH="/usr/local/bin/python2.7" # 再次编译 ./configure PYTHONPATH="/usr/local/bin/python2.7" LDFLAGS='-L/usr/local/opt/openssl/lib' CPPFLAGS='-I/usr/local/opt/openssl/include' --prefix=/usr/local/ --without-php --without-java --without-perl --without-nodejs sudo make sudo make intsall # 还是上一个错误 # 再次编译尝试 ./configure LDFLAGS='-L/usr/local/opt/openssl/lib' CPPFLAGS='-I/usr/local/opt/openssl/include' --prefix=/usr/local/bin/ --without-php --without-java --without-perl --without-nodejs # 还是上一个错误 export PYTHONPATH=/usr/local/lib/python2.7/site-packages ./configure LDFLAGS='-L/usr/local/opt/openssl/lib' CPPFLAGS='-I/usr/local/opt/openssl/include' --prefix=/usr/local/ --without-php --without-java --without-perl --without-nodejs 一直没成功 但是查看版本 显示出来了 thrift -version Thrift version 0.12.0

最新回复(0)