分享
Centos7.2 thrift-0.9.3 安装使用(cpp服务端,go客户端)
xingxingliangqilai · · 4678 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
- thrift是什么
- 依赖条件
- thrift安装
- thrift使用
- 总结
thrift是什么
Thrift是一种接口描述语言和二进制通讯协议,[1]它被用来定义和创建跨语言的服务。[2]它被当作一个远程过程调用(RPC)框架来使用,是由Facebook为"大规模跨语言服务开发"而开发的。它通过一个代码生成引擎联合了一个软件栈,来创建不同程度的、无缝的跨平台高效服务,可以使用C#、C++(基于POSIX兼容系统)、Cappuccino、Cocoa、Delphi、Erlang、Go、Haskell、Java、Node.js、OCaml、Perl、PHP、Python、Ruby和Smalltalk。[wiki链接](https://zh.wikipedia.org/wiki/Thrift)
依赖条件
thrift核心代码使用C++写的,用了boost库。
执行以下命令,安装相关依赖:
yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel
thrift安装
1.到http://thrift.apache.org/download 这里下载的是 thrift-0.9.3.tar.gz
2. 在执行下列操作, 可根据需要选择相应的语言支持
#tar -zxvf thrift-0.9.3.tar.gz
#cd thrift-0.9.3
#./configure --with-boost=/usr/local --without-java --without-php
#make; make install
- 在终端输入:thrift –version 查看是否安装成功。
thrift使用
- 定义一个thrift文件, phone.thrift, 内容如下:
enum PhoneType {
HOME,
WORK,
MOBILE,
OTHER
}
struct Phone {
1: i32 id,
2: string number,
3: PhoneType type
}
- 使用thrift命令行,生成java, golang 代码
thrift -r --gen cpp phone.thrift
thrift -r --gen go phone.thrift
- 在gen-cpp目录执行下列操作
cp PhoneService_server.skeleton.cpp PhoneService_server.cpp
- 修改 PhoneService_server.cpp 中的 get 函数
void get(Phone& _return, const std::string& uid) {
// Your implementation goes here
_return.id = 1024;
_return.number = "13424562789";
_return.type = PhoneType::MOBILE;
printf("get\n");
}
- 编写makefile文件
BOOST_PATH = /usr/include/boost/
THRIFT_PATH = /usr/local/include/thrift
LIB_PATH = /usr/local/lib
SRC = phone_types.cpp phone_constants.cpp PhoneService.cpp PhoneService_server.cpp
server: ${SRC}
g++ -g -o PhoneServer ${SRC} -I${THRIFT_PATH} -I{BOOST_PATH} -lthrift
运行make, 生成 PhoneServer 的可执行文件, 运行PhoneServer
./PhoneServer
- 准备golang的客户端
#cd gen-go
#mkdir src
#mv phone src/phone
#mv src/phone/phone_service-remote src/phone_service-remote
修改 /etc/profile文件, 增加golang项目路径
export GOPATH=/usr/local/gopath:/home/denny/thrift-test/gen-go
使环境变量生效
source /etc/profile
编译 phone_service-remote
go build phone_service-remote
运行 phone_service-remote
./phone_service-remote
输出:
Phone({ID:1024 Number:13424562789 Type:MOBILE}) <nil>
至此thrift的简单应用就完结了
总结
对thrift研究也不是很多, 现在感觉来说thrift适合多语言混合编程,且系统内或系统间的交互依赖于RPC这时候的用处应该是挺大的。
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信4678 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
- thrift是什么
- 依赖条件
- thrift安装
- thrift使用
- 总结
thrift是什么
Thrift是一种接口描述语言和二进制通讯协议,[1]它被用来定义和创建跨语言的服务。[2]它被当作一个远程过程调用(RPC)框架来使用,是由Facebook为"大规模跨语言服务开发"而开发的。它通过一个代码生成引擎联合了一个软件栈,来创建不同程度的、无缝的跨平台高效服务,可以使用C#、C++(基于POSIX兼容系统)、Cappuccino、Cocoa、Delphi、Erlang、Go、Haskell、Java、Node.js、OCaml、Perl、PHP、Python、Ruby和Smalltalk。[wiki链接](https://zh.wikipedia.org/wiki/Thrift)
依赖条件
thrift核心代码使用C++写的,用了boost库。
执行以下命令,安装相关依赖:
yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel
thrift安装
1.到http://thrift.apache.org/download 这里下载的是 thrift-0.9.3.tar.gz
2. 在执行下列操作, 可根据需要选择相应的语言支持
#tar -zxvf thrift-0.9.3.tar.gz
#cd thrift-0.9.3
#./configure --with-boost=/usr/local --without-java --without-php
#make; make install
- 在终端输入:thrift –version 查看是否安装成功。
thrift使用
- 定义一个thrift文件, phone.thrift, 内容如下:
enum PhoneType {
HOME,
WORK,
MOBILE,
OTHER
}
struct Phone {
1: i32 id,
2: string number,
3: PhoneType type
}
- 使用thrift命令行,生成java, golang 代码
thrift -r --gen cpp phone.thrift
thrift -r --gen go phone.thrift
- 在gen-cpp目录执行下列操作
cp PhoneService_server.skeleton.cpp PhoneService_server.cpp
- 修改 PhoneService_server.cpp 中的 get 函数
void get(Phone& _return, const std::string& uid) {
// Your implementation goes here
_return.id = 1024;
_return.number = "13424562789";
_return.type = PhoneType::MOBILE;
printf("get\n");
}
- 编写makefile文件
BOOST_PATH = /usr/include/boost/
THRIFT_PATH = /usr/local/include/thrift
LIB_PATH = /usr/local/lib
SRC = phone_types.cpp phone_constants.cpp PhoneService.cpp PhoneService_server.cpp
server: ${SRC}
g++ -g -o PhoneServer ${SRC} -I${THRIFT_PATH} -I{BOOST_PATH} -lthrift
运行make, 生成 PhoneServer 的可执行文件, 运行PhoneServer
./PhoneServer
- 准备golang的客户端
#cd gen-go
#mkdir src
#mv phone src/phone
#mv src/phone/phone_service-remote src/phone_service-remote
修改 /etc/profile文件, 增加golang项目路径
export GOPATH=/usr/local/gopath:/home/denny/thrift-test/gen-go
使环境变量生效
source /etc/profile
编译 phone_service-remote
go build phone_service-remote
运行 phone_service-remote
./phone_service-remote
输出:
Phone({ID:1024 Number:13424562789 Type:MOBILE}) <nil>
至此thrift的简单应用就完结了
总结
对thrift研究也不是很多, 现在感觉来说thrift适合多语言混合编程,且系统内或系统间的交互依赖于RPC这时候的用处应该是挺大的。