Thrift is an open-source, cross-lingual RPC service framework originally developed by Facebook, which Facebook submitted to the Apache Foundation in 2007. For Facebook at the time, the creation of thrift was designed to address the cross-platform nature of the large data traffic between systems in the Facebook system and the different language environments between the systems.
First you need to define the. Thrift interface File:
NamespaceCPPprojectstruct companyinfo{1: i32ID;2:stringname;3:stringdesc;4:stringCountry;} NamespaceCPPProjectinclude"Define.thrift"service Companyservlet {BOOLSender (1: list<define.companyinfo>companies); OneWay void Sender2 (1: list<define.companyinfo>companies);}
The code is then generated using the Thrift--gen CPP Rpc.thrift, which has a server-side framework file (COMPANYSERVLET_SERVER.SKELETON.CPP) that can be used to build the servers.
Writing server-side code
//This autogenerated skeleton file illustrates how to build a server.//You should copy it to another filename to avoid overwriting it.#include"./gen-cpp/companyservlet.h"#include<thrift/protocol/TBinaryProtocol.h>#include<thrift/server/TSimpleServer.h>#include<thrift/server/TThreadPoolServer.h>#include<thrift/server/TNonblockingServer.h>#include<thrift/transport/TServerSocket.h>#include<thrift/transport/TBufferTransports.h>#include<cstdio>#include<cstdlib>using namespace:: Apache::thrift;using namespace: : Apache::thrift::p rotocol;using namespace:: Apache::thrift::transport;using namespace:: apache::thrift::server;usingboost::shared_ptr;using namespace::p roject;classCompanyservlethandler:Virtual PublicCompanyservletif { Public: Companyservlethandler () {//Your initialization goes here } BOOLSender (Conststd::vector<::p roject::companyinfo> &companies) { //Your implementation goes hereprintf"sender\n"); for(Const::p Roject::companyinfo &info:companies) {printf ("id[%d], name[%s], desc[%s], country[%s]\n", Info.id, Info.name.c_str (), Info.desc.c_str (), Info.country.c_str ()); } return true; } voidSender2 (Conststd::vector<::p roject::companyinfo> &companies) { //Your implementation goes hereprintf"sender2\n"); }};intMainintargcChar**argv) { intPort =9090; shared_ptr<CompanyServletHandler> Handler (NewCompanyservlethandler ()); shared_ptr<TProcessor> Processor (NewCompanyservletprocessor (handler)); shared_ptr<TServerTransport> Servertransport (NewTserversocket (port)); shared_ptr<TTransportFactory> Transportfactory (Newtbufferedtransportfactory ()); shared_ptr<TProtocolFactory> Protocolfactory (Newtbinaryprotocolfactory ()); Tsimpleserver Server (processor, Servertransport, Transportfactory, protocolfactory); Server.serve (); return 0;}
Only simple printing is achieved here.
Writing client code
#include"./gen-cpp/companyservlet.h"#include<thrift/protocol/TBinaryProtocol.h>#include<thrift/transport/TBufferTransports.h>#include<thrift/transport/TSocket.h>#include<cstdio>#include<cstdlib>using namespace:: Apache::thrift;using namespace: : Apache::thrift::p rotocol;using namespace:: Apache::thrift::transport;usingboost::shared_ptr;using namespace::p roject;intMainintargcChar*argv[]) { intPort =9090; shared_ptr<TTransport> Tsocket (NewTsocket ("localhost", port)); shared_ptr<TTransport> Transport (NewTbufferedtransport (Tsocket)); shared_ptr<TProtocol> Protocol (NewTbinaryprotocol (transport)); Project::companyservletclient Client (protocol); for(;;) {Transport-open (); CompanyInfo info; Info.id= the; Info.name="Tencent"; Info.desc="Integrated Internet Service Provider"; Info.country=" China"; Std::vector<CompanyInfo>v; V.push_back (info); if(!client. Sender (v)) printf ("Sender failed!\n"); Transport-Close (); Sleep (1); } return 0;}
Stick to the build command.
g++-w-g-wno-unused-std=c++11-o thrift_test_server.run thrift_test_server.cpp./gen-cpp/define_types.o./GEN-CPP/RP C_TYPES.O./gen-cpp/define_constants.o./gen-cpp/rpc_constants.o./gen-cpp/companyservlet.o-lthrift
A simple C + + demo of Thrift RPC