Thrift是Apache的一個開源的跨語言服務開發框架,它提供了一個代碼生成引擎來構建服務,支持C++,Java,Python,PHP,Ruby,Erlang,Perl,Haskell,C#,Cocoa,JavaScript,Node.js,Smalltalk,OCaml,Delphi等多種編程語言。
一般來說,使用Thrift來開發應用程序,主要建立在兩種場景下:
-
第一,在我們開發過程中,一個比較大的項目需要多個團隊進行協作,而每個團隊的成員在編程技術方面的技能可能不一定相同,爲了實現這種跨語言的開發氛圍,使用Thrift來構建服務
-
第二,企業之間合作,在業務上不可避免出現跨語言的編程環境,使用Thrift可以達到類似Web Services的跨平臺的特性
安裝配置Thrift
Thrift的編譯器使用C++編寫的,在安裝編譯器之前,首先應該保證操作系統基本環境支持C++的編譯,安裝相關依賴的軟件包,如下所示
1 |
sudo yum install automake
libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl-devel |
下載Thrift的軟件包,並解壓縮:
2 |
tar -xvzf
thrift-0.9.0. tar .gz |
配置、編譯、安裝Thrift,如下所示:
如果在配置的時候總是報如下錯誤:
可能是沒有安裝openssl-devel,可以安裝這個軟件包,或者,如果已經安裝了這個軟件包,可以執行以下命令:
1
sudo yum update
如果需要的話,可以運行測試用例:
安裝成功以後,可以輸入如下命令行進行驗證:
02 |
Usage:
thrift [options] file |
04 |
-version
Print the compiler version |
05 |
-o
dir Set the output directory for gen-* packages |
06 |
(default:
current directory) |
07 |
-out
dir Set the ouput location for generated files. |
08 |
(no
gen-* folder will be created) |
09 |
-I
dir Add a directory to the list of directories |
10 |
searched
for include directives |
11 |
-nowarn
Suppress all compiler warnings (BAD!) |
12 |
-strict
Strict compiler warnings on |
13 |
-v[erbose]
Verbose mode |
14 |
-r[ecurse]
Also generate included files |
15 |
-debug
Parse debug trace to stdout |
16 |
--allow-neg-keys
Allow negative field keys (Used to preserve protocol |
17 |
compatibility
with older .thrift files) |
18 |
--allow-64bit-consts
Do not print warnings about using 64-bit constants |
19 |
--gen
STR Generate code with a dynamically-registered generator. |
20 |
STR
has the form language[:key1=val1[,key2,[key3=val3]]]. |
21 |
Keys
and values are options passed to the generator. |
22 |
Many
options will not require values. |
使用Thrift
我們直接使用Thrift官網提供的簡單例子,驗證一下。Thrift定義文件爲user.thrift,如下所示:
struct UserProfile {
1: i32 uid,
2: string name,
3: string blurb
}
service UserStorage {
void store(1: UserProfile user),
UserProfile retrieve(1: i32 uid)
}
然後,使用Thrift編譯器來進行編譯,生成Java、C++、PHP、Perl和C#代碼,執行命令:
01 |
[hadoop@master
thrift]$ ls |
03 |
[hadoop@master
thrift]$ thrift --gen java user.thrift |
04 |
[hadoop@master
thrift]$ thrift --gen cpp user.thrift |
05 |
[hadoop@master
thrift]$ thrift --gen php user.thrift |
06 |
[hadoop@master
thrift]$ thrift --gen perl user.thrift |
07 |
[hadoop@master
thrift]$ thrift --gen csharp user.thrift |
08 |
[hadoop@master
thrift]$ thrift --gen py user.thrift |
09 |
[hadoop@master
thrift]$ ls |
10 |
gen-cpp
gen-csharp gen-java gen-perl gen-php gen-py user.thrift |
可以看到,生成了對應的gen-的目錄,每個目錄 下面都是對應的代碼,下面看下,生成的代碼:
如果想要生成其他編程語言的代碼,可以參考Thrift命令支持的語言,如下所示:
01 |
Available
generators (and options): |
03 |
bindable:
Add [bindable] metadata to all the struct classes. |
04 |
c_glib
(C, using GLib): |
06 |
log_unexpected:
Log every time an unexpected field ID or type is encountered. |
08 |
cob_style:
Generate "Continuation OBject"-style classes. |
10 |
Omit
calls to completion__() in CobClient class. |
11 |
templates:
Generate templatized reader/writer methods. |
12 |
pure_enums:
Generate pure enums instead of wrapper classes. |
13 |
dense:
Generate type specifications for the dense protocol. |
14 |
include_prefix:
Use full include paths in generated files. |
16 |
async:
Adds Async CTP support. |
17 |
wcf:
Adds bindings for WCF to generated classes. |
18 |
serial:
Add serialization support to generated classes. |
21 |
ansistr_binary:
Use AnsiString as binary properties. |
27 |
beans:
Members will be private, and setter methods will return void. |
28 |
private-members:
Members will be private, but setter methods will return 'this' like usual. |
29 |
nocamel:
Do not use CamelCase field accessors with beans. |
30 |
hashcode:
Generate quality hashCode methods. |
31 |
android_legacy:
Do not use java.io.IOException(throwable) (available for Android 2.3 and above). |
32 |
java5:
Generate Java 1.5 compliant code (includes android_legacy flag). |
35 |
jquery:
Generate jQuery compatible code. |
36 |
node:
Generate node.js compatible code. |
40 |
inlined:
Generate PHP inlined files |
41 |
server:
Generate PHP server stubs |
42 |
oop:
Generate PHP with object oriented subclasses |
43 |
rest:
Generate PHP REST processors |
45 |
new_style:
Generate new-style classes. |
46 |
twisted:
Generate Twisted-friendly RPC services. |
47 |
utf8strings:
Encode/decode strings using utf8 in the generated code. |
48 |
slots:
Generate code using slots for instance members. |
49 |
dynamic:
Generate dynamic code, less code generated but slower. |
50 |
dynbase=CLS
Derive generated classes from class CLS instead of TBase. |
51 |
dynexc=CLS
Derive generated exceptions from CLS instead of TExceptionBase. |
52 |
dynimport='from
foo.bar import CLS' |
53 |
Add
an import line to generated code to find the dynbase class. |
55 |
rubygems:
Add a "require 'rubygems'" line to the top of each generated file. |