* install
mkdir -p $GOPATH/src/github.com/apache/thrift/lib/go/thrift
cp $GOPATH/src/git.apache.org/thrift.git/lib/go/thrift/* \
$GOPATH/src/github.com/apache/thrift/lib/go/thrift/
For windows:
mkdir -p E:\gocode\src\github.com\apache\thrift\lib\go\thrift
copy E:\gocode\src\git.apache.org\thrift.git\lib\go\thrift E:\gocode\src\github.com\apache\thrift\lib\go\thrift
- 安装 Thrift 的 Golang 库有两种方案:
- 直接通过 go get 命令安装,缺点是因为不可抗拒的网络因素大部分人可能会失败:$ go get git.apache.org/thrift.git/lib/go/thrift
- 通过源码安装:
- 在 $GOPATH 的 src 目录下创建多层级目录:git.apache.org/thrift.git/lib/go
- 从 github 上下载 thrift 0.10.0 的源码 ,解压后进入 thrift-0.10.0/lib/go 目录下,将 thrift 目录 copy 到刚创建的 $GOPATH/src/git.apache.org/thrift.git/lib/go 目录下
- 在任意目录下执行 $ go install git.apache.org/thrift.git/lib/go/thrift 就完成了 golang 的 thrift 库的安装
- 安装 Thrift 的 IDL 编译工具
- windows 平台下安装:
直接下载: thrift complier 下载地址,下载完成后改名为:thrift.exe 并将其放入到系统环境变量下即可使用
- Linux 平台下安装:
从 github 上下载 thrift 0.10.0 的源码,解压后进入:thrift-0.10.0/compiler/cpp 目录执行如下命令完成编译后
$ mkdir cmake-build
$ cd cmake-build
$ cmake ..
$ make$ sudo cp bin/thrift /usr/local/bin/
- 验证是否安装成功:
$ thrift -version,如果打印出来:Thrift version 0.10.0 表明 complier 安装成功
* test:
创建如下的目录结构 代码下载地址 GitHub - mingzhanghui/thriftDemo: https://studygolang.com/articles/9607
cd $GOPATH/src
git clone https://github.com/mingzhanghui/thriftDemo
mv thriftDemo ThriftDemo
E:\GOCODE\SRC\THRIFTDEMO
├─client
└─ client.go
├─example
│ └─format_data-remote
├─server
└─ server.go
└─thrift_file
└─ example.thrift
创建example.thrift
编译 thrift 文件
进入 thrift_file 目录执行:$ thrift -out .. --gen go example.thrift,就会在 thrift_file 的同级目录下生成 golang 的包:example,其中 format_data-remote 是生成的测试代码可以不用特别关注
实现server端和client端
thriftDemo/server/server.go at master · mingzhanghui/thriftDemo · GitHub
thriftDemo/client/client.go at master · mingzhanghui/thriftDemo · GitHub
修改example目录下代码的错误
thriftDemo/example at master · mingzhanghui/thriftDemo · GitHub
有个回调函数缺少一个参数
ctx context.Context
还需要import "context"
执行验证结果:
- 先启动 server,之后再执行 client
- client 侧控制台如果打印的结果为: HELLO,WORLD! ,证明 Thrift 的 RPC 接口定义成功