一、About? thrift???
二、什么是thrift,怎么工作?
?
三、Thrift? IDL?
四、Thrift?? Demo?
五、Thrift 協(xié)議棧 以及各層的使用(java 為例)
?
六、與protocolbuffer的區(qū)別
?
一、About? thrift???
???????? thrift是一種可伸縮的跨語(yǔ)言服務(wù)的發(fā)展軟件框架。它結(jié)合了功能強(qiáng)大的軟件堆棧的代碼生成引擎,以建設(shè)服務(wù),工作效率和無(wú)縫地與C + +,C#,Java,Python和PHP和Ruby結(jié)合。thrift是facebook開發(fā)的,我們現(xiàn)在把它作為開源軟件使用。thrift允許你定義一個(gè)簡(jiǎn)單的定義文件中的數(shù)據(jù)類型和服務(wù)接口。以作為輸入文件,編譯器生成代碼用來(lái)方便地生成RPC客戶端和服務(wù)器通信的無(wú)縫跨編程語(yǔ)言(來(lái)自百度百科)。????
? >>>最初由facebook開發(fā)用做系統(tǒng)內(nèi)個(gè)語(yǔ)言之間的RPC通信 。?
? >>>2007年由facebook貢獻(xiàn)到apache基金 ,現(xiàn)在是apache下的opensource之一 。?
? >>>支持多種語(yǔ)言之間的RPC方式的通信:php語(yǔ)言client可以構(gòu)造一個(gè)對(duì)象,調(diào)用相應(yīng)的服務(wù)方法來(lái)調(diào)用java語(yǔ)言的服務(wù) ,跨越語(yǔ)言的C/S?? rpc? 調(diào)用 。?
二、什么是thrift,怎么工作?
?
java? rmi的例子,代碼見(jiàn)附件,建立一個(gè)java rmi的流程? :?
? >>>定義一個(gè)服務(wù)調(diào)用接口 。?
? >>>server端:接口實(shí)現(xiàn)---impl的實(shí)例---注冊(cè)該服務(wù)實(shí)現(xiàn)(端口)---啟動(dòng)服務(wù)。?
? >>>client端:通過(guò)ip、端口、服務(wù)名,得到服務(wù),通過(guò)接口來(lái)調(diào)用 。?
? >>>rmi數(shù)據(jù)傳輸方式:java對(duì)象序列化 。?
Thrift? 服務(wù)??
? >>>例同rmi ,需要定義通信接口、實(shí)現(xiàn)、注冊(cè)服務(wù)、綁定端口……?
? >>>如何多種語(yǔ)言之間通信? ??
? >>>數(shù)據(jù)傳輸走socket(多種語(yǔ)言均支持),數(shù)據(jù)再以特定的格式(String ),發(fā)送,接收方語(yǔ)言解析?? 。?
??????? Object --->? String --->? Object? 。?
??? 問(wèn)題:編碼、解析完全需要自己做 ,復(fù)雜的數(shù)據(jù)結(jié)構(gòu)會(huì)編碼困難 .?
Thrift? 服務(wù) :thrift的中間編碼層?
? >>>java? Object ---> Thrift? Object ---> php? Object???
? >>> 定義thrift的文件 ,由thrift文件(IDL)生成 雙方語(yǔ)言的接口、model ,在生成的model以及接口中會(huì)有解碼編碼的代碼 。?
? >>>thrift?? 文件例子?
???? thrift-0.7.0.exe?? -r?? -gen? java??? TestThrift.thrift??? 生成java 代碼?
???? thrift-0.7.0.exe?? -r?? -gen? php??? TestThrift.thrift??? 生成php代碼?
???? thrift-0.7.0.exe?? -r?? -gen? py?????? TestThrift.thrift??? 生成python代碼?
???? thrift-0.7.0.exe?? -r?? -gen? as3???? TestThrift.thrift??? 生成as3代碼?
???? thrift-0.7.0.exe?? -r?? -gen? cpp???? TestThrift.thrift??? 生成C++代碼?
三、Thrift? IDL?
?????????????????
?????? http://www.cnblogs.com/tianhuilove/archive/2011/09/05/2167669.html?
?????? http://wiki.apache.org/thrift/?
???????????
?????? http://wiki.apache.org/thrift/ThriftTypes?
四、Thrift?? Demo?
Thrift? IDL 文件?
- namespace?java?com.gemantic.analyse.thrift.index??
- ??
- struct??NewsModel{??
- 1:i32?id?;??
- 2:string?title;??
- 3:string?content;??
- 4:string?media_from;??
- 5:string?author;??
- }??
- ??
- service?IndexNewsOperatorServices?{??
- bool?indexNews( 1:NewsModel?indexNews),??
- bool?deleteArtificiallyNews( 1:i32?id?)??
- }??
java? server?
- package?com.gemantic.analyse.thrift.index;??
- ??
- import?java.net.InetSocketAddress;??
- ??
- import?org.apache.thrift.protocol.TBinaryProtocol;??
- import?org.apache.thrift.server.TServer;??
- import?org.apache.thrift.server.TThreadPoolServer;??
- import?org.apache.thrift.server.TThreadPoolServer.Args;??
- import?org.apache.thrift.transport.TServerSocket;??
- import?org.apache.thrift.transport.TServerTransport;??
- import?org.apache.thrift.transport.TTransportFactory;??
- ??
- public? class?ThriftServerTest?{??
- ??
- ???? /**?
- ?????*?@param?args?
- ?????*/??
- ???? public? static? void?main(String[]?args)?{??
- ???????? //?TODO?Auto-generated?method?stub??
- ????????IndexNewsOperatorServices.Processor?processor?=? new?IndexNewsOperatorServices.Processor( new?IndexNewsOperatorServicesImpl());??
- ???????? try{??
- ????????????TServerTransport?serverTransport?=? new?TServerSocket(? new?InetSocketAddress( "0.0.0.0", 9813));??
- ????????????Args?trArgs= new?Args(serverTransport);??
- ????????????trArgs.processor(processor);??
- ???????????? //使用二進(jìn)制來(lái)編碼應(yīng)用層的數(shù)據(jù)??
- ????????????trArgs.protocolFactory( new?TBinaryProtocol.Factory( true,? true));??
- ???????????? //使用普通的socket來(lái)傳輸數(shù)據(jù)??
- ????????????trArgs.transportFactory( new?TTransportFactory());??
- ????????????TServer?server?=? new?TThreadPoolServer(trArgs);??
- ????????????System.out.println( "server?begin?......................");??
- ????????????server.serve();??
- ????????????System.out.println( "---------------------------------------");??
- ????????????server.stop();??
- ????????} catch(Exception?e){??
- ???????????? throw? new?RuntimeException( "index?thrift?server?start?failed!!"+ "/n"+e.getMessage());??
- ????????}??
- ????}??
- ??
- }??
java client?
- package?com.gemantic.analyse.thrift.index;??
- ??
- import?org.apache.thrift.TException;??
- import?org.apache.thrift.protocol.TBinaryProtocol;??
- import?org.apache.thrift.protocol.TProtocol;??
- import?org.apache.thrift.transport.TSocket;??
- import?org.apache.thrift.transport.TTransport;??
- ??
- public? class?ThriftClientTest?{??
- ??
- ???? /**?
- ?????*?@param?args?
- ?????*?@throws?TException??
- ?????*/??
- ???? public? static? void?main(String[]?args)? throws?TException?{??
- ???????? //?TODO?Auto-generated?method?stub??
- ????????TTransport?transport?=? new?TSocket( "10.0.0.41",? 9813);??
- ???????? long?start=System.currentTimeMillis();??
- //??????TTransport?transport?=?new?TSocket("218.11.178.110",9090);??
- ????????TProtocol?protocol?=? new?TBinaryProtocol(transport);??
- ????????IndexNewsOperatorServices.Client?client= new?IndexNewsOperatorServices.Client(protocol);??
- ????????transport.open();??
- ??
- ??????????
- ????????client.deleteArtificiallyNews( 123456);??
- ????????NewsModel?newsModel= new?NewsModel();??
- ????????newsModel.setId( 789456);??
- ????????newsModel.setTitle( "this?from?java?client");??
- ????????newsModel.setContent( " 世界杯比賽前,由于塞爾維亞和黑山突然宣布分裂,國(guó)際足聯(lián)開會(huì)決定剔除塞黑,由世界上球迷最多的國(guó)家頂替,名額恰巧來(lái)到中國(guó)。舉國(guó)上下一片歡騰,中國(guó)足協(xié)決定由“成世鐸”(成龍+閻世鐸)組隊(duì),進(jìn)軍世界杯。");??
- ????????newsModel.setAuthor( "ddc");??
- ????????newsModel.setMedia_from( "新華08");??
- ????????client.indexNews(newsModel);??
- ????????transport.close();??
- ????????System.out.println((System.currentTimeMillis()-start));??
- ????????System.out.println( "client?sucess!");??
- ????}??
- ??
- }??
php client?
- <?php??
- $GLOBALS[ 'THRIFT_ROOT']?=? '/home/tjiang/demo/thrift/lib/php/src';??
- require_once? $GLOBALS[ 'THRIFT_ROOT']. '/Thrift.php';??
- require_once? $GLOBALS[ 'THRIFT_ROOT']. '/protocol/TBinaryProtocol.php';??
- require_once? $GLOBALS[ 'THRIFT_ROOT']. '/transport/TSocket.php';??
- require_once? $GLOBALS[ 'THRIFT_ROOT']. '/transport/THttpClient.php';??
- require_once? $GLOBALS[ 'THRIFT_ROOT']. '/transport/TBufferedTransport.php';??
- include_once? $GLOBALS[ 'THRIFT_ROOT']. '/packages/TestThrift/TestThrift_types.php';??
- include_once? $GLOBALS[ 'THRIFT_ROOT']. '/packages/TestThrift/IndexNewsOperatorServices.php';??
- $data= array(??
- 'id'=> '1',??
- 'title'=> 'demo-標(biāo)題',??
- 'content'=> 'demo-內(nèi)容',??
- 'media_from'=> 'hexun',??
- 'author'=> 'xiaodi667'??
- );??
- $thrif_server_url?=? '10.0.0.41';??
- $transport?=? new?TSocket( $thrif_server_url,?9813);??
- $transport->open();??
- ??
- $protocol?=? new?TBinaryProtocol( $transport);??
- ??
- $client=? new?IndexNewsOperatorServicesClient( $protocol,? $protocol);??
- $obj?=? new?NewsModel( $data);??
- $result?=? $client->indexNews( $obj);??
- ??
- $transport->close();??
- ?>??
python client?
- #!/usr/bin/env?python??
- ??
- #??
- #?Licensed?to?the?Apache?Software?Foundation?(ASF)?under?one??
- #?or?more?contributor?license?agreements.?See?the?NOTICE?file??
- #?distributed?with?this?work?for?additional?information??
- #?regarding?copyright?ownership.?The?ASF?licenses?this?file??
- #?to?you?under?the?Apache?License,?Version?2.0?(the??
- #?"License");?you?may?not?use?this?file?except?in?compliance??
- #?with?the?License.?You?may?obtain?a?copy?of?the?License?at??
- #??
- #???http://www.apache.org/licenses/LICENSE-2.0??
- #??
- #?Unless?required?by?applicable?law?or?agreed?to?in?writing,??
- #?software?distributed?under?the?License?is?distributed?on?an??
- #?"AS?IS"?BASIS,?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY??
- #?KIND,?either?express?or?implied.?See?the?License?for?the??
- #?specific?language?governing?permissions?and?limitations??
- #?under?the?License.??
- #??
- ??
- import?sys??
- ??
- from?TestThrift.ttypes? import?NewsModel??
- from?TestThrift.IndexNewsOperatorServices? import?Client??
- ??
- from?thrift? import?Thrift??
- from?thrift.transport? import?TSocket??
- from?thrift.transport? import?TTransport??
- from?thrift.protocol? import?TBinaryProtocol??
- ??
- try:??
- ??
- ?? #?Make?socket??
- ??transport?=?TSocket.TSocket( '10.0.0.41',? 9813)??
- ??
- ?? #?Buffering?is?critical.?Raw?sockets?are?very?slow??
- ??transport?=?TTransport.TBufferedTransport(transport)??
- ??
- ?? #?Wrap?in?a?protocol??
- ??protocol?=?TBinaryProtocol.TBinaryProtocol(transport)??
- ??
- ?? #?Create?a?client?to?use?the?protocol?encoder??
- ??client?=?Client(protocol)??
- ??
- ?? #?Connect!??
- ??transport.open()??
- ??
- ??client.deleteArtificiallyNews( 123)??
- ????
- ??newsModel=NewsModel()??
- ??newsModel.id= 123456??
- ??newsModel.title= "python?Test"??
- ??newsModel.content= "client?test??come?from?python";??
- ??newsModel.media_from= "xinhua08"??
- ????
- ??client.indexNews(newsModel)??
- ????
- ?? #close??
- ??transport.close()??
- except?Thrift.TException,?tx:??
- ?? print? '%s'?%?(tx.message)??
Csharp client?
- TTransport?transport?=? new?TSocket( "10.0.0.41",?9813);??
- TProtocol?protocol?=? new?TBinaryProtocol(transport);??
- IndexNewsOperatorServices.Client?client?=? new?IndexNewsOperatorServices.Client(protocol);??
- ??
- transport.Open();??
- NewsModel?model?=? new?NewsModel();??
- model.Author?=? "jww";??
- model.Title?=? "title";??
- model.Content?=? "client???Come???From???CSharp";??
- model.Id?=?1;??
- ??
- client.deleteArtificiallyNews(123);??
- Console.WriteLine(client.indexNews(model));??
五、Thrift 協(xié)議棧 以及各層的使用(java 為例)
?
?
1、model?? interface?
?????? 服務(wù)的調(diào)用接口以及接口參數(shù)model、返回值model?
2、Tprotocol??? 協(xié)議層?
???????? 將數(shù)據(jù)(model)編碼 、解碼 。?
3、Ttramsport 傳輸層?
??????? 編碼后的數(shù)據(jù)傳輸(簡(jiǎn)單socket、http)?
5、Tserver?
??????? 服務(wù)的Tserver類型,實(shí)現(xiàn)了幾種rpc調(diào)用(單線程、多線程、非阻塞IO)?
六、與protocolbuffer的區(qū)別
?
http://liuchangit.com/development/346.html?
????????????
http://stackoverflow.com/questions/69316/biggest-differences-of-thrift-vs-protocol-buffers?
區(qū)別:?
1、Another important difference are the languages supported by default.??? protobuf: Java, C++, Python??? Thrift: Java, C++, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, Ocaml?
支持語(yǔ)言不同,thrift支持著更多的語(yǔ)言 。?
2、Thrift supports ‘exceptions 。?
?? thrift支持服務(wù)的異常 。?
3、Protocol Buffers much easier to read 。Protobuf API looks cleaner, though the generated classes are all packed as an inner classes which is not so nice.?
?? Protocol Buffers 在文檔方面比thrift豐富,而且比thrift簡(jiǎn)單 。?
4、Protobuf serialized objects are about 30% smaller then Thrift.?
?? Protocol Buffers在序列化/反序列化、傳輸上性能更優(yōu) 。?
5、RPC is another key difference. Thrift generates code to implement RPC clients and servers wheres Protocol Buffers seems mostly designed as a data-interchange format alone.??
??? thrift提供了一套完整的rpc服務(wù)實(shí)現(xiàn)(多線程socket、非阻塞的socket....)?
6、And according to the wiki the Thrift runtime doesn't run on Windows.?
?? thrift 對(duì)有些語(yǔ)言在windows上不支持:C++?? .....?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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