Thrift基本使用

thrift属于facebook.com技术核心框架之一,使用不同开发语言开发的系统可以通过该框架实现彼此间的通讯,开发者只需编辑一份thrift脚本,即可自动获得其它开发语言的代码(比如 c++ java python ruby c# haskell ocmal erlang cocoa php squeak).
thrift侧重点是构建夸语言的可伸缩的服务,特点就是支持的语言多,同时提供了完整的rpc service framework,可以很方便的直接构建服务,不需要做太多其他的工作。服务端可以根据需要编译成simple | thread-pool | threaded | nonblocking等方式;
thrift支持多种协议格,Thrift的代码实现,有专门的TProtocol和TTransport抽象,相互配合,可以实现多种协议,方便集成各种传输方式,目前支持xml、json等。
使用了epool
Apache软件基金会已将Thrift作为孵化器项目纳入其中。
thrift目前不支持Windows平台,不过有牛人已经在cygwin上编译调试通过了。
官方开发白皮书:http://incubator.apache.org/thrift/static/thrift-20070401.pdf

1 根据需求,书写 .thrift 服务接口文件 [用tutorial里的demo]
1.1 需要事先了解 ThriftTypes,对应各语言的本地类型
http://wiki.apache.org/thrift/ThriftTypes
1.2 了解thrift 接口定义语言 Thrfit IDL来写 .thrift文件
http://wiki.apache.org/thrift/ThriftIDL
1.3 .thrift 接口文件

/**

* This Thrift file can be included by other Thrift files that want to share

* these definitions.

*/

 

namespace cpp shared

namespace java shared

namespace perl shared

 

struct SharedStruct {

1: i32 key

2: string value

}//定义一个结构体

//定义一个服务

service SharedService {

SharedStruct getStruct(1: i32 key)

}

thrift的类型有如下几种:

/**

* The first thing to know about are types. The available types in Thrift are:

*

* 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

*

* Did you also notice that Thrift supports C style comments?

*/

其中 list<t1>    Ordered list of one type对应C++中的vector<t1>

使用下面的语句,生成cpp骨架文件

thrift –r –gen cpp service.thrift

生成7个文件,分别是由service.thrift脚本定义的类型文件四个,两个.h文件(service_constants.h,service_types.h),两个对应的.cpp文件(service_constants.cpp,service_types.cpp)。service_types对应的文件中,定义了对应的由service.thrift脚本定义的类型。例如struct SharedStruct对应到一个类。另外三个文件分别是由service.thrift脚本中所定义的服务相关的文件,分别是SharedService .h,SharedService .cpp,以及SharedService_server.skeleton.cpp 骨架文件,我们只需要修改SharedService_server.skeleton.cpp 骨架文件中相关的接口部分的逻辑,即可生成对应的服务。

### 如何使用 Thrift IDL 进行接口定义 #### 定义命名空间 为了确保生成的代码能够正确地集成到不同的编程环境中,通常会在文件顶部指定目标语言的命名空间。 ```thrift namespace py my_service // Python中的包名为my_service namespace java com.example.myapp.service // Java中的包路径为com.example.myapp.service ``` #### 数据类型声明 Thrift支持多种内置的数据类型以及复杂结构体的创建。以下是几种常见的数据类型的例子: - **基本类型** - 字符串 `string` 表示文本字符串[^3]。 ```thrift string user_name; ``` - **数值型** - 整数有 `i8`, `i16`, `i32`, 和 `i64`;浮点数则由 `double` 来表示。 ```thrift i32 age; double height; ``` - **布尔值** - 使用 `bool` 类型来存储真/假值。 ```thrift bool is_active; ``` - **列表 (List)** - 列表是一系列相同类型的元素集合,形式如下所示: ```thrift list<i32> scores; // 存储整数列表 ``` - **映射 (Map)** - 映射是以键值对的形式存在,其中键和值都可以是任意有效类型: ```thrift map<string, i32> score_map; // 键为字符串,值为整数的地图 ``` - **结构体 (Structs)** - 结构体用来组合多个字段成一个复合对象,每个成员变量都需要赋予唯一的编号以便序列化时识别. ```thrift struct Person { 1: required string name, 2: optional i32 id = 0, // 可选参数,默认值设为0 3: set<PhoneType> phone_types } ``` #### 异常处理 当远程调用可能出现错误情况时,可以通过定义异常类来进行捕获并返回给客户端。 ```thrift exception InvalidOperation { 1: string what_went_wrong } ``` #### 接口定义 服务接口通过关键字 `service` 开始定义,并且可以继承其他的服务接口。方法签名应该包含输入输出参数及其对应的类型说明。 ```thrift service Calculator extends shared.SharedService { /** * 加法运算函数. */ i32 add(1:i32 num1, 2:i32 num2), /** * 减法运算函数. */ void subtract(1:i32 minuend, 2:i32 subtrahend) throws ( 1:InvalidOperation invalid_operation), oneway void zip() // Oneway 方法不会等待服务器响应即可结束执行 } ``` 一旦完成了上述所有的定义之后,就可以利用命令行工具编译 `.thrift` 文件以生成所需的源码了[^2]。 ```bash thrift --gen cpp calculator.thrift # C++ 代码 thrift --gen php calculator.thrift # PHP 代码 ... ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值