Thrift简介

Thrift由FaceBook开发,其中文意思是“节俭”,引申一下,应该是“省事、省力”的意思,毕竟它使原本不能一起用的语言可以一起work了,这样能够博取众家之长来为我所用的手段,的确是省力省心。

其产生的背景是FB的工程师们在开发过程的觉得LAMP(Linux+Apache+Mysql/MariaDB+Perl/PHP/Python)架构对他们的限制越来越大,比如性能、开发的速度以及某些特殊库的使用上,而他们又很想找到一种标准统一的方式来高效可靠的解决各种问题,而不愿意去迁就语言的局限性(Facebook’s engineering culture has tended towards choosing the best tools and implementations available over standardizing on any one programming language and begrudgingly accepting its inherent limitations),所有Thrift应运而生了,他们看到世面还没有开源的,于是就把Thrift开源了。目前,Thrift主要应用在FB的搜索(php&C++)和日志系统上。

简介

Thrift是一个软件库和一套代码生成工具的统称(a software library and set of code-generation tools),其主要目的是将不同编程语言之间存在的差异抽象成公共的库来使他们可以一起协同工作,各个语言之间的交流方式是 RPC,RPC的代码可由Thrift的配置文件生成,编写该配置文件所使用的语言中立于任何其他编程语言( IDL)。
整个Thrift有几个比较重要的组成部分,它们分别是:
  • Type:不同语言共同的类型系统,比如C++中的STL风格的Map可以很方便与Python中的动态字典交换数据。
  • Transport:不同语言之间的数据交互方式,包括TCP、内存、硬盘等,这些数据交互方式对程序员是不可见的。
  • Protocol:数据传输协议,这个是介于Type和Transport之间的一层,它可以将数据进行编码交给Transport传输,也可以将Transport的数据进行解码编程Type。
  • Versioning:版本控制。主要是用来处理客户端和服务器之间的数据结构不一致的情况。
  • Processor:就是靠他来实现远程方法调用。

Type

分为 基本数据类型复合数据类型,Thrfit中的数据类型都是大部分语言所共有的,除了基本的数据结构之外,Thrift没有引入动态对象以及复杂的类等数据类型。

基本数据类型

  • bool: A boolean value, true or false
  • byte: A signed byte
  • i16: A 16-bit signed integer 2.4
  • i32: A 32-bit signed integer Exceptions are syntactically and functionally equivalent to structs
  • i64: A 64-bit signed integer
  • double: A 64-bit floating point number
  • string: An encoding-agnostic text or binary string
考虑到有些语言不支持无符号数,在实际使用中无符号数的用处也不是很大,所以Thrift并没有引入无符号类型。

复合数据类型

  • 结构体
struct Example {
1:i32 number=10,
2:i64 bigNumber,
3:double decimals,
4:string name="thrifty"
}
前面的数字以及紧跟着的数据类型可以作为标识来方便版本控制。
  • 容器
list<type>: An ordered list of elements. Translates directly into an  STL vector, Java ArrayList, or  native array in scripting languages. May contain duplicates.
set<type>: An unordered set of unique elements. Translates into an  STL set, Java HashSet, set in Python, or native dictionary in PHP/Ruby.
map<type1,type2>: A map of strictly unique keys to values Translates into an  STL map, Java HashMap, PHP associative array, or  Python/Ruby dictionary.
  • 服务
service StringCache {
void set(1:i32 key, 2:string value),
string get(1:i32 key) throws (1:KeyNotFound knf),
void delete(1:i32 key)
}
服务就相当于一个只有方法没有成员变量的类,其中,void也是一种数据类型,它的意义是告诉服务器函数已经正确执行。如果在void前加上 async,则程序将不等待该函数执行结束就接着执行接下来的代码。

Transport

该层对外提供的接口( TTransport)有:
open:Opens the tranpsort
close :Closes the tranport
isOpen: Indicates whether the transport is open
read: Reads from the transport
write: Writes to the transport
flush: Forces any pending writes
除以上接口外,还有 TServerTransport接口:
open:Opens the transport
listen:Begins listening for connections
accept:Returns a new client transport
close:Closes the transport
除了典型的socket数据传输方式外,Thrift还支持硬盘文件读写( TFileTransport)、缓存读写( TBufferedTransport)、按帧读写( TFramedTransport)、内存读写( TMemoryBuffer)等。

Versioning

由于代码是客户端和服务器端分别生成的,在以后的使用中有可能因为某一端的变化出现不一致的情况,Thrift采取了一些措施来处理这些情况。
1. Added field, old client, new server. In this case, the old client does not send the new field. The new server recognizes that the field is not set, and implements default behavior for out-of-date requests.
2. Removed field, old client, new server. In this case, the old client sends the removed field. The new server simply ignores it.
3. Added field, new client, old server. The new client sends a field that the old server does not recognize. The old server simply ignores it and processes as normal.
4. Removed field, new client, old server. This is the most dangerous case, as the old server is unlikely to have suitable default behavior implemented for the missing field. It is recommended that in this situation the new server be rolled out prior to the new clients.

例子

本例实现语言:服务器端java+客户端语言python。
基本流程:
1、编写thrift文件;
2、在客户端运行thrift -r --gen language xxx.thrift,生成核心代码;
3、在客户端编写自定义的程序;
4、在服务器端运行thrift -r --gen language xxx.thrift,生成核心代码;
5、编写代码实现service中的接口以及接收/处理客户端的请求。

编写.thrift文件

详细语法可参考:http://roclinux.cn/?p=3316
这里说两点:
1、服务可以继承
2、所有同一文件定义的基本数据类型或复合数据类型都属于同一个作用域,也就是说服务中的方法可以调用服务外部定义的数据类型。

服务器端

需要3个jar包:
libthrift-x.x.x.jar:http://repo1.maven.org/maven2/org/apache/thrift/libthrift/
slf4j-api-xxx.jar,slf4j-simple-xxx.jar:http://www.slf4j.org/download.html

自定义代码:
https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=tree;f=tutorial/java/src;hb=HEAD

客户端

不需要额外的安装包,自定义代码:
https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob;f=tutorial/py/PythonClient.py;h=0554ee1291e1e63a711753892386d674462670e7;hb=HEAD

参考

Linux大棚版Thrift入门教程:http://roclinux.cn/?p=3316
Thrift: Scalable Cross-Language Services Implementation:http://thrift.apache.org/static/files/thrift-20070401.pdf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值