用thrift实现客户端和服务端的C++代码 - 金美光

系统 2319 0

用thrift实现客户端和服务端的C++代码 - 金美光的小屋 - 博客频道 - CSDN.NET

用thrift实现客户端和服务端的C++代码

分类: thrift 1058人阅读 评论 (0) 收藏 举报

Getting started

The first thing you need to know is that the C++ code generated by Thrift compiles only on Unix based systems, although some success has been reported using Cygwin on Win32 in   ThriftInstallationWin32 .

Requirements

Make sure that your system meets the requirements as noted in   ThriftRequirements

  • Thrift library files
  • Thrift header files.

Installing the Thrift library

Installing the Thrift library is trivial to link with the generated code.

1.     Download a snapshot of Thrift and extract if you haven't done so already -   Direct Link

2.     Navigate to   lib/cpp   using the terminal of your choice

3.     Run    make   to compile

4.     Run    make install   to install the library. Your user needs root permissions.

Generating the server code

In this example we use an imaginary file called   your_thrift_file.thrift :

#!/usr/local/bin/thrift --gen cpp

 

namespace cpp Test

/*

struct param     //thrift 中的 struct 形式其实是参数,客户端要传递的参数类型必须在 thrift 文件中用 struct 先进行定义。注意只是客户端要传递的参数,服务器端的参数无需在 thrift 文件中定义。

{

  1:double left,

  2:double right,

}

/*

service Something { // service 中声明要实现的函数

  i32 ping ()    // 服务器端要实现的函数的声明

}

Now run:

thrift --gen cpp your_thrift_file.thrift

Exploring the generated code - The Server

The generated code should be under the   gen-cpp   directory. You will see a number of generated C++ and header files along with an automatically generated server skeleton code (in bold).

  • Something.cpp
  • Something.h
  • Something_server.skeleton.cpp
  • your_thrift_file_constants.cpp
  • your_thrift_file_constants.h
  • your_thrift_file_types.cpp
  • your_thrift_file_types.h

Implementing the Server

Copy the generated server skeleton to a file named Something_server.cpp and keep the original:

cp Something_server.skeleton.cpp Something_server.cpp

When this server is run in console it prints "ping" on the console window each time the function is called from a client.

Here's the autogenerated skeleton file to illustrate how to write a server:   Something_server.cpp

#include "Something.h"

#include <protocol/TBinaryProtocol.h>

#include <server/TSimpleServer.h>

#include <transport/TServerSocket.h>

#include <transport/TTransportUtils.h>

 

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 Test;

 

class SomethingHandler : virtual public SomethingIf {

  public:

  SomethingHandler() {

   // Your initialization goes here

  }

 

  int32_t ping() {

   // Your implementation goes here

printf("ping/n");

应该添加一条返回语句,源程序省略了,在此补上。

return 0;

  }

 

};

 

int main(int argc, char **argv) {

  int port = 9090; // 端口是随机分配的 可修改,但要保持一致

  shared_ptr<SomethingHandler> handler(new SomethingHandler());

  shared_ptr<TProcessor> processor(new SomethingProcessor(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;

}

Compiling/Building the server

To quickly build a binary using a single command use:

mv Something_server.skeleton.cpp Something_client.cpp.backup

g++ -Wall -I/usr/local/include/thrift *.cpp -lthrift -o something  

// 这里首先得把 Something_server.skeleton.cpp 的文件名做修改否则编译时会出现问题,我们不妨先把 Something_server.skeleton.cpp 文件名改成 Something_client.cpp.backup ,最后在把文件名改成 cpp 格式,并把 client cpp 代码写在这里头

Compiling

You need to point your compiler to the thrift include path (CXX flag:    -I/usr/local/include/thrift )

g++ -Wall -I/usr/local/include/thrift -c Something.cpp -o something.o

g++ -Wall -I/usr/local/include/thrift -c Something_server.cpp -o server.o

g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_constants.cpp -o constants.o

g++ -Wall -I/usr/local/include/thrift -c your_thrift_file_types.cpp -o types.o

Linking

You need to point your linker to the thrift library. (Linker flag:    -lthrift    or    -l/usr/local/lib/libthrift.so 

g++ -L/usr/local/lib -lthrift *.o -o Something_server

Writing the client code

thrift   generates a client interface, but you have to hook it up a bit on your own.

以下的代码是标准的客户端代码模板,把这段代码 copy client cpp 文件中,黄色标记的部分是主要需要修改的地方。

#include "Something.h"   // As an example

 

#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 namespace Test;

 

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));

 

  SomethingClient client(protocol);

  transport->open();

client.ping(); /* 客户端只要写了这条语句,服务器就将执行。

                 其他的语句都是套话。可以直接 copy

              ping 是执行的函数名,在 thrift 中声明了。

如果函数的原型,既 ping 是一个带参数的函数,则使用 ping 函数之前先进行参数的赋值等操作,如

  param op; 这个 param 结构体也需要在 thrift 文件中先定义。

  op.left = 2.0;

  op.right = 3.5;

client.ping(op);*/

  transport->close();

 

  return 0;

}

Compiling

再次把 Something_client.cpp.backup 改成 Something_client.cpp

mv Something_client.cpp.backup Something_client.cpp

g++ -Wall -I/usr/local/include/thrift -c Something_client.cpp -o client.o

Linking

g++ -L/usr/local/lib -lthrift client.o something.o constants.o types.o -o Something_client

也可以把所有的语句做成一个 makefile ,如下

Compiling/Building everything with Makefile

GEN_SRC := Something.cpp your_thrift_file_constants.cpp your_thrift_file_types.cpp

GEN_OBJ := $(patsubst %.cpp,%.o, $(GEN_SRC))

 

THRIFT_DIR := /usr/local/include/thrift

BOOST_DIR := /usr/local/include

 

INC := -I$(THRIFT_DIR) -I$(BOOST_DIR)

 

.PHONY: all clean

 

all: something_server something_client

 

%.o: %.cpp

        $(CXX) -Wall $(INC) -c $Slt; -o $@

 

something_server: Something_server.o $(GEN_OBJ)

        $(CXX) -L/usr/local/lib -lthrift $^ -o $@

 

something_client: Something_client.o $(GEN_OBJ)

        $(CXX) -L/usr/local/lib -lthrift $^ -o $@

 

clean:

        $(RM) *.o something_server something_client

Appendix: About TNonblockingServer

If you are writing an application that will serve a lot of connection (like php front end calling thrift service), you'd better use TNonblockingServer. TNonblockingServer can accept a lot of connections while throttling the processor threads using a pool.

* TNonblockingServer with a thread pool is the c++ alternative of the JAVA THsHaServer;   * TNonblockingServer withOUT a thread pool is the c++ alternative of the JAVA TNonblockingServer;

Server code with thread pool:

    shared_ptr<TProcessor> processor(new SomethingProcessor(handler));

    shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

 

    // using thread pool with maximum 15 threads to handle incoming requests

    shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(15);

    shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());

    threadManager->threadFactory(threadFactory);

    threadManager->start();

    TNonblockingServer server(processor, protocolFactory, 8888, threadManager);

    server.serve();

 

    // ...

C++ client code (you have to use TFramedTransport here):

    boost::shared_ptr<TSocket> socket(new TSocket("localhost", 8888));

    boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));

    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

 

    SomethingClient client(protocol);

    transport->open();

    // do something here...

    transport->close();

用thrift实现客户端和服务端的C++代码 - 金美光的小屋 - 博客频道 - CSDN.NET


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论