首页 > 代码库 > Thrift RPC的一个简单c++ demo

Thrift RPC的一个简单c++ demo

Thrift是一种开源的跨语言的RPC服务框架,最初由facebook公司开发的,在2007年facebook将其提交apache基金会开源了。对于当时的facebook来说创造thrift是为了解决facebook系统中各系统间大数据量的传输通信以及系统之间语言环境不同需要跨平台的特性。

 

首先需要定义.thrift接口文件:

namespace cpp project
struct CompanyInfo{
1: i32 id;
2: string name;
3: string desc;
4: string country;
}

namespace cpp project
include "define.thrift"
service CompanyServlet {
bool Sender(1: list<define.CompanyInfo> companies);
oneway void Sender2(1: list<define.CompanyInfo> companies);
}

然后使用thrift --gen cpp rpc.thrift生成代码,生成的代码里有一个服务端框架文件(CompanyServlet_server.skeleton.cpp),可以以此来构建server。

编写server端代码

// 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::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

using namespace  ::project;

class CompanyServletHandler : virtual public CompanyServletIf {
 public:
  CompanyServletHandler() {
    // Your initialization goes here
  }

  bool Sender(const std::vector< ::project::CompanyInfo> & companies) {
    // Your implementation goes here
    printf("Sender\n");
    for(const ::project::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;
  }

  void Sender2(const std::vector< ::project::CompanyInfo> & companies) {
    // Your implementation goes here
    printf("Sender2\n");
  }

};

int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr<CompanyServletHandler> handler(new CompanyServletHandler());
  shared_ptr<TProcessor> processor(new CompanyServletProcessor(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;
}

这里只实现了简单的打印。

编写client代码

#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::protocol;
using namespace ::apache::thrift::transport;

using boost::shared_ptr;

using namespace  ::project;

int main(int argc, char *argv[])
{
    int port = 9090;
    shared_ptr<TTransport> tsocket(new TSocket("localhost", port));
    shared_ptr<TTransport> transport(new TBufferedTransport(tsocket));
    shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    project::CompanyServletClient client(protocol);

    for(;;){
        transport->open();
        CompanyInfo info;
        info.id = 700;
        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;
}

 贴上编译命令吧

g++ -W -g -Wno-unused -std=c++11 -o thrift_test_server.run thrift_test_server.cpp  ./gen-cpp/define_types.o  ./gen-cpp/rpc_types.o  ./gen-cpp/define_constants.o  ./gen-cpp/rpc_constants.o  ./gen-cpp/CompanyServlet.o -lthrift

Thrift RPC的一个简单c++ demo