前言:thrift的版本問(wèn)題比較麻煩,高版本的在c++上需要c++ 11的支持,而低版本則可能在go的支持上有問(wèn)題,測(cè)試發(fā)現(xiàn)thrift 9.0以上需要c++ 11,而thrift 8.0則在go的支持上可能會(huì)存在問(wèn)題。使用的發(fā)行版本是CentOS 6.5,GCC 4.4.7。最終選擇使用thrift-0.8.0來(lái)編譯安裝。
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 #這一步配置可以選擇支持的語(yǔ)言,如Python,c++,go等。可以./configure --help來(lái)查看可配置項(xiàng)。如./configure --without-gomake && make install
注意:在執(zhí)行完configure后,會(huì)顯示實(shí)際上支持的庫(kù),比如
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
如果編譯沒(méi)問(wèn)題,那么就可以查看一下版本thrift -version
,如果能顯示,那么就ok。
安裝完后要搞一個(gè)小程序測(cè)試一下。thrift的使用是從.thirft文件開(kāi)始的。
安裝完后,自帶的tutorial就有小例子,但感覺(jué)不夠直觀,來(lái)copy一個(gè)試試效果。
step 1 編寫(xiě)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
。這樣就能在目錄下生成一個(gè)gen-cpp目錄,里面生成了多個(gè)文件,其中的Serv_server.skeleton.cpp
就是服務(wù)器端main函數(shù)入口文件。--gen后指定生成的語(yǔ)言,yang.thrift就是編寫(xiě)的thrift魔板。
step 3 編寫(xiě)客戶(hù)端代碼
新聞熱點(diǎn)
疑難解答
圖片精選