標籤:pytho esc question 服務 www 序列 types.h clu types
Apache Thrift
Thrift是一個跨語言的服務部署架構,最初由Facebook於2007年開發,2008年進入Apache開源項目。Thrift通過IDL(Interface Definition Language,介面定義語言)來定義RPC(Remote Procedure Call,遠端程序呼叫)的介面和資料類型,然後通過thrift編譯器產生不同語言的代碼(目前支援C++,Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk和OCaml),並由產生的程式碼負責RPC協議層和傳輸層的實現。
- Apache Thrift
- Apache Thrift Tutorial
- Apache Thrift interface description language
- Apache Thrift Whitepaper
- Thrift入門教程
- thrift 的原理和使用
- 誰能用通俗的語言解釋一下什麼是 RPC 架構?
過程:
- Call ID 映射
- 序列化和還原序列化
- 網路傳輸
何謂RPC遠端程序呼叫?
RPC(Remote Procedure Call)遠端程序呼叫,它是一種通過網路從遠端電腦程式上請求服務,而不需要瞭解底層網路技術的協議。
樣本WhatTime.thrift檔案
namespace cpp ldstruct Example{ 1:i32 number=10, 2:i64 bigNumber, 3:double decimals, 4:string name="thrifty"}service TimeInterface{ i32 GetTime(), void SetTime()}
generate the source from a thrift file
thrift [-r] --gen <language> <Thrift filename># -r 可選
client.cpp
#include "TimeInterface.h"#include <thrift/transport/TSocket.h>#include <thrift/protocol/TBinaryProtocol.h>#include <thrift/transport/TBufferTransports.h>using namespace ::apache::thrift;using namespace ::apache::thrift::protocol;using namespace ::apache::thrift::transport;using boost::shared_ptr;using namespace ::ld;int main(int argc, char **argv) { int port = 9090; boost::shared_ptr<TSocket> socket(new TSocket("localhost", port)); //注意此處的ip和連接埠 boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket)); boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); transport->open(); // 我們的代碼寫在這裡 TimeInterfaceClient client(protocol); // 用戶端介面,聲明於TimeInterface.h中。 client.SetTime(); client.GetTime(); transport->close(); return 0;}
makefile
all : server clientserver : TimeInterface.cpp TimeInterface.h TimeInterface_server.skeleton.cpp WhatTime_constants.cpp WhatTime_constants.h WhatTime_types.cpp WhatTime_types.h g++ -std=c++11 -g -Ithrift -lthrift TimeInterface.cpp TimeInterface.h TimeInterface_server.skeleton.cpp WhatTime_constants.cpp WhatTime_constants.h WhatTime_types.cpp WhatTime_types.h -o serverclient: TimeInterface.cpp TimeInterface.h WhatTime_constants.cpp WhatTime_constants.h WhatTime_types.cpp WhatTime_types.h client.cpp g++ -std=c++11 -g -Ithrift -lthrift TimeInterface.cpp TimeInterface.h WhatTime_constants.cpp WhatTime_constants.h WhatTime_types.cpp WhatTime_types.h client.cpp -o client
Apache Thrift 的使用