(转)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的編譯就不再這里介紹了,我分別使用了boost1.55或boost1.49編譯通過;
按需編譯,如果不需要異步server就可以不編譯libevent,否則可以點此下載libevent-2.0.21-stable;
下載針對你系統版本的openssl庫,windows下有編譯好的二進制文件,可以直接下載,32位/62位系統openssl; Linux發行版一般都自帶ssl庫;
thrift在Windows下的安裝
我是在Windows7 64bit, VS2010編譯的。 Windows下編譯倒也不麻煩,簡單介紹如下:
說明: thrift-0.9.3這一版的release其實在windows下是編譯不過的,因為vs工程中要編譯的Thrift.cpp已經不存在了,從工程中移除就可以順利編譯了,參考thrift-pull-739。
另外還可以自行編譯thrift文件的生成工具,當然也可以直接從官網下載,這里給出編譯步驟:
thrift在linux(Centos)下的安裝
我是在Centos6.4 64bit,g++ 4.4.7編譯的,編譯很簡單,分別可以使用cmake或者make工具進行編譯,這里不再多做介紹,當然,編譯過程中缺少了某些庫什么的,就先按照即可,更詳細的步驟請看本文的參考文章鏈接。
開發步驟
入門示例
下面就演示一個簡單的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平台下的安装和使用示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 盘点最经典的外包案例
- 下一篇: P1867 【Mc生存】经验值