前言:thrift的版本問題比較麻煩,高版本的在c++上需要c++ 11的支持,而低版本則可能在go的支持上有問題,測試發現thrift 9.0以上需要c++ 11,而thrift 8.0則在go的支持上可能會存在問題。使用的發行版本是CentOS 6.5,GCC 4.4.7。最終選擇使用thrift-0.8.0來編譯安裝。
wget http://archive.apache.org/dist/thrift/0.8.0/thrift-0.8.0.tar.gztar -zxvf thrift-0.8.0.tar.gzcd thrift-0.11.0./configure #這一步配置可以選擇支持的語言,如Python,c++,go等??梢?/configure --help來查看可配置項。如./configure --without-gomake && make install
注意:在執行完configure后,會顯示實際上支持的庫,比如
thrift 0.11.0 Building C (GLib) Library .... : noBuilding C# (Mono) Library ... : noBuilding C++ Library ......... : yesBuilding D Library ........... : noBuilding Dart Library ........ : noBuilding dotnetcore Library .. : noBuilding Erlang Library ...... : noBuilding Go Library .......... : noBuilding Haskell Library ..... : noBuilding Haxe Library ........ : noBuilding Java Library ........ : noBuilding Lua Library ......... : noBuilding NodeJS Library ...... : noBuilding Perl Library ........ : noBuilding PHP Library ......... : noBuilding Plugin Support ...... : noBuilding Python Library ...... : yesBuilding Py3 Library ......... : yesBuilding Ruby Library ........ : noBuilding Rust Library ........ : no
如果編譯沒問題,那么就可以查看一下版本thrift -version
,如果能顯示,那么就ok。
安裝完后要搞一個小程序測試一下。thrift的使用是從.thirft文件開始的。
安裝完后,自帶的tutorial就有小例子,但感覺不夠直觀,來copy一個試試效果。
step 1 編寫thrift文件 yang.thrift
struct Student{1: i32 sno,2: string sname,3: bool ssex,4: i16 sage,}service Serv{ void put(1: Student s), i32 icall(1: Student s), string scall(1: Student s), /* string& srcall(1: Student s), ----------------------------- -thrift -r --gen cpp student.thrift -error: - [ERROR:/root/test/thrift/student.thrift:12] (last token was '&') - syntax error - [FAILURE:/root/test/thrift/student.thrift:12] Parser error during include pass. ----------------------------- */ Student stcall(1: Student s),}
step 2 生成源代碼
比如要生成c++代碼,就可以 thrift -r --gen cpp yang.thrift
。這樣就能在目錄下生成一個gen-cpp目錄,里面生成了多個文件,其中的Serv_server.skeleton.cpp
就是服務器端main函數入口文件。--gen后指定生成的語言,yang.thrift就是編寫的thrift魔板。
step 3 編寫客戶端代碼
新聞熱點
疑難解答