当前位置:网站首页>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 :
There is bin/protoc , This protoc Command is used to compile and generate code .
View version :
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 :
View version :
Here is the protoc Tools 、protoc-gen-go Put the tools in the same folder , Also set environment variables differently .
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 .
3.2 compile person.proto file
Enter the command :protoc --go_out=. *.proto
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 :
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 :
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 :
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 .
The code can be found in the :
https://gitee.com/alifeidao/go-mircroservice-simple/tree/master/chapter2
边栏推荐
- [cloud native] what is the microservice architecture?
- SubGHz, LoRaWAN, NB-IoT, 物联网
- Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
- 每秒10W次分词搜索,产品经理又提了一个需求!!!(收藏)
- [solved] record an error in easyexcel [when reading the XLS file, no error will be reported when reading the whole table, and an error will be reported when reading the specified sheet name]
- Red hat install kernel header file
- Flask 1.1.4 werkzeug1.0.1 analyse du code source: processus de démarrage
- STM32按键状态机2——状态简化与增加长按功能
- 驱动开发中platform设备驱动架构详解
- SAP ABAP BDC (batch data communication) -018
猜你喜欢

10W word segmentation searches per second, the product manager raised another demand!!! (Collection)

Question 102: sequence traversal of binary tree

Introduction to yarn (one article is enough)

Career experience feedback to novice programmers

SAP ABAP BDC (batch data communication) -018

Jstat of JVM command: View JVM statistics

Chain storage of stack

cf:C. Column Swapping【排序 + 模擬】

PowerPivot - DAX (function)

Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
随机推荐
Jmeter自带函数不够用?不如自己动手开发一个
Red Hat安装内核头文件
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
EMMC print cqhci: timeout for tag 10 prompt analysis and solution
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
Go language learning notes - Gorm use - Gorm processing errors | web framework gin (10)
Bypass open_ basedir
产业金融3.0:“疏通血管”的金融科技
Question 102: sequence traversal of binary tree
Input of native applet switches between text and password types
MFC BMP sets the resolution of bitmap, DPI is 600 points, and gdiplus generates labels
关于STC单片机“假死”状态的判别
How much do you know about clothing ERP?
Flask 1.1.4 werkzeug1.0.1 analyse du code source: processus de démarrage
From "running distractor" to data platform, Master Lu started the road of evolution
SAP Spartacus checkout 流程的扩展(extend)实现介绍
STM32 key state machine 2 - state simplification and long press function addition
yarn入门(一篇就够了)
Swagger3 configuration
Detailed explanation of platform device driver architecture in driver development