Thrift can be implemented in C + +, Java, Python and other languages automatically generated, here in C + + as an example.
This paper consists of 5 parts, of which 1, 2 and 3 are focused.
1. Preparation of [. Thrift] Files
You can Google to the official example as follows:
struct student{
1:i32 Sno,
2:string sname,
3:bool ssex,
4:i16 sage,
}
service serv{
void Put (1:student s),
}
Save the above code as a student.thrift file.
2. Automatically generate server-side programs
Enter the following command in terminal to automatically generate [. cpp] and [. h] files.
Thrift-r--gen CPP Student.thrift
The documents obtained are as follows:
Serv.cpp
Serv.h
Serv_server.skeleton.cpp
student_constants.cpp
student_constants.h
Student_types.cpp
Student_types.h
In Serv_server.skeleton.cpp, there is a main function running on the server side. The Serv and student of these file names are related to the thrift file you originally created.
3. Writing Client Programs
#include "Serv.h" //Your. h File
#include <transport/TSocket.h>
#include <transport/ tbuffertransports.h>
#include <protocol/TBinaryProtocol.h>
using namespace Apache::thrift;
Using namespace Apache::thrift::p rotocol;
using namespace Apache::thrift::transport;
Using Boost::shared_ptr;
int main (int argc, char **argv) {
boost::shared_ptr<tsocket> socket (new Tsocket ("localhost", 9090));
Boost::shared_ptr<ttransport> Transport (new Tbufferedtransport (socket));
Boost::shared_ptr<tprotocol> Protocol (new Tbinaryprotocol (transport));
Transport->open ();
Your Codes
transport->close ();
return 0;
}
4. Compile/Link
g++-g-i/home/michael/opt/include/thrift-l/home/michael/opt/lib/-lthrift Serv.cpp student_types.cpp Student_ Constants.cpp serv_server.skeleton.cpp-o server
g++-g-i/home/michael/opt/include/thrift-l/home/michael/opt/ lib/-lthrift-lm-pthread-lz-lrt-lssl Serv.cpp student_types.cpp student_constants.cpp Client
5. Run
./server
./client