日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

(转)Thrift在Windows及Linux平台下的安装和使用示例

發布時間:2023/12/10 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)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。

安裝依賴庫

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

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

  • 解壓縮源代碼,進入到lib\cpp目錄下,打開Thrift.sln,里面有libthrift和libthriftnb兩個工程,其中libthrift工程是常規的阻塞型server端(單線程server,一個連接一個線程server,線程池server),libthriftnb工程是非阻塞(non-blocking)模式的服務server端,也只有編譯libthriftnb時才需要依賴libevent庫,否則可以不編譯libevent庫;
  • 設置依賴庫頭文件和庫文件,這就不再介紹了;
  • 編譯,順利的話就OK了,會在lib\cpp\Debug目錄下生成libthrift.lib和libthriftnb.lib(如果編譯的話);
  • 說明: thrift-0.9.3這一版的release其實在windows下是編譯不過的,因為vs工程中要編譯的Thrift.cpp已經不存在了,從工程中移除就可以順利編譯了,參考thrift-pull-739。

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

  • 將compiler\cpp\src\windows\version.h.in文件拷貝到compiler\cpp\src\目錄下,并重命名為version.h;
  • 到compiler\cpp目錄下,打開compiler.sln,編譯即可
  • thrift在linux(Centos)下的安裝

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

    開發步驟

  • 寫一個.thrift文件,也就是IDL(Interface Description File,接口描述文件);
  • 用Thrift的IDL生成工具(windows下就是上面提供下載鏈接的thrift-0.9.1.exe, Linux下就是/usr/local/bin/thrift程序) ,然后根據需要生成目標語言代碼;
  • server端程序引入第2步生成的代碼,實現RPC業務代碼;
  • client端程序引入第2步生成的代碼,實現RPC調用邏輯;
  • 用第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官方安裝手冊(譯)

    總結

    以上是生活随笔為你收集整理的(转)Thrift在Windows及Linux平台下的安装和使用示例的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。