Thrift的安裝和簡單示例

jopen 10年前發布 | 129K 次閱讀 Thrift WEB服務/RPC/SOA

本文只是簡單的講解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
  • </ul>

    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  </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959553871188207797" target="_blank"></a></div>
    

    </div> </div> 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.  </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959553871188207797" target="_blank"></a></div>
    

    </div> </div> 在本人電腦上make的時候會出現下面的bug,
    ar: .libs/ThriftTest_constants.o: No such file or directory

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

    </div> </div>

        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/   </pre><br />
    

    Thrift的簡單示例                                                            

    首先創建Thrift的語法規則文件,命名為server.thrift,內容如下:

        struct message
    {
    1:i32 seqId,
    2:string content
    }

    service serDemo  
    {  
        void put(1:message msg)  
    }  </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959553871188207797" target="_blank"></a></div>
    

    </div> </div> 在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());  
      }  </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959553871188207797" target="_blank"></a></div>
    

    </div> </div> 然后進行編譯就可以了: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;    
    }  </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959553871188207797" target="_blank"></a></div>
    

    </div> </div> 然后進行編譯: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  
    </div> </div>
    來自:http://blog.csdn.net/anonymalias/article/details/26154405

     本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!