CentOs安裝Thrift

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,如下所示:

1 sudo ./configure
2 sudo make
3 sudo make install

如果在配置的時候總是報如下錯誤:

可能是沒有安裝openssl-devel,可以安裝這個軟件包,或者,如果已經安裝了這個軟件包,可以執行以下命令:
1
sudo yum update

如果需要的話,可以運行測試用例:

1 sudo make check

安裝成功以後,可以輸入如下命令行進行驗證:

01 thrift --help
02 Usage: thrift [options] file
03 Options:
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
02 user.thrift
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-的目錄,每個目錄 下面都是對應的代碼,下面看下,生成的代碼:

  • Java代碼

    生成2個Java文件:

    1 [hadoop@master thrift]$ cd gen-java/
    2 [hadoop@master gen-java]$ ls
    3 UserProfile.java  UserStorage.java

    具體代碼可以查看相應的代碼文件。

  • C++代碼

    生成多個C++文件:

    1 [hadoop@master thrift]$ cd gen-cpp/
    2 [hadoop@master gen-cpp]$ ls
    3 user_constants.cpp  UserStorage.cpp  UserStorage_server.skeleton.cpp  user_types.h
    4 user_constants.h    UserStorage.h    user_types.cpp

    具體代碼可以查看相應的代碼文件。

  • PHP代碼

    生成2個文件:

    1 [hadoop@master thrift]$ cd gen-php/
    2 [hadoop@master gen-php]$ ls
    3 Types.php  UserStorage.php

    具體代碼可以查看相應的代碼文件。

  • Perl代碼

    生成3個文件:

    1 [hadoop@master thrift]$ cd gen-perl/
    2 [hadoop@master gen-perl]$ ls
    3 Constants.pm  Types.pm  UserStorage.pm

    具體代碼可以查看相應的代碼文件

  • C#代碼

    生成2個文件:

    1 [hadoop@master thrift]$ cd gen-csharp/
    2 [hadoop@master gen-csharp]$ ls
    3 UserProfile.cs  UserStorage.cs

    具體代碼可以查看相應的代碼文件。

  • Python代碼

    生成一個__init__.py文件,和一個目錄user:

    1 [hadoop@master thrift]$ cd gen-py/
    2 [hadoop@master gen-py]$ ls -R
    3 .:
    4 __init__.py  user
    5  
    6 ./user:
    7 constants.py  __init__.py  ttypes.py  UserStorage.py  UserStorage-remote

如果想要生成其他編程語言的代碼,可以參考Thrift命令支持的語言,如下所示:

01 Available generators (and options):
02   as3 (AS3):
03     bindable:          Add [bindable] metadata to all the struct classes.
04   c_glib (C, using GLib):
05   cocoa (Cocoa):
06     log_unexpected:  Log every time an unexpected field ID or type is encountered.
07   cpp (C++):
08     cob_style:       Generate "Continuation OBject"-style classes.
09     no_client_completion:
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.
15   csharp (C#):
16     async:           Adds Async CTP support.
17     wcf:             Adds bindings for WCF to generated classes.
18     serial:          Add serialization support to generated classes.
19   d (D):
20   delphi (delphi):
21     ansistr_binary:  Use AnsiString as binary properties.
22   erl (Erlang):
23   go (Go):
24   hs (Haskell):
25   html (HTML):
26   java (Java):
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).
33   javame (Java ME):
34   js (Javascript):
35     jquery:          Generate jQuery compatible code.
36     node:            Generate node.js compatible code.
37   ocaml (OCaml):
38   perl (Perl):
39   php (PHP):
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
44   py (Python):
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.
54   rb (Ruby):
55     rubygems:        Add a "require 'rubygems'" line to the top of each generated file.
56   st (Smalltalk):
57   xsd (XSD):
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章