go语言之thrift协议二
在上一篇文章分析了在thrift协议下面,使用thrift生成了go对应的文件,以及thrift不同的语法在go中生成的模块。接下来结合官方的示例分析一下thrift的基本使用。
实现看一下在server端的实现。
server
package main
import (
"crypto/tls"
"flag"
"fmt"
"os"
"github.com/apache/thrift/lib/go/thrift"
)
func Usage() {
fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n")
flag.PrintDefaults()
fmt.Fprint(os.Stderr, "\n")
}
func main() {
flag.Usage = Usage
// 是server端还是client端
server := flag.Bool("server", false, "Run server")
// 编码方式 默认是compact
protocol := flag.String("P", "compact", "Specify the protocol (binary, compact, json, simplejson)")
// 是否使用基于frame格式的编码方式
framed := flag.Bool("framed", false, "Use framed transport")
// 是否使用带有buffer的transport
buffered := flag.Bool("buffered", false, "Use buffered transport")
// server或者client的地址
addr := flag.String("addr", "localhost:9090", "Address to listen to")
// 是否使用https
secure := flag.Bool("secure", false, "Use tls secure transport")
// 解析参数
flag.Parse()
// 根据不同的protocol初始化不同的thrift.TProtocolFactory
var protocolFactory thrift.TProtocolFactory
switch *protocol {
case "compact":
protocolFactory = thrift.NewTCompactProtocolFactoryConf(nil)
case "simplejson":
protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(nil)
case "json":
protocolFactory = thrift.NewTJSONProtocolFactory()
case "binary", "":
protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil)
default:
fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n")
Usage()
os.Exit(1)
}
// 根据参数生成不同的transportFactory
var transportFactory thrift.TTransportFactory
cfg := &thrift.TConfiguration{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
if *buffered {
transportFactory = thrift.NewTBufferedTransportFactory(8192)
} else {
transportFactory = thrift.NewTTransportFactory()
}
if *framed {
transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, cfg)
}
// 初始化server或者client
if *server {
if err := runServer(transportFactory, protocolFactory, *addr, *secure); err != nil {
fmt.Println("error running server:", err)
}
} else {
if err := runClient(transportFactory, protocolFactory, *addr, *secure, cfg); err != nil {
fmt.Println("error running client:", err)
}
}
}
这里可以看出来这里的主要就是有两个动作
- 初始化thrift.TProtocolFactory 这里因为使用了默认的protocol也就是compact。所以这里是 protocolFactory = thrift.NewTCompactProtocolFactoryConf(nil)
- 初始化thrift.TTransportFactory。因为这里是初始化的默认的.所以transportFactory = thrift.NewTTransportFactory()
这里看一下这两个方法
thrift.TProtocolFactory
从名字上来看这个应该是一个工厂模式。然后先看一下这个定义
type TProtocolFactory interface {
GetProtocol(trans TTransport) TProtocol
}
所以看出来这个具体的作用就是根据TTransport进行封装,然后生成符合TProtocol的结构体
然后看一下TTransport和TProtocol这两个方法
TTransport
// Encapsulates the I/O layer
type TTransport interface {
io.ReadWriteCloser
ContextFlusher
ReadSizeProvider
// Opens the transport for communication
Open() error
// Returns true if the transport is open
IsOpen() bool
}
然后看一下继承的结构体的属性
ReadWriteCloser
// ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
type ReadWriteCloser interface {
Reader
Writer
Closer
}
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
type Closer interface {
Close() error
}
ContextFlusher
type ContextFlusher interface {
Flush(ctx context.Context) (err error)
}
ReadSizeProvider
type ReadSizeProvider interface {
RemainingBytes() (num_bytes uint64)
}
从后面可以看出来这就是服务端接收到连接后,拿到的连接,所以这个里面有read,write等方法
TProtocol
这个就是具体的编码方式对应的interface,具体如下
type TProtocol interface {
WriteMessageBegin(ctx context.Context, name string, typeId TMessageType, seqid int32) error
WriteMessageEnd(ctx context.Context) error
WriteStructBegin(ctx context.Context, name