http://gemantic.iteye.com/blog/1199214

一、About  thrift  
二、什么是thrift,怎么工作?

三、Thrift  IDL

四、Thrift   Demo

五、Thrift 协议栈 以及各层的使用(java 为例)

六、与protocolbuffer的区别

一、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  调用 。

二、什么是thrift,怎么工作?

java  rmi的例子,代码见附件,建立一个java rmi的流程  :

  >>>定义一个服务调用接口 。

  >>>server端:接口实现---impl的实例---注册该服务实现(端口)---启动服务。

  >>>client端:通过ip、端口、服务名,得到服务,通过接口来调用 。

  >>>rmi数据传输方式:java对象序列化 。

Thrift  服务 

  >>>例同rmi ,需要定义通信接口、实现、注册服务、绑定端口……

  >>>如何多种语言之间通信  ?

  >>>数据传输走socket(多种语言均支持),数据再以特定的格式(String ),发送,接收方语言解析   。

        Object --->  String --->  Object  。

问题:编码、解析完全需要自己做 ,复杂的数据结构会编码困难 .

Thrift  服务 :thrift的中间编码层

  >>>java  Object ---> Thrift  Object ---> php  Object  

  >>> 定义thrift的文件 ,由thrift文件(IDL)生成 双方语言的接口、model ,在生成的model以及接口中会有解码编码的代码 。

  >>>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 文件

  1. namespace java com.gemantic.analyse.thrift.index
  2. struct  NewsModel{
  3. 1:i32 id ;
  4. 2:string title;
  5. 3:string content;
  6. 4:string media_from;
  7. 5:string author;
  8. }
  9. service IndexNewsOperatorServices {
  10. bool indexNews(1:NewsModel indexNews),
  11. bool deleteArtificiallyNews(1:i32 id )
  12. }

java  server

  1. package com.gemantic.analyse.thrift.index;
  2. import java.net.InetSocketAddress;
  3. import org.apache.thrift.protocol.TBinaryProtocol;
  4. import org.apache.thrift.server.TServer;
  5. import org.apache.thrift.server.TThreadPoolServer;
  6. import org.apache.thrift.server.TThreadPoolServer.Args;
  7. import org.apache.thrift.transport.TServerSocket;
  8. import org.apache.thrift.transport.TServerTransport;
  9. import org.apache.thrift.transport.TTransportFactory;
  10. public class ThriftServerTest {
  11. /**
  12. * @param args
  13. */
  14. public static void main(String[] args) {
  15. // TODO Auto-generated method stub
  16. IndexNewsOperatorServices.Processor processor = new IndexNewsOperatorServices.Processor(new IndexNewsOperatorServicesImpl());
  17. try{
  18. TServerTransport serverTransport = new TServerSocket( new InetSocketAddress("0.0.0.0",9813));
  19. Args trArgs=new Args(serverTransport);
  20. trArgs.processor(processor);
  21. //使用二进制来编码应用层的数据
  22. trArgs.protocolFactory(new TBinaryProtocol.Factory(true, true));
  23. //使用普通的socket来传输数据
  24. trArgs.transportFactory(new TTransportFactory());
  25. TServer server = new TThreadPoolServer(trArgs);
  26. System.out.println("server begin ......................");
  27. server.serve();
  28. System.out.println("---------------------------------------");
  29. server.stop();
  30. }catch(Exception e){
  31. throw new RuntimeException("index thrift server start failed!!"+"/n"+e.getMessage());
  32. }
  33. }
  34. }

java client

  1. package com.gemantic.analyse.thrift.index;
  2. import org.apache.thrift.TException;
  3. import org.apache.thrift.protocol.TBinaryProtocol;
  4. import org.apache.thrift.protocol.TProtocol;
  5. import org.apache.thrift.transport.TSocket;
  6. import org.apache.thrift.transport.TTransport;
  7. public class ThriftClientTest {
  8. /**
  9. * @param args
  10. * @throws TException
  11. */
  12. public static void main(String[] args) throws TException {
  13. // TODO Auto-generated method stub
  14. TTransport transport = new TSocket("10.0.0.41", 9813);
  15. long start=System.currentTimeMillis();
  16. //      TTransport transport = new TSocket("218.11.178.110",9090);
  17. TProtocol protocol = new TBinaryProtocol(transport);
  18. IndexNewsOperatorServices.Client client=new IndexNewsOperatorServices.Client(protocol);
  19. transport.open();
  20. client.deleteArtificiallyNews(123456);
  21. NewsModel newsModel=new NewsModel();
  22. newsModel.setId(789456);
  23. newsModel.setTitle("this from java client");
  24. newsModel.setContent(" 世界杯比赛前,由于塞尔维亚和黑山突然宣布分裂,国际足联开会决定剔除塞黑,由世界上球迷最多的国家顶替,名额恰巧来到中国。举国上下一片欢腾,中国足协决定由“成世铎”(成龙+阎世铎)组队,进军世界杯。");
  25. newsModel.setAuthor("ddc");
  26. newsModel.setMedia_from("新华08");
  27. client.indexNews(newsModel);
  28. transport.close();
  29. System.out.println((System.currentTimeMillis()-start));
  30. System.out.println("client sucess!");
  31. }
  32. }

php client

  1. <?php
  2. $GLOBALS['THRIFT_ROOT'] = '/home/tjiang/demo/thrift/lib/php/src';
  3. require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
  4. require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
  5. require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
  6. require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';
  7. require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
  8. include_once $GLOBALS['THRIFT_ROOT'].'/packages/TestThrift/TestThrift_types.php';
  9. include_once $GLOBALS['THRIFT_ROOT'].'/packages/TestThrift/IndexNewsOperatorServices.php';
  10. $data=array(
  11. 'id'=>'1',
  12. 'title'=>'demo-标题',
  13. 'content'=>'demo-内容',
  14. 'media_from'=>'hexun',
  15. 'author'=>'xiaodi667'
  16. );
  17. $thrif_server_url = '10.0.0.41';
  18. $transport = new TSocket($thrif_server_url, 9813);
  19. $transport->open();
  20. $protocol = new TBinaryProtocol($transport);
  21. $client= new IndexNewsOperatorServicesClient($protocol, $protocol);
  22. $obj = new NewsModel($data);
  23. $result = $client->indexNews($obj);
  24. $transport->close();
  25. ?>

python client

  1. #!/usr/bin/env python
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. #   http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing,
  14. # software distributed under the License is distributed on an
  15. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. # KIND, either express or implied. See the License for the
  17. # specific language governing permissions and limitations
  18. # under the License.
  19. #
  20. import sys
  21. from TestThrift.ttypes import NewsModel
  22. from TestThrift.IndexNewsOperatorServices import Client
  23. from thrift import Thrift
  24. from thrift.transport import TSocket
  25. from thrift.transport import TTransport
  26. from thrift.protocol import TBinaryProtocol
  27. try:
  28. # Make socket
  29. transport = TSocket.TSocket('10.0.0.41', 9813)
  30. # Buffering is critical. Raw sockets are very slow
  31. transport = TTransport.TBufferedTransport(transport)
  32. # Wrap in a protocol
  33. protocol = TBinaryProtocol.TBinaryProtocol(transport)
  34. # Create a client to use the protocol encoder
  35. client = Client(protocol)
  36. # Connect!
  37. transport.open()
  38. client.deleteArtificiallyNews(123)
  39. newsModel=NewsModel()
  40. newsModel.id=123456
  41. newsModel.title="python Test"
  42. newsModel.content="client test  come from python";
  43. newsModel.media_from="xinhua08"
  44. client.indexNews(newsModel)
  45. #close
  46. transport.close()
  47. except Thrift.TException, tx:
  48. print '%s' % (tx.message)

Csharp client

  1. TTransport transport = new TSocket("10.0.0.41", 9813);
  2. TProtocol protocol = new TBinaryProtocol(transport);
  3. IndexNewsOperatorServices.Client client = new IndexNewsOperatorServices.Client(protocol);
  4. transport.Open();
  5. NewsModel model = new NewsModel();
  6. model.Author = "jww";
  7. model.Title = "title";
  8. model.Content = "client   Come   From   CSharp";
  9. model.Id = 1;
  10. client.deleteArtificiallyNews(123);
  11. Console.WriteLine(client.indexNews(model));

五、Thrift 协议栈 以及各层的使用(java 为例)



1、model   interface

       服务的调用接口以及接口参数model、返回值model

2、Tprotocol    协议层

         将数据(model)编码 、解码 。

3、Ttramsport 传输层

        编码后的数据传输(简单socket、http)

5、Tserver

        服务的Tserver类型,实现了几种rpc调用(单线程、多线程、非阻塞IO)

六、与protocolbuffer的区别

http://liuchangit.com/development/346.html

           

http://stackoverflow.com/questions/69316/biggest-differences-of-thrift-vs-protocol-buffers

区别:

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

支持语言不同,thrift支持着更多的语言 。

2、Thrift supports ‘exceptions 。

   thrift支持服务的异常 。

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简单 。

4、Protobuf serialized objects are about 30% smaller then Thrift.

   Protocol Buffers在序列化/反序列化、传输上性能更优 。

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服务实现(多线程socket、非阻塞的socket....)

6、And according to the wiki the Thrift runtime doesn't run on Windows.

   thrift 对有些语言在windows上不支持:C++   .....

PS:http://blog.csdn.net/amuseme_lu/article/details/6262572

PS:http://dongxicheng.org/search-engine/thrift-guide/

[转] thrift的使用介绍的更多相关文章

  1. Thrift总结(一)介绍

    这段时间,一直在整理公司的内部 rpc 服务接口,面临的一个问题就是:由于公司内部的系统由几个不同的语言编写的.C# ,java,node.js 等,如何实现这些内部系统之间的接口统一调用,确实是比较 ...

  2. Thrift RPC框架介绍

    u 简介 Thrift是一种开源的跨语言的RPC服务框架.Thrift最初由facebook公司开发的,在2007年facebook将其提交apache基金会开源了.对于当时的facebook来说创造 ...

  3. thrift学习之一-------介绍

    thrift 官网:  http://thrift.apache.org/ Thrift是一个软件框架,用于支持可扩展的跨语言服务的开发,它无缝的与C++,Java,Python,PHP,Ruby,E ...

  4. Apache Thrift的简单介绍

    1.什么是Thrift thrift是一种可伸缩的跨语言服务的发展软件框架.它结合了功能强大的软件堆栈的代码生成引擎,以建设服务.不同开发语言开发的服务可以通过该框架实现通信. thrift是face ...

  5. Thrift搭建分布式微服务(一)

    一.Thrift是什么? 关于Thrift的基本介绍,参看张善友的文章Thrift简介. 二.为什么使用微服务? 在公司的高速发展过程中,随着业务的增长,子系统越来越多.各系统间又不同程度的在某些逻辑 ...

  6. 使用C#通过Thrift访问HBase

    前言 因为项目需要要为客户程序提供C#.Net的HBase访问接口,而HBase并没有提供原生的.Net客户端接口,可以通过启动HBase的Thrift服务来提供多语言支持. Thrift介绍 环境 ...

  7. Thrift搭建分布式微服务1

    Thrift搭建分布式微服务 一.Thrift是什么? 关于Thrift的基本介绍,参看张善友的文章Thrift简介. 二.为什么使用微服务? 在公司的高速发展过程中,随着业务的增长,子系统越来越多. ...

  8. Thrift入门初探--thrift安装及java入门实例

    什么是thrift? 简单来说,是Facebook公布的一款开源跨语言的RPC框架. 那么问题来了. 什么是RPC框架? RPC全称为Remote Procedure Call,意为远程过程调用. 假 ...

  9. Thrift总结(二)创建RPC服务

    前面介绍了thrift 基础的东西,怎么写thrift 语法规范编写脚本,如何生成相关的语言的接口.不清楚的可以看这个<Thrift总结(一)介绍>.做好之前的准备工作以后,下面就开始如何 ...

随机推荐

  1. Swift - 18 - 数组的基础操作

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  2. 如何让Hadoop运行得更快一些

    在数据处理方面,我们发现数据输入速度一般要比的数据处理速度快很多,这种现象在大多数据领域尤为明显.随着数据不断膨胀,相应的响应时间自然要有所增加,数据处理的复杂度也在不断提高.作为一个开发者,我们自然 ...

  3. angularJS学习笔记一

    AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是小花招)来让 ...

  4. SASS type-of 函数

    今儿写个type-of,算是备忘录吧. 1.number type-of(0) // number type-of(1px) // number 2.string type-of(a) // stri ...

  5. Python新手学习基础之数据结构-序列2

    长度.最大值和最小值 序列类型的数据结构,常常会用到长度检查.最大最小值检查的函数. 他们的作用: len(序列):返回列表的长度(元素个数): max(序列) :返回列表中元素最大值: min(序列 ...

  6. 如何把powerpoint幻灯片大小改为标准或宽屏教程【图文】

    不知道大家还记得早期powerpoint版本,幻灯片的形状是较方(4:3),随着科技的发展,很多电视.视频都在用宽屏,powerpoint自然也是如此,现在默认的幻灯片大小是宽屏 (16:9),怎么把 ...

  7. hdu 4548 第六周H题(美素数)

    第六周H题 - 数论,晒素数 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   De ...

  8. Python三元表达式

    我们知道Python没有三元表达式,但是我们通过技巧达到三元表达式的效果. 摘自<Dive Into Python>: 在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的 ...

  9. Preventing Web Attacks with Apache

    http://www.boyunjian.com/do/article/snapshot.do?uid=net.csdn.blog/wurangy050/article/details/5287235

  10. java Future 模式

    考慮這樣一個情況,使用者可能快速翻頁瀏覽文件中,而圖片檔案很大,如此在瀏覽到有圖片的頁數時,就會導致圖片的載入,因而造成使用者瀏覽文件時會有停頓 的現象,所以我們希望在文件開啟之後,仍有一個背景作業持 ...