Apache Thrift使用總結

來源:互聯網
上載者:User

標籤:

使用感受

之前對Thrift的理解有點不準確,使用之後發現Thrift比想象中的要簡單得多。

Thrift做的事情就是跨語言的分布式RPC,通過編寫.thrift檔案聲明介面類和方法,client調用定義的方法,Server端實現定義的介面。儘管的確RPC是須要網路請求,但不像Netty這樣的NIO網路編程庫(還要關注許多傳輸資料中的細節,比方資料怎樣序列化、怎樣在位元組數組裡建立結構、怎樣在兩端解析位元組數組、怎樣處理Handler裡的事件狀態、怎樣把多個Handler按順序串起來),Thrift掩蓋了傳輸資料這件事情,開發人員使用的時候就是純純的RPC的使用感受。


基本使用

Thrift使用起來差點兒沒有不論什麼門檻,能夠看這篇HelloWorld的文章,儘管有點老,可是看完之後基本使用起來沒有不論什麼障礙了。

官方給出的這個範例更加全面些,全面在.thrift檔案中能夠聲明的東西列的更全些。

以下看看兩個.thrift的定義:

shared.thrift

/** * This Thrift file can be included by other Thrift files that want to share * these definitions. */namespace java com.baidu.mordor.sink.servicestruct SharedStruct {  1: i32 key  2: string value}service SharedService {  SharedStruct getStruct(1: i32 key)}

tutorial.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 *  set<t1>     Set of unique elements of one type * * Did you also notice that Thrift supports C style comments? */// Just in case you were wondering... yes. We support simple C comments too./** * Thrift files can reference other Thrift files to include common struct * and service definitions. These are found using the current path, or by * searching relative to any paths specified with the -I compiler flag. * * Included objects are accessed using the name of the .thrift file as a * prefix. i.e. shared.SharedObject */include "shared.thrift"/** * Thrift files can namespace, package, or prefix their output in various * target languages. */namespace java com.baidu.mordor.sink.service/** * Thrift lets you do typedefs to get pretty names for your types. Standard * C style here. */typedef i32 MyInteger/** * Thrift also lets you define constants for use across languages. Complex * types and structs are specified using JSON notation. */const i32 INT32CONSTANT = 9853const map<string,string> MAPCONSTANT = {‘hello‘:‘world‘, ‘goodnight‘:‘moon‘}/** * You can define enums, which are just 32 bit integers. Values are optional * and start at 1 if not supplied, C style again. */enum Operation {  ADD = 1,  SUBTRACT = 2,  MULTIPLY = 3,  DIVIDE = 4}/** * Structs are the basic complex data structures. They are comprised of fields * which each have an integer identifier, a type, a symbolic name, and an * optional default value. * * Fields can be declared "optional", which ensures they will not be included * in the serialized output if they aren‘t set.  Note that this requires some * manual management in some languages. */struct Work {  1: i32 num1 = 0,  2: i32 num2,  3: Operation op,  4: optional string comment,}/** * Structs can also be exceptions, if they are nasty. */exception InvalidOperation {  1: i32 what,  2: string why}/** * Ahh, now onto the cool part, defining a service. Services just need a name * and can optionally inherit from another service using the extends keyword. */service Calculator extends shared.SharedService {   void ping(),   i32 add(1:i32 num1, 2:i32 num2),   i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),   /**    * This method has a oneway modifier. That means the client only makes    * a request and does not listen for any response at all. Oneway methods    * must be void.    */   oneway void zip()}

Thrift通過IDL(介面定義語言),在.thrift檔案中聲明介面類和方法,聲明struct結構、const、Exception等,還能夠include別的.thrift檔案,這套文法與C很類似。通過編寫IDL和generate代碼,做到了不同語言之間的RPC,client實現介面類和使用結構類的時候很easy好用。
上面範例的代碼能夠從官方下載到,能夠放到本地看一下他的使用,很easy。


Thrift重要組件

Thrift API裡三個重要組成部分:Protocal,Transport,Server。


Protocal定義了訊息怎樣序列化。常見的是TBinaryProtocol,TJSONProtocol,TCompactProtocol。


Transport定義了訊息在client和服務端怎樣通訊。常見的是TSocket,TFramedTransport,TNonblockingTransport等。


Server從transport端接收序列化後的訊息,依據protocal還原序列化回來,然後調用使用者實現的訊息handler(介面實作類別),最後把返回的資料序列化後再傳回給client。常見的TServer為TSimpleServer,THsHaServer,TThreadPoolServer,TNonBlockingServer,TThreadedSelectorServer。以下會詳細介紹各個Server的特點,開發人員須要選擇適合自己情境的一套 Server+相應的Transport+相應的Protocol。


TServer說明Thrift實現的幾種不同的TServer。對於Java而言,版本號碼按0.9.0為準:

TSimpleServer在sever端僅僅有一個I/O堵塞的單線程,每次僅僅接受並服務一個client,適合測試使用,不能用於線上服務。


TNonblockingServer改動了TSimpleServer裡堵塞的缺點,藉助NIO裡的Selector實現非堵塞I/O,同意多個client串連而且client能夠使用select()選擇。可是處理訊息和select()的是同一個線程,當有大量client串連的時候,效能是不理想的。


 THsHaServer(半同步半非同步server)在以上基礎上,使用一個單獨線程來處理網路I/O,一個worker線程池來處理訊息。優點是僅僅要有空暇worker線程,訊息能夠被及時、平行處理,輸送量會大一些。


TThreadedSelectorServer,與THsHaServer的差別是處理網路I/O也是多線程了,它維護兩個線程池,一個負責網路I/O,一個負責資料處理。優點是當網路I/O是瓶頸的情況下,效能比THsHaServer更好。


TThreadPoolServer有一個專用的線程來接收connections,串連被建立後,會從ThreadPoolExecutor裡取一個背景工作執行緒來負責這次串連,直到串連斷開後線程回到線程池裡,且線程池大小可配。也就是說,並發性的大小可依據伺服器設定,假設不介意開許多線程的話,TThreadPoolServer是個還不錯的選擇。


全文完 :)


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.