protobuf和json的对比

protobuf简介

  • 习惯用Json,XML数据储存格式的程序员,大多都没听说过Protocol Buffer(ProtoBuf go初体验博客)
  • Protocol Buffer其实是Google出品的一种轻量&高效的结构化数据储存格式,性能比Json,XML真的强太多
  • protobuf经历了protobuf2和protobuf3,pb3比pb2简化了很多,目前主流的版本是pb3

protobuf优缺点

  • 优点
    • 性能
      • 压缩性好
      • 序列化和反序列化快 (比XML和Json快2-100倍)
      • 传输速度快
    • 便捷性
      • 使用简单;自动生成序列化和反序列化代码
      • 维护成本低;只维护proto文件
      • 向后兼容;不必破坏旧格式
      • 加密性好
    • 跨语言
      • 跨平台
      • 支持各种主流语言
  • 缺点
    • 通用性差;Json可以任何语言都支持,但是protobuf需要专门的解析库
    • 自解释性差;只有通过proto文件才能了解数据结构;这一点是源自于它的加密性好,才导致的自解释性差;加密性和自解释性是相悖的

protobuf和json直观对比

helloworld.proto

syntax = "proto3";
package hello;
option go_package = "ShopBefore/rpc/helloword/proto;hello";

message HelloRequest{
  string name = 1;
  int32 age = 2;
  repeated string courses = 3;
}

使用命令生成go代码:

protoc -I=. --go_out=paths=source_relative:. helloworld.proto

helloworld.pb.go

// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// 	protoc-gen-go v1.27.1
// 	protoc        v3.19.4
// source: helloworld.proto

package hello

import (
	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)
)

type HelloRequest 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"`
	Courses []string `protobuf:"bytes,3,rep,name=courses,proto3" json:"courses,omitempty"`
}

func (x *HelloRequest) Reset() {
	*x = HelloRequest{}
	if protoimpl.UnsafeEnabled {
		mi := &file_helloworld_proto_msgTypes[0]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

func (x *HelloRequest) String() string {
	return protoimpl.X.MessageStringOf(x)
}

func (*HelloRequest) ProtoMessage() {}

func (x *HelloRequest) ProtoReflect() protoreflect.Message {
	mi := &file_helloworld_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 HelloRequest.ProtoReflect.Descriptor instead.
func (*HelloRequest) Descriptor() ([]byte, []int) {
	return file_helloworld_proto_rawDescGZIP(), []int{0}
}

func (x *HelloRequest) GetName() string {
	if x != nil {
		return x.Name
	}
	return ""
}

func (x *HelloRequest) GetAge() int32 {
	if x != nil {
		return x.Age
	}
	return 0
}

func (x *HelloRequest) GetCourses() []string {
	if x != nil {
		return x.Courses
	}
	return nil
}

var File_helloworld_proto protoreflect.FileDescriptor

var file_helloworld_proto_rawDesc = []byte{
	0x0a, 0x10, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x70, 0x72, 0x6f,
	0x74, 0x6f, 0x12, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x22, 0x4e, 0x0a, 0x0c, 0x48, 0x65, 0x6c,
	0x6c, 0x6f, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d,
	0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x10, 0x0a,
	0x03, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x61, 0x67, 0x65, 0x12,
	0x18, 0x0a, 0x07, 0x63, 0x6f, 0x75, 0x72, 0x73, 0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x09,
	0x52, 0x07, 0x63, 0x6f, 0x75, 0x72, 0x73, 0x65, 0x73, 0x42, 0x26, 0x5a, 0x24, 0x53, 0x68, 0x6f,
	0x70, 0x42, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x68, 0x65, 0x6c, 0x6c,
	0x6f, 0x77, 0x6f, 0x72, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x68, 0x65, 0x6c, 0x6c,
	0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var (
	file_helloworld_proto_rawDescOnce sync.Once
	file_helloworld_proto_rawDescData = file_helloworld_proto_rawDesc
)

func file_helloworld_proto_rawDescGZIP() []byte {
	file_helloworld_proto_rawDescOnce.Do(func() {
		file_helloworld_proto_rawDescData = protoimpl.X.CompressGZIP(file_helloworld_proto_rawDescData)
	})
	return file_helloworld_proto_rawDescData
}

var file_helloworld_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_helloworld_proto_goTypes = []interface{}{
	(*HelloRequest)(nil), // 0: hello.HelloRequest
}
var file_helloworld_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_helloworld_proto_init() }
func file_helloworld_proto_init() {
	if File_helloworld_proto != nil {
		return
	}
	if !protoimpl.UnsafeEnabled {
		file_helloworld_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*HelloRequest); 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_helloworld_proto_rawDesc,
			NumEnums:      0,
			NumMessages:   1,
			NumExtensions: 0,
			NumServices:   0,
		},
		GoTypes:           file_helloworld_proto_goTypes,
		DependencyIndexes: file_helloworld_proto_depIdxs,
		MessageInfos:      file_helloworld_proto_msgTypes,
	}.Build()
	File_helloworld_proto = out.File
	file_helloworld_proto_rawDesc = nil
	file_helloworld_proto_goTypes = nil
	file_helloworld_proto_depIdxs = nil
}

main

package main

import (
	hello "ShopBefore/rpc/helloworld/proto"
	"encoding/json"
	"fmt"
	"google.golang.org/protobuf/proto"
)

type Hello struct {
	Name string `json:"name"`
	Age int32 `json:"age"`
	Courses []string `json:"courses"`
}

func main() {
	protoRequest := hello.HelloRequest{
		Name: "番茄炒蛋",
		Age: 24,
		Courses: []string{"aaa","bbb"},
	}
	jsonRequest := Hello{
		Name: "番茄炒蛋",
		Age: 24,
		Courses: []string{"aaa","bbb"},
	}
	protoMarshal, _ := proto.Marshal(&protoRequest)
	jsonMarshal, _ := json.Marshal(jsonRequest)
	fmt.Println("protoMarshal: ",len(protoMarshal))
	fmt.Println("jsonMarshal: ",len(jsonMarshal))
}

// protoMarshal:  26
// jsonMarshal:  56

从序列化的结果上来看有两倍以上的差异;差异还是比较明显的.压缩的体积较小,性能上也会更快.

你可能感兴趣的:(grpc入门和进阶,json,golang,开发语言,protobuf)