Apache Thrift 的使用

來源:互聯網
上載者:User

標籤: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 的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.