当前位置:网站首页>go-microservice-simple(2) go-Probuffer

go-microservice-simple(2) go-Probuffer

2022-07-07 06:10:00 Linghu Feixia

1 Protocol buffers brief introduction

1.1 Protocol buffers Definition

Protocol Buffers Is a lightweight and efficient structured data storage format , Can be used for structured data serialization , Suitable for data storage or RPC Data exchange format . It can be used for communication protocol 、 Language independence in areas such as data storage 、 Platform independent 、 Extensible serialization structure data format .

1.2 Protocol buffers characteristic

characteristic :
It is a serialization format , and JSON,XML similar .
It's binary , High compression ratio , Short .
Serialization processing is fast
Non plaintext transmission
It passes through a kind of proto Syntax description format , Multiple language codes can be generated through tools
Support multiple development languages ,Java,Go,Python,JS etc. .

2 GO Protocol buffers Environmental Science

Configuration environment :
1 install protoc Tools
2 install protoc-gen-go.
Here is the introduction windows Environmental Science . If it is Linux The environment can also download the corresponding package installation . Download directly here exe file , Of course go get Post acquisition installation .

2.1 install protoc Tools

Download installation package address :
https://github.com/protocolbuffers/protobuf/releases
I downloaded it :
 Insert picture description here
There is bin/protoc , This protoc Command is used to compile and generate code .

View version :
 Insert picture description here

2.2 install protoc-gen-go Tools

protoc-gen-go yes Generate Go Code protocolbuffers compiler . It can be understood as a compiler plug-in , coordination protoc To use .

Download address :
https://github.com/protocolbuffers/protobuf-go/releases
I downloaded it :
 Insert picture description here
View version :
 Insert picture description here
Here is the protoc Tools 、protoc-gen-go Put the tools in the same folder , Also set environment variables differently .
 Insert picture description here

3 GO Protocol buffers Use

Use steps
1 Definition .proto file , Define packets during transmission .
2 compile .proto file , The generated code . The code used to compile the description file into the specified platform ( For example, generate GO Source code file ).
3 obtain go Class library / package , Implement coding , Decoding, etc .

3.1 Definition person.proto file

Definition person.proto file :

syntax="proto3";

option go_package="./;model";
package model;

message Person {
    
    string name = 1;
    int32 age = 2;
}

person.proto Put the file in protoc Tools Directory .
 Insert picture description here

3.2 compile person.proto file

Enter the command :protoc --go_out=. *.proto
 Insert picture description here
I'm going to generate the corresponding go file :person.pb.go.

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

package model

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 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_person_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_person_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_person_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_person_proto protoreflect.FileDescriptor

var file_person_proto_rawDesc = []byte{
    
	0x0a, 0x0c, 0x70, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05,
	0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x22, 0x2e, 0x0a, 0x06, 0x50, 0x65, 0x72, 0x73, 0x6f, 0x6e, 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, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x6d, 0x6f, 0x64, 0x65,
	0x6c, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var (
	file_person_proto_rawDescOnce sync.Once
	file_person_proto_rawDescData = file_person_proto_rawDesc
)

func file_person_proto_rawDescGZIP() []byte {
    
	file_person_proto_rawDescOnce.Do(func() {
    
		file_person_proto_rawDescData = protoimpl.X.CompressGZIP(file_person_proto_rawDescData)
	})
	return file_person_proto_rawDescData
}

var file_person_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_person_proto_goTypes = []interface{
    }{
    
	(*Person)(nil), // 0: model.Person
}
var file_person_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_person_proto_init() }
func file_person_proto_init() {
    
	if File_person_proto != nil {
    
		return
	}
	if !protoimpl.UnsafeEnabled {
    
		file_person_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_person_proto_rawDesc,
			NumEnums:      0,
			NumMessages:   1,
			NumExtensions: 0,
			NumServices:   0,
		},
		GoTypes:           file_person_proto_goTypes,
		DependencyIndexes: file_person_proto_depIdxs,
		MessageInfos:      file_person_proto_msgTypes,
	}.Build()
	File_person_proto = out.File
	file_person_proto_rawDesc = nil
	file_person_proto_goTypes = nil
	file_person_proto_depIdxs = nil
}

3.3 Project use person.proto file

Introduced in the project person.pb.go file :
 Insert picture description here
Code first :

	fmt.Println("-------------- code begin--------------")
	var p = &model.Person{
    
		Name: " linghu chong ",
		Age:  40,
	}
	data,err:= proto.Marshal(p)
	if err!=nil{
    
		fmt.Println(" Serialization error :", err.Error())
	}
	fmt.Println(" Serialization successful .",data)
	fmt.Println("-------------- code end--------------")

The operation results are as follows :
 Insert picture description here
Then decode :

	fmt.Println("-------------- decode begin--------------")
	newPerson := model.Person{
    }
	err = proto.Unmarshal(data, &newPerson)
	if err!=nil{
    
		fmt.Println(" Deserialization error :", err.Error())
	}
	fmt.Println(" Deserialization successful :")
	fmt.Println("name:",newPerson.Name)
	fmt.Println("-------------- decode end--------------")

The operation results are as follows :
 Insert picture description here

3.4 Problems encountered

problem 1:Protoc Command error :

protoc-gen-go: unable to determine Go import path for "hello.proto"

Please specify either:
        • a "go_package" option in the .proto source file, or
        • a "M" argument on the command line.

solve : add to option go_package="./;hello";
explain
option go_package = “path;name”;
path Represents generated go File storage address , The directory will be automatically generated .

problem 2: Can't find google.golang.org/protobuf/encoding/prototext .
solve :https://github.com/protocolbuffers/protobuf-go Download the project to $gopath Corresponding src Under the folder .
 Insert picture description here

The code can be found in the :
https://gitee.com/alifeidao/go-mircroservice-simple/tree/master/chapter2

原网站

版权声明
本文为[Linghu Feixia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130720175492.html