Thrift的安裝方法和簡單一實例_Linux

來源:互聯網
上載者:User

本文只是簡單的講解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的結果對於使用非同步伺服器模型是必須的。

anonymalias@anonymalias-Rev-1-0:~/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的安裝方法和簡單一實例全部內容了,希望大家多多支援雲棲社區~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.