初识thrift
thrift 是 facebook 于2007年开发的一款跨平台 RPC(Remote Procedure Call) 软件框架,
它可以在多种平台上进行无缝交互,数据传输使用二进制的方式,比XML和JSON体积更小,适合于内网的之间的数据进行交互。
thrift 结构
(参见https://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/)
thrift 是由传输层、协议层和业务组成。用户选定的传输层类型、协议层类型之后,只需要关注业务代码即可,无需关注底层实现。
当生成了一套协议后,由客户端和和服务端根据协议文件生成 thrift 的接口库,
接口库会提供定义的service方法,直接调用方法,远程服务端会返回数据。
thrift类型
接口定义地址;
http://thrift.apache.org/docs/types
基本类型:
- bool 对应js中boolean
- byte 8位无符号整数
- i16 16位无符号整数
- i32 32位无符号整数
- i64 64位无符号整数
- double 64位符点型数字
- string utf-8类型的字符串
- binary 一系列基于string类型的编码的字符串
与Js对应的对象类型,
- struct 结构体,可使用基本类型,强类型,需要定义不同的名称
示例:
struct ListItem {
1: i32 id,
2: string content = '',
3: Status status = 1,
4: string author,
5: i32 textLength
}
容器类型,是不能直接在外层定义的,需要在 struct 中定义或者在 service 中定义,
主要包括:
与Js的数组对应的类型:
- list 一系列的数组元素
与Js中set对应的类型:
- set 相当于不重复的数组
与Js中Map对应的类型
- map 类似于字典模式
其他类型;
异常类型
- exception
可调用接口
- service
示例:
service Todo {
list<ListItem> getTodoList(),
i32 getTotalLength(1: string author),
i8 postTodo(1: PostItem item)
ListItem doneArtical(1: i32 id)
ListItem deleteArtical(1: i32 id)
}