下载安装:
URL:http://archive.apache.org/dist/thrift/0.11.0/ (自行根据版本选择)
Windows下:
https://blog.youkuaiyun.com/liulongling/article/details/51886626
Ubuntu下:
下载文件压缩包使用:wget命令 (参考:https://www.cnblogs.com/wuheng1991/p/5332764.html)
https://blog.youkuaiyun.com/huanbia/article/details/77098205
验证是否安装成功:
thrift -version
thrift框架对比其他:
gRPC (2015年开始),序列化使用ProtoBuf,传输协议HTTP2.0,目前正在发展中
传统http接口,restful,使用短连接,字符串序列化效率低
protobuf只是序列化框架,缺少thrift底层通信的封装,所以出现了gRPC
golang如何使用:
1 在程序中安装thrift库
go get git.apache.org/thrift.git/lib/go/thrift
2 安装开发辅助工具
mac:brew install thrift
3 定义thrift/lucky.thrift文件
主要包括:
1 定义数据结构
namespace [java/php/go]
thrift支持数据结构:struct , bool, i8, i16, i32, i64, double, string, binary, map<t1,t2>, list, set
2 定义服务
3 定义方法
lucky.thrift 参考
namespace go rpc
namespace php rpc
namespace java rpc
# 奖品详情
# 参数的序号是必须的
struct DataGiftPrize{
1: i64 Id = 0
2: string Title = ""
3: string Img = ""
4: i64 Displayorder = 0
5: i64 Gtype = 0
6: string Gdata = ""
}
# 抽奖返回值
struct DataResult{
1:i64 Code
2:string Msg
3:DataGiftPrize Gift
}
# 服务接口
service LuckyService {
# 抽奖的方法
DataResult DoLucky(1:i64 uid, 2:string username, 3:string ip, 4:i64 now, 5:string app, 6:string sign),
list<DataGiftPrize> MyPrizeList(1:i64 uid, 2:string username, 3:string ip, 4:i64 now, 5:string app, 6:string sign),
}
3 生成相应语言的功能代码
在lucky.thrift目录下执行
thrift -out …/ --gen [go/php/java] lucky.thrift
检查生成的代码,排除异常信息