Skip to content

xiaoxinyi/thrift-cpp-demo

Repository files navigation

thrift cpp demo

运行 demo 的步骤

1.克隆仓库

git clone https://github.com/xiaoxinyi/thrift-cpp-demo thrift-cpp

2.利用tutorial.thriftshared.thrift生成需要的接口文件。注意,这两个文件都需要使用,在tutoial.thrift中引用了(继承了)shared.thrift

thrift -r --gen cpp tutorial.thrift

3.利用CMakeLists.txt生成demo的可执行文件。

mkdir -p build
cd build
cmake ..
make

4.运行服务器端和客户端的的程序。

./server &
./client

CMakeLists.txt说明

主要生成3个目标文件,分别是thrift_gen_cpp.aserverclient

在链接生成这三个目标文件的时候需要链接thrift库和pthread库。我的thrift库的路径是/usr/local/lib/libthrift.a。所以必须加上以下的两句:

# 比如链接生成目标thrift_gen_cpp
target_link_libraries(thrift_gen_cpp /usr/local/lib/libthrift.a)
target_link_libraries(thrift_gen_cpp pthread)

NOTE:因为需要用到boost库,添加include_directories("/usr/include/boost/")

thrift说明

定义在tutorial.thrift中的服务接口:

service Calculator extends shared.SharedService {

  /**
   * A method definition looks like C code. It has a return type, arguments,
   * and optionally a list of exceptions that it may throw. Note that argument
   * lists and exception lists are specified using the exact same syntax as
   * field lists in struct or exception definitions.
   */

   void ping(),

   i32 add(1:i32 num1, 2:i32 num2),

   i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),

   /**
    * This method has a oneway modifier. That means the client only makes
    * a request and does not listen for any response at all. Oneway methods
    * must be void.
    */
   oneway void zip()

}

从这个文件中,我们可以看到Caculator服务继承了shared.SharedService服务。

我们可以从shared.thrift中看到,它声明了SharedService服务的所有接口。

service SharedService {
  SharedStruct getStruct(1: i32 key)
}

现在我们来看一下总共这四个接口:其中3个在Caculator服务中声明,还有一个在SharedService服务中声明。其中Caculator服务所对应的类是gen-cpp/Caculator.h中定义的叫CalculatorIf的抽象类。同理SharedService服务所对应的类是gen/SharedService.h中定义的叫SharedServiceIf的抽象类。根据两个.thrift文件,我们可以推断出,这两个抽象类存在继承关系,CaculatorIf类继承了SharedServiceIf类。

我们分别来看这两个抽象类:

CaculatorIf

 class CalculatorIf : virtual public  ::shared::SharedServiceIf {
  public:
   virtual ~CalculatorIf() {}

   /**
    * A method definition looks like C code. It has a return type, arguments,
    * and optionally a list of exceptions that it may throw. Note that argument
    * lists and exception lists are specified using the exact same syntax as
    * field lists in struct or exception definitions.
    */
   virtual void ping() = 0;
   virtual int32_t add(const int32_t num1, const int32_t num2) = 0;
   virtual int32_t calculate(const int32_t logid, const Work& w) = 0;

   /**
    * This method has a oneway modifier. That means the client only makes
    * a request and does not listen for any response at all. Oneway methods
    * must be void.
    */
   virtual void zip() = 0;
 };

SharedServiceIf

class SharedServiceIf {
 public:
  virtual ~SharedServiceIf() {}
  virtual void getStruct(SharedStruct& _return, const int32_t key) = 0;
};

从这两个抽象类定义中,我们看到类CaculatorIf虚继承于SharedServiceIf。以及声明了4个纯虚函数,与两个.thrift文件中的接口一一对应。

thrift服务器端

服务器端我们需要写一个自己的类继承CaculatorIf,在这个demo中写了一个CaculatorHandler这样一个类,这个类中实现了祖先类中所有的虚函数,即4个接口函数。

在demo中,还实现了一个叫CalculatorCloneFactory的类,这个类虚继承于CalculatorIfFactory。是否实现这个类,取决于你要使用的服务类别。具体可以参考CppServer.cpp文件。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published