从零开始基于gothrift创建一个RPC服务
Thrift 是一种被广泛使用的 rpc 框架,可以比较灵活的定义数据结构和函数输入输出参数,并且可以跨语言调用。为了保证服务接口的统一性和可维护性,我们需要在最开始就制定一系列规范并严格遵守,降低后续维护成本。Thrift开发流程是:先定义IDL,使用thrift工具生成目标语言接口(interface)代码,然后进行开发。 官网: http://thrift.apache.org/ github:https://github.com/apache/thr... 安装Thrift将Thrift IDL文件编译成目标代码需要安装Thrift二进制工具。 Mac建议直接使用brew安装,节省时间: brew install thrift安装后查看版本: $ thrift -versionThrift version 0.12.0也可以下载源码安装,参考:http://thrift.apache.org/docs...。 源码地址:http://www.apache.org/dyn/clo... CentOS需下载源码安装,参考:http://thrift.apache.org/docs...。 Debian/Ubuntu需下载源码安装,先安装依赖:http://thrift.apache.org/docs...,然后安装thrift:http://thrift.apache.org/docs...。 Windows可以直接下载二进制包。地址:http://www.apache.org/dyn/clo...。 实战该小节我们通过一个例子,讲述如何使用Thrift快速开发出一个RPC微服务,涉及到Golang服务端、Golang客户端、PHP客户端、PHP服务端。项目名就叫做thrift-sample,代码托管在 https://github.com/52fhy/thri...。 推荐使用Golang服务端实现微服务,PHP客户端实现调用。 编写thrift IDLthrift├── Service.thrift└── User.thriftUser.thrift namespace go Samplenamespace php Samplestruct User { 1:required i32 id; 2:required string name; 3:required string avatar; 4:required string address; 5:required string mobile;}struct UserList { 1:required list<User> userList; 2:required i32 page; 3:required i32 limit;}Service.thrift include "User.thrift"namespace go Samplenamespace php Sampletypedef map<string, string> Datastruct Response { 1:required i32 errCode; //错误码 2:required string errMsg; //错误信息 3:required Data data;}//定义服务service Greeter { Response SayHello( 1:required User.User user ) Response GetUser( 1:required i32 uid )}说明: 1、namespace用于标记各语言的命名空间或包名。每个语言都需要单独声明。2、struct在PHP里相当于class,golang里还是struct。 3、service在PHP里相当于interface,golang里是interface。service里定义的方法必须由服务端实现。 4、typedef和c语言里的用法一致,用于重新定义类型的名称。 5、struct里每个都是由1:required i32 errCode;结构组成,分表代表标识符、是否可选、类型、名称。单个struct里标识符不能重复,required表示该属性不能为空,i32表示int32。 ...