为什么要使用protobuf
最近的项目中,一直使用Json做数据传输。Json用起来的确很方便。但相对于protobuf数据量更大些。做一个移动端应用,为用户省点流量还是很有必要的。正好也可以学习一下protobuf的使用
跟Json相比protobuf性能更高,更加规范
但也失去了一些便利性
安装
在go中使用protobuf,有两个可选用的包goprotobuf(go官方出品)和gogoprotobuf。
gogoprotobuf完全兼容google protobuf,它生成的代码质量和编解码性能均比goprotobuf高一些
安装protoc
首先去https://github.com/google/pro… 上下载protobuf的编译器protoc,windows上可以直接下到exe文件(linux则需要编译),最后将下载好的可执行文件拷贝到\$GOPATH的bin目录下(\$GOPATH/bin目录最好添加到系统环境变量里)
安装protobuf库文件
1
| go get github.com/golang/protobuf/proto
|
goprotobuf
安装插件
1
| go get github.com/golang/protobuf/protoc-gen-go
|
生成go文件
1
| protoc --go_out=. *.proto
|
gogoprotobuf
安装插件
gogoprotobuf有两个插件可以使用
1 2 3 4 5
| go get github.com/gogo/protobuf/protoc-gen-gogo go get github.com/gogo/protobuf/protoc-gen-gofast
|
安装gogoprotobuf库文件
1 2
| go get github.com/gogo/protobuf/proto go get github.com/gogo/protobuf/gogoproto
|
生成go文件
1 2 3 4 5
| //gogo protoc //gofast protoc
|
性能测试
这里只是简单的用go test测试了一下
1 2 3 4 5 6 7 8 9 10 11
| "编码":447ns/op "解码":422ns/op "编码":433ns/op "解码":427ns/op "编码":112ns/op "解码":112ns/op
|
go_protobuf的简单使用
test.proto
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| syntax = "proto3"; package proto; enum FOO { X = 0; }; message UserInfo{ string message = 1; int32 length = 2; int32 cnt = 3; }
|
client_protobuf.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package main import ( "bufio" "fmt" "net" "os" stProto "proto" "time" "github.com/golang/protobuf/proto" ) func main() { strIP := "localhost:6600" var conn net.Conn var err error for conn, err = net.Dial("tcp", strIP); err != nil; conn, err = net.Dial("tcp", strIP) { fmt.Println("connect", strIP, "fail") time.Sleep(time.Second) fmt.Println("reconnect...") } fmt.Println("connect", strIP, "success") defer conn.Close() cnt := 0 sender := bufio.NewScanner(os.Stdin) for sender.Scan() { cnt++ stSend := &stProto.UserInfo{ Message: sender.Text(), Length: *proto.Int(len(sender.Text())), Cnt: *proto.Int(cnt), } pData, err := proto.Marshal(stSend) if err != nil { panic(err) } conn.Write(pData) if sender.Text() == "stop" { return } } }
|
server_protobuf.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package main import ( "fmt" "net" "os" stProto "proto" "github.com/golang/protobuf/proto" ) func main() { listener, err := net.Listen("tcp", "localhost:6600") if err != nil { panic(err) } for { conn, err := listener.Accept() if err != nil { panic(err) } fmt.Println("new connect", conn.RemoteAddr()) go readMessage(conn) } } func readMessage(conn net.Conn) { defer conn.Close() buf := make([]byte, 4096, 4096) for { cnt, err := conn.Read(buf) if err != nil { panic(err) } stReceive := &stProto.UserInfo{} pData := buf[:cnt] err = proto.Unmarshal(pData, stReceive) if err != nil { panic(err) } fmt.Println("receive", conn.RemoteAddr(), stReceive) if stReceive.Message == "stop" { os.Exit(1) } } }
|