Protobuf 介绍
序列化库在网络传输,RPC,数据库访问等环境中经常用到,它的性能的好坏直接影响着整个产品的性能, 所以我们有必要对一些优秀的序列化库进一步的学习与掌握.
protobuf是Google开发的一种数据描述语言语言,能够将结构化的数据序列化,可用于数据存储,通信协议等方面,官方版本支持 Go, C++, Java, Python,社区版本支持更多语言.
相对于JSON和XML具有以下优点:
- 简洁
- 体积小:是json的10分之一,xml格式的20分之一,是二进制序列化的10分之一
- 速度快:解析速度比XML快20 ~ 100倍
- 使用Protobuf的编译器,可以生成更容易在编程中使用的数据访问代码
- 更好的兼容性,Protobuf设计的一个原则就是要能够很好的支持向下或向上兼容
- protobuf将数据序列化为二进制之后,占用的空间相当小,基本仅保留了数据部分,而xml和json会附带消息结构在数据中;
IDE设置
默认情况下IDE goland 是不支持protobuf协议文件类型".proto"的,为了更快高效的编写proto文件中的代码下面我们介绍一款插件让其支持,该插件支持关键字高亮及语法错误提示
1、File->Settings->Plugins->输入protobuf support->install,并重启IDE
2、file->Settings->Editor->File Types,找到Protobuf,注册支持*.proto
编译器配置
protobuffer属于跨平台,跨语言的协议,有自己的编译器
protoc是Protobuf编译器
下载最新的压缩包,解压后将其中的protoc.exe程序放到gopath的bin目录中,最好把gopath的bin目录添加到系统环境变量里
下载地址:https://github.com/protocolbuffers/protobuf/releases
测试:
安装protobuf库文件:
go get github.com/golang/protobuf/proto
安装goprotobuf插件(go版本的 Protobuf 编译器插件):
go get github.com/golang/protobuf/protoc-gen-go
示例
目录结构:
说明:
protos目录中存放我们自己要写的proto格式文件
syntax = "proto3";
// 新版的proto编译器都要求文件中标注编译后文件的存储位置
// 即生成的.pb.go文件会存放在../pb中,并和该文件名保持一致
// test.proto => test.pb.go
option go_package = "../pb";
message Person {
string Name = 1;
int32 Age = 2;
}
生成.pb.go
到protos目录下执行
protoc --go_out=. *.proto
- 1
pb目录下就存放生成的文件
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc v3.14.0
// source: test.proto
package pb
import (
proto "github.com/golang/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsVersion4
type Person struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=Name,proto3" json:"Name,omitempty"`
Age int32 `protobuf:"varint,2,opt,name=Age,proto3" json:"Age,omitempty"`
}
func (x *Person) Reset() {
*x = Person{}
if protoimpl.UnsafeEnabled {
mi := &file_test_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Person) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Person) ProtoMessage() {}
func (x *Person) ProtoReflect() protoreflect.Message {
mi := &file_test_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Person.ProtoReflect.Descriptor instead.
func (*Person) Descriptor() ([]byte, []int) {
return file_test_proto_rawDescGZIP(), []int{0}
}
func (x *Person) GetName() string {
if x != nil {
return x.Name
}
return ""
}
func (x *Person) GetAge() int32 {
if x != nil {
return x.Age
}
return 0
}
var File_test_proto protoreflect.FileDescriptor
var file_test_proto_rawDesc = []byte{
0x0a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2e, 0x0a, 0x06,
0x50, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x41, 0x67,
0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x41, 0x67, 0x65, 0x42, 0x07, 0x5a, 0x05,
0x2e, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_test_proto_rawDescOnce sync.Once
file_test_proto_rawDescData = file_test_proto_rawDesc
)
func file_test_proto_rawDescGZIP() []byte {
file_test_proto_rawDescOnce.Do(func() {
file_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_test_proto_rawDescData)
})
return file_test_proto_rawDescData
}
var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_test_proto_goTypes = []interface{}{
(*Person)(nil), // 0: Person
}
var file_test_proto_depIdxs = []int32{
0, // [0:0] is the sub-list for method output_type
0, // [0:0] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_test_proto_init() }
func file_test_proto_init() {
if File_test_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Person); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_test_proto_rawDesc,
NumEnums: 0,
NumMessages: 1,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_test_proto_goTypes,
DependencyIndexes: file_test_proto_depIdxs,
MessageInfos: file_test_proto_msgTypes,
}.Build()
File_test_proto = out.File
file_test_proto_rawDesc = nil
file_test_proto_goTypes = nil
file_test_proto_depIdxs = nil
}
main函数就是主体业务逻辑,这里做序列化和反序列化演示:
package main
import (
"fmt"
"grpc_learn/pb"
"github.com/golang/protobuf/proto"
)
func main() {
p1 := pb.Person{
Name: "Jack",
Age: 34,
}
fmt.Printf("person: %+v\n", p1)
buffer, _ := proto.Marshal(&p1)
fmt.Println("序列化person: ", buffer)
var person pb.Person
proto.Unmarshal(buffer, &person)
fmt.Printf("反序列化person: %+v\n", person)
}
转自:
https://blog.csdn.net/csdniter/article/details/112209397