This is a creation in Article, where the information may have evolved or changed.
- What is thrift?
- Dependency conditions
- Thrift Installation
- Thrift use
- Summarize
What is 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)
Dependency conditions
thrift核心代码使用C++写的,用了boost库。执行以下命令,安装相关依赖:
yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel
Thrift Installation
1. http://thrift.apache.org/download here to download is thrift-0.9.3.tar.gz
2. Select the appropriate language support as required when you do the following:
#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
- In terminal input: Thrift–version to see if the installation was successful.
Thrift use
- Define a thrift file, Phone.thrift, with the following content:
enum PhoneType { HOME, WORK, MOBILE, OTHER}struct12string3type}
- Using the thrift command line, generate Java, Golang code
thrift-r--gencppphone.thrift thrift-r--gengophone.thrift
- Perform the following actions in the Gen-cpp directory
cp PhoneService_server.skeleton.cpp PhoneService_server.cpp
- Modify the Get function in Phoneservice_server.cpp
_returnconst std::string& uid) { // Your implementation goes here _return1024; _return"13424562789"; _return.type = PhoneType::MOBILE; printf("get\n"); }
- Writing makefile Files
= /usr/include/boost/= /usr/local= /usr/local= phone_types.cpp phone_constants.cpp PhoneService.cpp PhoneService_server.cppserver: ${SRC} g++-g-o-I-I-lthrift
Run make, generate the Phoneserver executable, run the Phoneserver
./PhoneServer
- Preparing the Golang Client
#cd gen-go#mkdir src#mv phone src/phone#mv src/phone/phone_service-remote src/phone_service-remote
Modify the/etc/profile file to increase the Golang project path
GOPATH=/usr/local/gopath:/home/denny/thrift-test/gen-go
Make environment variables effective
source /etc/profile
Compiling phone_service-remote
go build phone_service-remote
Run Phone_service-remote
./phone_service-remote
Output:
Phone({ID:1024 Number:13424562789 Type:MOBILE}) <nil>
This is the end of thrift's simple application.
Summarize
The study of thrift is not much, it is now felt that thrift is suitable for multi-language mixed programming, and the interaction between the system or the system depends on the RPC this time the usefulness should be quite large.