天天看點

Thrift的安裝和簡單示例

本文隻是簡單的講解Thrift開源架構的安裝和簡單使用示例,對于詳細的講解,後面在進行闡述。

Thrift簡述                                                                        

Thrift是一款由Fackbook開發的可伸縮、跨語言的服務開發架構,該架構已經開源并且加入的Apache項目。Thrift主要功能是:通過自定義的Interface Definition Language(IDL),可以建立基于RPC的用戶端和服務端的服務代碼。服務代碼的生成是通過Thrift内置的代碼生成器來實作的。Thrift的跨語言性展現在,它可以生成C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml , Delphi等語言的代碼,且它們之間可以進行透明的通信。

Thrift的安裝                                                                    

安裝版本為:Thrift v0.9.1

系統版本:Ubuntu 14.04 64位

基本安裝環境:

  • g++ 4.2
  • boost 1.53.0
  • libssl-dev 

Thrift的編譯器即代碼生成器是由C++編寫的,是以需要g++來進行編譯安裝,且Thrift源碼中用到了boost庫中相關實作,例如shared_ptr,是以要事先安裝boost庫。

Thrift通信過程中使用ssl對資料進行安全包含,如果為安裝該庫,會在configure時出現 configure: error: "Error: libcrypto required." 

Thrift提供了,TThreadSever, TThreadPoolServer, TNonblockingServer四種伺服器架構,TSimpleServer以單一主線程阻塞的方式進行事件處理,TThreadPoolServer以多線程阻塞的方式提供服務,TNonblockingServer以多線程非阻塞方式工作。TNonblockingServer服務模型的使用需要事先安裝 libevent,libevent-dev 庫,libevent是異步事件處理的程式庫,其包含我們常用的poll,select,epoll等異步處理函數。

安裝步驟:

$./configure
$make
#sudo make install
           

configure的結果最後一部分如下,其中 Build TNonblockingServer .. : yes 的結果對于使用異步的伺服器模型是必須的。 

[email protected]:~/download/thrift-0.9.1$./configure
......
thrift 0.9.1 Building C++ Library ......... : yes Building C (GLib) Library .... : no Building Java Library ........ : no Building C# Library .......... : no Building Python Library ...... : yes Building Ruby Library ........ : no Building Haskell Library ..... : no Building Perl Library ........ : no Building PHP Library ......... : no Building Erlang Library ...... : no Building Go Library .......... : no Building D Library ........... : no C++ Library:  Build TZlibTransport ...... : yes  Build TNonblockingServer .. : yes  Build TQTcpServer (Qt) .... : no Python Library:  Using Python .............. : /usr/bin/python If something is missing that you think should be present, please skim the output of configure to find the missing component. Details are present in config.log.
           

在本人電腦上make的時候會出現下面的bug, 

ar: .libs/ThriftTest_constants.o: No such file or directory

不知道Makefile如何生成的,導緻上面這個編譯檔案路徑有問題,解決方法有下面兩種:

method1:
解決的方法時直接從Github(git://git.apache.org/thrift.git)上git clone 源碼,先運作./bootstrap.sh,在按照configure安裝。

method2:
可以将已經編譯的test/cpp/*.o複制到test/cpp/.libs後,繼續編譯就可以了
cp test/cpp/*.o test/cpp/.libs/      

Thrift的簡單示例                                                             

首先建立Thrift的文法規則檔案,命名為server.thrift,内容如下: 

struct message
{
  1:i32 seqId,
  2:string content } service serDemo {  void put(1:message msg) }      

在shell下面執行執行:

thrift -gen cpp server.thrift      

該語句用于建立c++服務架構,建立成功後會在該目錄下生成gen-cpp檔案夾,然後修改該目錄下的serDemo_server.skeleton.cpp,在put函數中添加如下代碼: 

class serDemoHandler : virtual public serDemoIf {
 public:
  serDemoHandler() {
    // Your initialization goes here
  }

  void put(const message& msg) {
    // Your implementation goes here
    printf("receive message: id: %d, content: %s\n", msg.seqId, msg.content.c_str());
  }
           

然後進行編譯就可以了 : g++ -o server *.cpp -lthrift 

上面是server架構的代碼,對于client的架構其實已經建立,但是現在需要添加client執行代碼,可以在該目錄下建立client.cpp,然後輸入以下内容,下面的内容可以作為SimpleServer的client的模闆,隻需修改注釋的部分。

// -------------------------替換成對應service名字的.h  檔案------------------------
#include "SerDemo.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", 9090));  boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));  boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));  transport->open();  // ----------------------------我們的代碼寫在這裡------------------------------  message msg;  msg.seqId = 1;  msg.content = "client message";  client.put(msg);  //--------------------------------------------------------------------------  transport->close();  return 0; }
           

然後進行編譯: g++ -o client *[^n].cpp - lthrift ,也可以将serDemo_server.skeleton.cpp移動到其它目錄,使用 g++ -o client *.cpp - lthrift指令。

然後就可以執行了,啟動server後,啟動client,server執行如下:

anonymalias@anonymalias-Rev-1-0:~/code/thriftSerDemo/gen-cpp$ ./server 
receive message: id: 1, content: client message
           

下周打算翻譯Thrift Whitepaper,come on! 

轉載于:https://www.cnblogs.com/martinjinyu/articles/3840156.html