一 简介:
Apache Thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器,现在是 Apache 基金会的顶级项目,对于当时的facebook来说创造thrift是为了解决facebook系统中各系统间大数据量的传输通信以及系统之间语言环境不同需要跨平台的特性,所以thrift可以支持多种程序语言:C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml, Delphi等。
Apache Thrift是跨语言服务访问的RPC通信框架。通过接口定义语言(IDL)定义和创建服务,Thrift生成特定语言的可供server和client访问的代码。关于IDL的话题我们可以追溯到CORBA盛行1999-2001年(Common Object Request Broker Architecture/公用对象请求代理体系结构)。Apache Thrfit有着非常优秀的效率。适用于搭建大型数据交换及存储的通用工具,对于大型系统中的内部数据传输相对于JSON和xml无论在性能、传输大小上有明显的优势。
二 需要的工具和文件:
以java为例,使用thirft时,在编写完接口定义文件*.thirft后,需要一个生成器(windows下是thrift-0.9.3.exe)来生成server和client的代码,生成的代码里面依赖一个jar包libthrift-0.9.3.jar。
thrift-0.9.3.exe官网有提供直接下载,libthrift-0.9.3.jar官网有提供maven的下载方式:
http://thrift.apache.org/download
也可以通过下载thrift源码工程来编译生成thrift-0.9.3.exe和libthrift-0.9.3.jar,下载下来的是一个压缩包thrift-0.9.3.tar.gz,解压后路径thrift-0.9.3\compiler下就是thrift-0.9.3.exe的源码,cpp写的,有兴趣可以编译打包一下。路径thrift-0.9.3\lib\java下就是libthrift-0.9.3.jar的源码,直接导入eclipse,直接ant build一下就可以了,在build目录下就能找到打包好的libthrift-0.9.3.jar以及一些依赖的jar包。
三 接口定义文件说明
thrift-0.9.3.tar.gz,解压后文件thrift-0.9.3\tutorial\tutorial.thrift是一份关于接口定义文件的基本说明:
1--支持的类型
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
2--可以include另外一个*.thrift,默认是搜索当前路径的,也可以在编译*.thrift文件时,命令行用-I指定要搜索的被包含文件的路径
include "shared.thrift"
3--针对不同的语言命名空间、包或前缀
namespace cpp tutorial
namespace d tutorial
namespace java tutorial
namespace php tutorial
namespace perl tutorial
namespace haxe tutorial
4--像c语言一样重新命名类型
typedef i32 MyInteger
5--可以定义常量,复杂类型的常量使用json来赋值
const i32 INT32CONSTANT = 9853
const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}
6--可以定义枚举,值是32位的整数,不指定值的话默认从1开始算起
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
}
7--可以定义一个struct,每一个field由整数标识符,一个类型,一个符号名称,和一个可选的默认值,field可声明为可选(如果没set值的话,序列化输出的时候将会忽略)
struct Work {
1: i32 num1 = 0,
2: i32 num2,
3: Operation op,
4: optional string comment,
}
8--定义异常类型的struct
exception InvalidOperation {
1: i32 whatOp,
2: string why
}
9--定义一个服务,可以使用关键字extends继承另外一个服务
service Calculator extends shared.SharedService { //SharedService定义在另外一个文件shared.thrift
//类似c语言风格定义一个方法,可以有参数和异常列表
void ping(),
i32 add(1:i32 num1, 2:i32 num2),
i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),
//用oneway声明的方法只接受请求,没有响应,返回类型必须是void
oneway void zip()
}
以上仅是一些比较基础的说明,更多详细的例子在路径thrift-0.9.3\test下
相关推荐
NULL 博文链接:https://zhuwx.iteye.com/blog/2368356
NULL 博文链接:https://zhuwx.iteye.com/blog/2371657
NULL 博文链接:https://zhuwx.iteye.com/blog/2374326
Apache Thrift is an open source cross language serialization and RPC framework. With support for over 15 programming languages, Apache Thrift can play an important role in a range of distributed ...
NULL 博文链接:https://zhuwx.iteye.com/blog/2377030
NULL 博文链接:https://zhuwx.iteye.com/blog/2347581
Make applications cross-communicate using Apache Thrift! About This Book Leverage Apache Thrift to enable applications written in different programming languages (Java, C++, Python, PHP, Ruby, and so...
本例改编自Apache Thrift教程: http://mikecvet.wordpress.com/2010/05/13/apache-thrift-tutorial-the-sequel/ http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/ 原教程使用的是c++ server和...
详细介绍了Apache Thrift在Ubuntu以及Windows下基于C++和Java语言的安装和运行。附有小例子,亲自测试通过。所述方法网上应该有教程,但大多零散不统一或者不完整,因此本人整理了一份,特来分享。
Learning Apache Thrift
Apache Thrift程序员指南以下示例中的源代码:Apache Thrift程序员指南 本书分为三个部分:第一部分-Apache Thrift概述对Apache Thrift及其体系结构的高级介绍。 这部分的例子非常有趣。 本部分还介绍了基本的Apache...
基于RPC技术的java开发,可用于分布式系统开发,高性能高可用。
FacebookThrift最初是紧跟Apache Thrift发行的,但现在朝着新的方向发展。 特别是,编译器是从头开始重写的,新的实现具有完全异步的Thrift服务器。 在了解有关这些改进的。 您还可以在原始的Facebook Code 了解...
Apache Thrift Java实战源码,包含了客户端和服务端源码,客户端和服务端是分开的,如果需要放到一个工程,直接把Client.java文件复制到服务端运行即可。
Apache Thrift——可伸缩的跨语言服务开发框架
使用Apache thrift通信框架编写的一个实例,以及提供开发中使用thrift所需的jar包
apache-thrift-amqp 支持 Apache Thrift 的 AMQP 传输层该项目为 Ruby 中的 Apache Thrift 实现了 AMQP 传输层,并扩展了 Apache Thrift 教程。 AMQP 传输层依赖 Ruby bunny gem 来连接到 RabbitMQ 代理。 本教程...
Thrift源于facebook之手,是IDL(interface definition language)描述性语言的一个具体实现,在2007年facebook提交Apache基金会将Thrift作为一个开源项目,对于当时的facebook来说创造thrift是为了解决facebook系统中...
Thrifty是 Apache Thrift 软件栈的一个Android实现,它只用到了1/4的方法数。