echo.thrift:
namespace go echo
struct EchoReq {
1: string msg;
}
struct EchoRes {
1: string msg;
}
service Echo {
EchoRes echo(1: EchoReq req);
}
如果是Win10的话直接下载执行exe,其它系统可能需要先安装thrift 0.14.1,下载地址https://downloads.apache.org/thrift/
thrift-0.14.1.exe -r --gen go echo.thrift
生成目录gen-go结构如下:
.
├── client.go
├── echo.thrift
├── gen-go
│ └── echo
│ ├── echo-consts.go
│ ├── echo.go
│ ├── echo-remote
│ │ └── echo-remote.go
│ └── GoUnusedProtection__.go
├── go.mod
├── go.sum
└── server.go
go.mod (thrift客户端使用v0.14.1这个版本号和thrift代码自动生成器的版本号是一致的,而且可以看到在$GOPATH/pkg/mod/github.com/apache/thrift\@v0.14.1/lib/go/thrift/目录下都是*.go文件没有C语言所以不用编译)
module thrift-example
go 1.13
require github.com/apache/thrift v0.14.1
示例代码里和之前的版本不同的有两点:
1)引用路径现在是”github.com/apache/thrift/lib/go/thrift“
2)函数的声明和调用增加了context.Context参数
server.go
package main
import (
"context"
"fmt"
"github.com/apache/thrift/lib/go/thrift"
"thrift-example/gen-go/echo"
)
type EchoServer struct {
}
func (e *EchoServer) Echo(ctx context.Context, req *echo.EchoReq) (*echo.EchoRes, error) {
fmt.Printf("message from client: %v\n", req.GetMsg())
res := &echo.EchoRes{
Msg: "success",
}
return res, nil
}
func main() {
transport, err := thrift.NewTServerSocket(":9898")
if err != nil {
panic(err)
}
handler := &EchoServer{}
processor := echo.NewEchoProcessor(handler)
transportFactory := thrift.NewTBufferedTransportFactory(8192)
//也可以选择Framed传输协议,但是必须确保服务端和客户端的传输协议一致
//transportFactory := thrift.NewTBufferedTransportFactory(8192)
//framedtransportFactory := thrift.NewTFramedTransportFactory(transportFactory)
//可以选择任意一种编码协议,但是必须确保服务端和客户端的编码协议一致
protocolFactory := thrift.NewTBinaryProtocolFactory(true, true) //布尔参数strictRead, strictWrite,读和写时是否加入版本校验。
//protocolFactory := thrift.NewTCompactProtocolFactory()
//protocolFactory := thrift.NewTJSONProtocolFactory()
server := thrift.NewTSimpleServer4(
processor,
transport,
transportFactory,
protocolFactory,
)
if err := server.Serve(); err != nil {
panic(err)
}
}
client.go
package main
import (
"context"
"fmt"
"github.com/apache/thrift/lib/go/thrift"
"log"
"net"
"os"
"thrift-example/gen-go/echo"
)
func main() {
transportFactory := thrift.NewTBufferedTransportFactory(8192)
//可以选择任意一种通信协议,但是必须确保服务端和客户端的通信协议一致
protocolFactory := thrift.NewTBinaryProtocolFactory(true, true) //布尔参数strictRead, strictWrite,读和写时是否加入版本校验。
//protocolFactory := thrift.NewTCompactProtocolFactory()
//protocolFactory := thrift.NewTJSONProtocolFactory()
transport, err := thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "9898"))
if err != nil {
fmt.Fprintln(os.Stderr, "error resolving address:", err)
os.Exit(1)
}
useTransport, err := transportFactory.GetTransport(transport)
client := echo.NewEchoClientFactory(useTransport, protocolFactory)
if err := transport.Open(); err != nil {
fmt.Fprintln(os.Stderr, "Error opening socket to 127.0.0.1:9898", " ", err)
os.Exit(1)
}
defer transport.Close()
req := &echo.EchoReq{Msg: "You are welcome."}
res, err := client.Echo(context.Background(), req)
if err != nil {
log.Println("Echo failed:", err)
return
}
log.Println("response:", res.Msg)
fmt.Println("well done")
}
测试使用的Go版本go1.13.8
相关文章:
《thrift协议0.14.1在Nodejs和Go语言之间的跨语言调用的兼容性测试》
参考文章: