(轉)Thrift在Windows及Linux平臺下的安裝和使用示例

轉載自Thrift在Windows及Linux平臺下的安裝和使用示例

thrift介紹

Apache Thrift 是 Facebook 實現的一種高效的、支持多種編程語言的RPC(遠程服務調用)框架。

本文主要目的是分別介紹在Windows及Linux平臺下的Thrift安裝步驟,以及實現一個簡單的demo演示Thrift的使用方法。更多Thrift原理留在以後再行介紹。

thrift安裝

源碼下載:thrift官網,或者thrift-github地址,我下載的是thrift-0.9.3.tar.gz

安裝依賴庫

  1. boost
    boost的編譯就不再這裏介紹了,我分別使用了boost1.55或boost1.49編譯通過;
  2. libevent
    按需編譯,如果不需要異步server就可以不編譯libevent,否則可以點此下載libevent-2.0.21-stable;
  3. openssl
    下載針對你係統版本的openssl庫,windows下有編譯好的二進制文件,可以直接下載,32位/62位系統openssl; Linux發行版一般都自帶ssl庫;

thrift在Windows下的安裝

我是在Windows7 64bit, VS2010編譯的。 Windows下編譯倒也不麻煩,簡單介紹如下:

  1. 解壓縮源代碼,進入到lib\cpp目錄下,打開Thrift.sln,裏面有libthrift和libthriftnb兩個工程,其中libthrift工程是常規的阻塞型server端(單線程server,一個連接一個線程server,線程池server),libthriftnb工程是非阻塞(non-blocking)模式的服務server端,也只有編譯libthriftnb時才需要依賴libevent庫,否則可以不編譯libevent庫;
  2. 設置依賴庫頭文件和庫文件,這就不再介紹了;
  3. 編譯,順利的話就OK了,會在lib\cpp\Debug目錄下生成libthrift.lib和libthriftnb.lib(如果編譯的話);

說明: thrift-0.9.3這一版的release其實在windows下是編譯不過的,因爲vs工程中要編譯的Thrift.cpp已經不存在了,從工程中移除就可以順利編譯了,參考thrift-pull-739

另外還可以自行編譯thrift文件的生成工具,當然也可以直接從官網下載,這裏給出編譯步驟:

  1. 將compiler\cpp\src\windows\version.h.in文件拷貝到compiler\cpp\src\目錄下,並重命名爲version.h;
  2. 到compiler\cpp目錄下,打開compiler.sln,編譯即可

thrift在linux(Centos)下的安裝

我是在Centos6.4 64bit,g++ 4.4.7編譯的,編譯很簡單,分別可以使用cmake或者make工具進行編譯,這裏不再多做介紹,當然,編譯過程中缺少了某些庫什麼的,就先按照即可,更詳細的步驟請看本文的參考文章鏈接。

開發步驟

  1. 寫一個.thrift文件,也就是IDL(Interface Description File,接口描述文件);
  2. 用Thrift的IDL生成工具(windows下就是上面提供下載鏈接的thrift-0.9.1.exe, Linux下就是/usr/local/bin/thrift程序) ,然後根據需要生成目標語言代碼;
  3. server端程序引入第2步生成的代碼,實現RPC業務代碼;
  4. client端程序引入第2步生成的代碼,實現RPC調用邏輯;
  5. 用第4步生成的程序就可以調用第3步實現的遠程服務了;

入門示例

下面就演示一個簡單的server端和client端程序。

設計thrift文件(IDL)

假設實現這麼一個簡單服務,client通過hello接口發送自己的名字,且需要server端回覆,比如 hello.thrift:

service HelloService
{
    void hello(1: string name);
}

通過IDL工具生成源代碼

執行thrift命令生成源文件

thrift --gen cpp hello.thrift                 # centos下
thrift-0.9.3.exe --gen cpp hello.thrift   # Windows下
thrift-0.9.3.exe --gen py hello.thrift    # Windows下python代碼

以上命令表示生成C++語言的源代碼,然後會生成一個gen-cpp目錄,裏面包含自動生成的幾個源代碼文件:

hello_constants.cpp
hello_constants.h
HelloService.cpp
HelloService.h
HelloService_server.skeleton.cpp
hello_types.cpp
hello_types.h

實現server端程序

HelloService_server.skeleton.cpp就是默認的server端程序入口,可以直接修改該文件,或者拷貝一份再做修改(我是拷貝並重命名爲server.cpp),以便增加自己的邏輯處理:

class HelloServiceHandler : virtual public HelloServiceIf {
 public:
  HelloServiceHandler() {
    // Your initialization goes here
  }

  void hello(const std::string& name) {
    // Your implementation goes here
    // 這裏只簡單打印出client傳入的名稱
    printf("hello, I got your name %s\n", name.c_str());
  } 
};

如果是在linux平臺下,直接通過g++編譯:

g++ -o server hello_constants.cpp  HelloService.cpp hello_types.cpp  server.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift

如果是在Windows平臺下,通過vs2010新建win32控制檯工程,將gen-cpp目錄下的所有文件複製到新工程下,設置頭文件包含和lib庫目錄。 比如設置libthrift.lib的頭文件目錄爲thrift-0.9.3\lib\cpp\src\thrift,lib庫目錄爲thrift-0.9.3\lib\cpp\Debug。

實現client端程序

因爲沒有默認的client實現,所以需要新建一個client.cpp文件,自己增加實現:

#include <stdio.h>
#include <string>
#include "transport/TSocket.h"
#include "protocol/TBinaryProtocol.h"
#include "server/TSimpleServer.h"
#include "transport/TServerSocket.h"
#include "transport/TBufferTransports.h"
#include "hello_types.h"
#include "HelloService.h"
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;

int main(int argc, char** argv)
{
    shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
    shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    HelloServiceClient client(protocol);
    try
    {
        transport->open();
        client.hello("cpper.info");
        transport->close();
    }
    catch(TException& tx)
    {
        printf("ERROR:%s\n",tx.what());
    }
}

如果是在linux平臺下,直接通過g++編譯:

g++ -o client client.cpp hello_constants.cpp  HelloService.cpp hello_types.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift

如果是在Windows平臺下,通過vs2010新建win32控制檯工程,將gen-cpp目錄下的所有文件(除HelloService_server.skeleton.cpp之外)複製到新工程下,並增加上面手動實現的client.cpp

通過以上步驟,就實現一個簡單的RPC server和client程序了,可以分別運行進行測試看看效果怎麼樣。

Reference

Thrift官方安裝手冊(譯)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章