編譯thrift和使用
我使用的版本是thrift-0.6.1
解壓以后,先看readme,其中說編譯環境參考http://wiki.apache.org/thrift/ThriftRequirements,打開看:
Language requirements
以C++為例,先安裝Boost和libevent,zlib是一個支持壓縮的,以后再說,按照網站的指示安裝就可以了,
- C++
- Boost 1.33.1+
- libevent (optional, to build the nonblocking server)
- zlib (optional)
- Java
- Java 1.5+
- Apache Ant
- Apache Ivy (recommended)
- Apache Commons Lang (recommended)
- SLF4J
- C#: Mono 1.2.4+ (and pkg-config to detect it) or Visual Studio 2005+
- Python 2.4+ (including header files for extension modules)
- PHP 5.0+ (optionally including header files for extension modules)
- Ruby 1.8+ (including header files for extension modules)
- Erlang R12 (R11 works but not recommended)
- Perl 5
- Bit::Vector
- Class::Accessor
我編譯thrift使用的是:
./configure --prefix=/usr/local/ --disable-static --with-libevent=/usr/local
沒有加--with-boost=/usr/local,因為我把boost放在/usr/include下了,所以能夠找到
因為我公司服務器是RHEL4,版本相對較老,所以出現了很多問題:
-。找不到BOOST
因我我早先已經在其它地方安裝了boost,所以
#ln -s <我的boost root path>/boost /usr/include/boost
二。undefined reference to `_Unwind_Resume'
因為編譯器較老,所以需要加上-Wl,-Bdynamic -lgcc_s 這個參數就可以了,一開始我只是在thrift的根目錄的Makefile文件中的相關FLAG標記中加入了,但還是報相同錯誤,后仔細查看了錯誤信息,發現得在<thrift>/compiler/cpp和<thrift>/lib/cpp中的Makefile中加入才行
三、在rhel4.8上安裝thrift.0.8
由于gcc32無法編譯0.8,所以要執行
# ./configure CXX=g++4
就可以了,(系統上得安裝gcc4相關的rpm),
如果不這樣,直接改MakeFile中CXX=g++4的話,執行過程中會報一個錯:
unsupported hardcode properties
導致無法繼續執行下去的。
使用java時,要編譯thrift的java包
cd thrift-0.6.0/lib/java
ant
編譯完成以后會在當前目錄下出現一個libthrift.jar
把libthrift.jar拷貝到/usr/local/lib下(這個位置是因為在thrift的tutor中,java sample code的build.xml中指明了要在/usr/local/lib找到這個jar文件)。
然后把/usr/local/lib/libthrift.jar也加入到CLASSPATH中。
三。no class template named `tr1' in `std'
還是因為編譯器老的原因,只有在gcc 4.3以后才加入了tr1,所以只能換成boost中的tr1,具體在頭文件中加入
在<thrift>/lib/cpp/src/concurrency/ThreadManager.h 中改為
#include <boost/tr1/memory.hpp>
#include <boost/tr1/functional.hpp>
就行了
////////////////////////////////////
在MacOS中使用參考:
http://wiki.apache.org/thrift/ThriftUsageObjectiveC
/////////////////////////////////////////////////////////////
數據類型
??? ?* Base Types:基本類型 bool,byte,i16,i32,i64,double,string(UTF-8 編碼)
??? ?* Struct:結構體類型
??? ?* Container:容器類型,即List、Set、Map
??? ?* Exception:異常類型
??? ?* Service: 定義對象的接口,和一系列方法一個server只允許定義一個接口服務。這樣的話多個接口需要多個server。這樣會帶來資源的浪費。同意通過繼承接口的方式。
協議
? Thrift可以讓你選擇客戶端與服務端之間傳輸通信協議的類別,在傳輸協議上總體上劃分為文本(text)和二進制(binary)傳輸協議, 為節約帶寬,提供傳輸效率,一般情況下使用二進制類型的傳輸協議為多數,但有時會還是會使用基于文本類型的協議,這需要根據項目/產品中的實際需求:
??? * TBinaryProtocol – 二進制編碼格式進行數據傳輸。
??? * TCompactProtocol – 這種協議非常有效的,使用Variable-Length Quantity (VLQ) 編碼對數據進行壓縮。
??? * TJSONProtocol – 使用JSON的數據編碼協議進行數據傳輸。
??? * TSimpleJSONProtocol – 這種節約只提供JSON只寫的協議,適用于通過腳本語言解析
??? * TDebugProtocol – 在開發的過程中幫助開發人員調試用的,以文本的形式展現方便閱讀。傳輸層
服務端類型
??? * TSocket- 使用堵塞式I/O進行傳輸,也是最常見的模式。
??? * TFramedTransport- 使用非阻塞方式,按塊的大小,進行傳輸,類似于Java中的NIO。
??? * TFileTransport- 顧名思義按照文件的方式進程傳輸,雖然這種方式不提供Java的實現,但是實現起來非常簡單。
??? * TMemoryTransport- 使用內存I/O,就好比Java中的ByteArrayOutputStream實現。
??? * TZlibTransport- 使用執行zlib壓縮,不提供Java的實現。
??? * TSimpleServer -? 單線程服務器端使用標準的堵塞式I/O。
??? * TThreadPoolServer -? 多線程服務器端使用標準的堵塞式I/O。
??? * TNonblockingServer – 多線程服務器端使用非堵塞式I/O,并且實現了Java中的NIO通道。
當服務器端使用socket協議時,可以用simple|thread-pool|threaded|nonblocking等方式運行,從而獲得更好的性能。
基本使用
thrift的類型有如下幾種:
/**
* The first thing to know about are types. The available types in Thrift are:
*
* bool??????? Boolean, one byte
* byte??????? Signed byte
* i16???????? Signed 16-bit integer
* i32???????? Signed 32-bit integer
* i64???????? Signed 64-bit integer
* double????? 64-bit floating point value
* string????? String
* binary????? Blob (byte array)
* map<t1,t2> Map from one type to another
* list<t1>??? Ordered list of one type //對應C++中的vector<t1>
* set<t1>???? Set of unique elements of one type
*
* Did you also notice that Thrift supports C style comments?
*/
例子
編寫角本service.thrift
/**
* This Thrift file can be included by other Thrift files that want to share
* these definitions.
*/
namespace cpp shared
//namespace java shared
//namespace perl shared
struct SharedStruct {
1: i32 key
2: string value
}//定義一個結構體
//定義一個服務
service SharedService {
SharedStruct getStruct(1: i32 key)
}
使用下面的語句,生成cpp骨架文件
thrift –r –gen cpp service.thrift
(生成php骨架文件: # thrift –r –gen php service.thrift )
生成7個文件,分別是由service.thrift腳本定義的類型文件四個,兩個.h文件 (service_constants.h,service_types.h),兩個對應的.cpp文件 (service_constants.cpp,service_types.cpp)。service_types對應的文件中,定義了對應的由 service.thrift腳本定義的類型。例如struct SharedStruct對應到一個類。另外三個文件分別是由service.thrift腳本中所定義的服務相關的文件,分別是 SharedService .h,SharedService .cpp,以及SharedService_server.skeleton.cpp 骨架文件,我們只需要修改SharedService_server.skeleton.cpp 骨架文件中相關的接口部分的邏輯,即可生成對應的服務。
///////////////////////////
進入cpp目錄下,執行make命令,如果編譯出錯,第一個錯誤是/usr/local/include/thrift/protocol/TBinaryProtocol.tcc:147:35: error: there are no arguments to ‘htons’ that depend on a template parameter, so a declaration of ‘htons’ must be available則修改Makefile,加上編譯選項 -DHAVE_NETINET_IN_Hserver: CppServer.cpp? ? ? ? g++?-DHAVE_NETINET_IN_H?-o CppServer -I${THRIFT_DIR} -I${BOOST_DIR} ?-I../gen-cpp -L${LIB_DIR} -lthrift CppServer.cpp ${GEN_SRC}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
