Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thrift: Scalable Cross-Language Services Implementation

Similar presentations


Presentation on theme: "Thrift: Scalable Cross-Language Services Implementation"— Presentation transcript:

1 Thrift: Scalable Cross-Language Services Implementation
丁冬超

2 目录 About thrift Thrift 能用来干什么 什么是thrift,怎么工作? Thrift IDL Thrift Demo
Thrift 协议栈 以及各层的使用(java 为例) 与protocolbuffer的区别

3 About thrift thrift是一种可伸缩的跨语言服务的发展软件框架。它结合了功能强大的软件堆栈 的代码生成引擎,以建设服务,工作效率和无缝地与C + +,C#,Java,Python和PHP和 Ruby结合。thrift是facebook开发的,我们现在把它作为开源软件使用。thrift允许你定 义一个简单的定义文件中的数据类型和服务接口。以作为输入文件,编译器生成代码 用来方便地生成RPC客户端和服务器通信的无缝跨编程语言(来自百度百科)。 》》最初由facebook开发用做系统内个语言之间的RPC通信 。 》》2007年由facebook贡献到apache基金 ,现在是apache下的opensource之一 。 》》支持多种语言之间的RPC方式的通信:php语言client可以构造一个对象,调用相应 的服务方法来调用java语言的服务 ,跨越语言的C—S rpc 调用 。

4 什么是thrift,怎么工作 java rmi 的例子 ,见代码 》》 定义一个服务调用接口 。
》》 server端:接口实现---impl的实例---注册该服务实现(端口)---启动服务。 》》 client端:通过ip、端口、服务名,得到服务,通过接口来调用 。 》》rmi数据传输方式:java对象序列化 。

5 什么是thrift,怎么工作 Thrift 服务 》》 例同rmi ,需要定义通信接口、实现、注册服务、绑定端口……
》》如何多种语言之间通信 ? 数据传输走socket(多种语言均支持),数据再以特定的格式(String ),发送 ,接收方语言解析 。 Object  String  Object 。 问题:编码、解析完全需要自己做 ,复杂的数据结构会编码困难 ?

6 什么是thrift,怎么工作 Thrift 服务 :thrift的中间编码层
》》java Object  Thrift Object  php Object 》》 定义thrift的文件 ,由thrift文件(IDL)生成 双方语言的接口、model ,在生成的 model以及接口中会有解码编码的代码 。 》》 thrift 文件例子 thrift exe -r -gen java TestThrift.thrift 生成java 代码 thrift exe -r -gen php TestThrift.thrift 生成php代码 thrift exe -r -gen py TestThrift.thrift 生成python代码 thrift exe -r -gen as3 TestThrift.thrift 生成as3代码 thrift exe -r -gen cpp TestThrift.thrift 生成C++代码

7 Thrift IDL Thrift 服务

8 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 )

9 Thrift Demo Thrift 服务 --java 1、生成接口代码 2、 实现 3、服务
4、client demo :java client 、php client (铁钢)、Cshape(维维)、python client (高 铭)

10 java使用thrift TTransport transport = new TSocket("10.0.0.41", 9813);
long start=System.currentTimeMillis(); 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(" 世界杯比赛前"); newsModel.setAuthor("ddc"); newsModel.setMedia_from("新华08"); client.indexNews(newsModel); transport.close();

11 php使用thrift $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(); $thrif_server_url = ' '; $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();

12 python 使用thrift # Make socket
transport = TSocket.TSocket(' ', 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()

13 CSharp 使用thrift 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));

14 Thrift协议栈以及各层 Thrift协议栈以及各层 1、model 2、Tprotocol 协议层 3、Ttramsport 传输层
4、TServer

15 Thrift协议栈以及各层 Thrift协议栈以及各层 1、model interface
服务的调用接口以及接口参数model、返回值model 2、Tprotocol 协议层 将数据(model)编码 、解码 。 3、Ttramsport 传输层 编码后的数据传输(简单socket、heet) 5、Tserver 服务的Tserver类型,实现了几种rpc调用(单线程、多线程、非阻塞IO)

16 与protocolbuffer的区别 参考 : http://liuchangit.com/development/346.html

17 与protocolbuffer的区别 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 2、Thrift supports 'exceptions‘ 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. 4、Protobuf serialized objects are about 30% smaller then Thrift. 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. *****6 、And according to the wiki the Thrift runtime doesn't run on Windows.


Download ppt "Thrift: Scalable Cross-Language Services Implementation"

Similar presentations


Ads by Google