1,定义Thrift接口文件test.thrift:
struct TestStruct { 1: i16 v1, 2: i32 v2, 3: i64 v3, 4: bool v4, 5: string v5, } service Server { string echo (1: string arg1), TestStruct echoStruct (1: TestStruct arg1), }
2,通过Thrift生成框架,包含以下几个文件:Server.cpp Server_server.skeleton.cpp test_constants.cpp test_types.cpp Server.h test_constants.h test_types.h
thrift --gen cpp test.thrift -o <output directory>
3,编写客户端代码,在生成的Server类中,包含客户端调用的接口类ServerClient,因此客户端只需要关心接口函数的输入,输出参数以及异常处理,代码如下:
#include "Server.h" #include <transport/TSocket.h> #include <transport/TBufferTransports.h> #include <protocol/TBinaryProtocol.h> using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; using boost::shared_ptr; int main(int argc, char **argv) { boost::shared_ptr<TSocket> socket(new TSocket("localhost", 5555)); boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket)); boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); transport->open(); ServerClient client(protocol); std::string str = "hello world"; std::string retStr; client.echo(retStr, str); TestStruct test; test.v1 = 111; test.v2 = 222; test.v3 = 333; test.v4 = false; test.v5 = "thrift"; TestStruct retVal; client.echoStruct(retVal, test); transport->close(); return 0; }
4,编译客户端代码,生成可执行文件client,通过如下命令行:
g++ -g -I/usr/local/include/thrift -L/usr/local/lib -lm -pthread -lz -lrt -lssl Server.cpp test_types.cpp test_constants.cpp test_client.cpp -o client -lthrift
5,服务端的实现,在生成的Server_server.skeleton.cpp文件中包含服务端的基本骨架,只需要在相应的接口函数里面做我们关心的事情,代码如下:
using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server; using boost::shared_ptr; class ServerHandler : virtual public ServerIf { public: ServerHandler() { // Your initialization goes here } void echo(std::string& _return, const std::string& arg1) { // Your implementation goes here printf("echo %s\n", arg1.c_str()); } void echoStruct(TestStruct& _return, const TestStruct& arg1) { // Your implementation goes here printf("echoStruct: %d %d %ld %d %s\n", arg1.v1, arg1.v2, arg1.v3, arg1.v4, arg1.v5.c_str()); } }; int main(int argc, char **argv) { int port = 5555; shared_ptr<ServerHandler> handler(new ServerHandler()); shared_ptr<TProcessor> processor(new ServerProcessor(handler)); shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve(); return 0; }
6,编译服务端的代码,生成可执行文件server,如下命令:
g++ -g -I/usr/local/include/thrift -L/usr/local/lib -lm -pthread -lz -lrt -lssl Server.cpp test_types.cpp test_constants.cpp Server_server.skeleton.cpp -o server -lthrift
7,如上步骤都正常通过以后,先运行server,服务端会在5555端口监听,然后运行客户端,自动连接到服务端,并调用相应接口,在服务端的输出如下:
echo hello world
echoStruct: 111 222 333 0 thrift