公司内的技术比较闭塞,以至于个人对外博客很久没有东东可以分享了;
最近稍有闲情,打算对比一下golang下的几个rpc框架,折腾thrift时发现了一个小问题记录;

一、环境
官网 https://thrift.apache.org/download 下载的thrift 0.11.0版本;
go get git.apache.org/thrift.git/lib/go/thrift 安装的最新thrift go包;

二、错误
在build server时,发现以下两类错误:


tomzhou@TZ-NB:/data/gocode/demo/bin$ go build server
# model
../src/model/MathService.go:103:14: not enough arguments in call to oprot.Flush
have ()
want (context.Context)
../src/model/MathService.go:120:16: not enough arguments in call to oprot.Flush
have ()
want (context.Context)
../src/model/MathService.go:133:16: not enough arguments in call to oprot.Flush
have ()
want (context.Context)
../src/model/MathService.go:147:24: not enough arguments in call to oprot.Flush
have ()
want (context.Context)

tomzhou@TZ-NB:/data/gocode/demo/bin$ go build server
# server
../src/server/main.go:36:44: cannot use handler (type *MathServiceImpl) as type model.MathService in argument to model.NewMathServiceProcessor:
*MathServiceImpl does not implement model.MathService (wrong type for Add method)
  have Add(int32, int32) (int32, error)
  want Add(context.Context, int32, int32) (int32, error)


三、原因
google一下发现不少人碰到这个问题,但没有人给出如何解决;
仔细查阅资料,发现根本原因是thrift在git上的go包更新了增加对go 1.7中的http.request + context的用法,部分函数中增加了context参数;但是官网下载的0.11.0.tgz包并没有更新;所以不兼容无法编译;

具体commit的原因参考:
https://github.com/apache/thrift/pull/1459

提交的代码参考:
https://github.com/apache/thrift/commit/5785279e2e809f6c56dbbe0eb41d13fb17c88bdd#diff-8a01e4124163225974263ecb577e8bf4


四、解决办法

需要解决两个问题:
1. 更新thrift
https://github.com/apache/thrift 下载最新zip包,编译thrift;

2. 服务实现的时候,前面增加一个context.Context的参数;


Program | 评论(0) | 引用(0) | 阅读(2421)