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++的編譯,安裝相關依賴的軟件包,以下所示
sudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl-devel
下載Thrift的軟件包,並解壓縮:
tar -xvzf thrift-0.9.0.tar.gz
配置、編譯、安裝Thrift,以下所示:
sudo ./configure
sudo make
sudo make install
若是在配置的時候老是報以下錯誤:
多是沒有安裝openssl-devel,能夠安裝這個軟件包,或者,若是已經安裝了這個軟件包,能夠執行如下命令:
sudo yum update
若是須要的話,能夠運行測試用例:
sudo make check
安裝成功之後,能夠輸入以下命令行進行驗證:
thrift --help
Usage: thrift [options] file
Options:
-version Print the compiler version
-o dir Set the output directory for gen-* packages
(default: current directory)
-out dir Set the ouput location for generated files.
(no gen-* folder will be created)
-I dir Add a directory to the list of directories
searched for include directives
-nowarn Suppress all compiler warnings (BAD!)
-strict Strict compiler warnings on
-v[erbose] Verbose mode
-r[ecurse] Also generate included files
-debug Parse debug trace to stdout
--allow-neg-keys Allow negative field keys (Used to preserve protocol
compatibility with older .thrift files)
--allow-64bit-consts Do not print warnings about using 64-bit constants
--gen STR Generate code with a dynamically-registered generator.
STR has the form language[:key1=val1[,key2,[key3=val3]]].
Keys and values are options passed to the generator.
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#代碼,執行命令:
user.thrift
gen-cpp gen-csharp gen-java gen-perl gen-php gen-py user.thrift
能夠看到,生成了對應的gen-的目錄,每一個目錄 下面都是對應的代碼,下面看下,生成的代碼:
Java代碼
生成2個Java文件:
UserProfile.java UserStorage.java
具體代碼能夠查看相應的代碼文件。
C++代碼
生成多個C++文件:
user_constants.cpp UserStorage.cpp UserStorage_server.skeleton.cpp user_types.h
user_constants.h UserStorage.h user_types.cpp
具體代碼能夠查看相應的代碼文件。
PHP代碼
生成2個文件:
Types.php UserStorage.php
具體代碼能夠查看相應的代碼文件。
Perl代碼
生成3個文件:
Constants.pm Types.pm UserStorage.pm
具體代碼能夠查看相應的代碼文件
C#代碼
生成2個文件:
UserProfile.cs UserStorage.cs
具體代碼能夠查看相應的代碼文件。
Python代碼
生成一個__init__.py文件,和一個目錄user:
.:
__init__.py user
./user:
constants.py __init__.py ttypes.py UserStorage.py UserStorage-remote
若是想要生成其餘編程語言的代碼,能夠參考Thrift命令支持的語言,以下所示:
Available generators (and options):
as3 (AS3):
bindable: Add [bindable] metadata to all the struct classes.
c_glib (C, using GLib):
cocoa (Cocoa):
log_unexpected: Log every time an unexpected field ID or type is encountered.
cpp (C++):
cob_style: Generate "Continuation OBject"-style classes.
no_client_completion:
Omit calls to completion__() in CobClient class.
templates: Generate templatized reader/writer methods.
pure_enums: Generate pure enums instead of wrapper classes.
dense: Generate type specifications for the dense protocol.
include_prefix: Use full include paths in generated files.
csharp (C#):
async: Adds Async CTP support.
wcf: Adds bindings for WCF to generated classes.
serial: Add serialization support to generated classes.
d (D):
delphi (delphi):
ansistr_binary: Use AnsiString as binary properties.
erl (Erlang):
go (Go):
hs (Haskell):
html (HTML):
java (Java):
beans: Members will be private, and setter methods will return void.
private-members: Members will be private, but setter methods will return 'this' like usual.
nocamel: Do not use CamelCase field accessors with beans.
hashcode: Generate quality hashCode methods.
android_legacy: Do not use java.io.IOException(throwable) (available for Android 2.3 and above).
java5: Generate Java 1.5 compliant code (includes android_legacy flag).
javame (Java ME):
js (Javascript):
jquery: Generate jQuery compatible code.
node: Generate node.js compatible code.
ocaml (OCaml):
perl (Perl):
php (PHP):
inlined: Generate PHP inlined files
server: Generate PHP server stubs
oop: Generate PHP with object oriented subclasses
rest: Generate PHP REST processors
py (Python):
new_style: Generate new-style classes.
twisted: Generate Twisted-friendly RPC services.
utf8strings: Encode/decode strings using utf8 in the generated code.
slots: Generate code using slots for instance members.
dynamic: Generate dynamic code, less code generated but slower.
dynbase=CLS Derive generated classes from class CLS instead of TBase.
dynexc=CLS Derive generated exceptions from CLS instead of TExceptionBase.
dynimport='from foo.bar import CLS'
Add an import line to generated code to find the dynbase class.
rb (Ruby):
rubygems: Add a "require 'rubygems'" line to the top of each generated file.
st (Smalltalk):
xsd (XSD):
參考連接