Go语言:thrift协议0.14.1的使用示例

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语言之间的跨语言调用的兼容性测试》

 

参考文章:

《Go thrift使用举例》

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值