当前位置:网站首页>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
边栏推荐
- On the difference between FPGA and ASIC
- Chain storage of stack
- New Year Fireworks code plus copy, are you sure you don't want to have a look
- 盘点国内有哪些EDA公司?
- Dc-7 target
- PTA ladder game exercise set l2-002 linked list de duplication
- yarn入门(一篇就够了)
- From "running distractor" to data platform, Master Lu started the road of evolution
- 改变ui组件原有样式
- MySQL performance_ Schema common performance diagnosis query
猜你喜欢
Question 102: sequence traversal of binary tree
JVM命令之 jstat:查看JVM统计信息
If you don't know these four caching modes, dare you say you understand caching?
Say sqlyog deceived me!
生活中的开销,怎么记账合适
[InstallShield] Introduction
C. colonne Swapping [tri + Simulation]
404 not found service cannot be reached in SAP WebService test
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略
Dc-7 target
随机推荐
[FPGA tutorial case 14] design and implementation of FIR filter based on vivado core
What is make makefile cmake qmake and what is the difference?
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
[FPGA tutorial case 13] design and implementation of CIC filter based on vivado core
Web authentication API compatible version information
每秒10W次分词搜索,产品经理又提了一个需求!!!(收藏)
STM32按键状态机2——状态简化与增加长按功能
Apple CMS V10 template /mxone Pro adaptive film and television website template
Change the original style of UI components
SubGHz, LoRaWAN, NB-IoT, 物联网
SQL Server 2008 各种DateTime的取值范围
成为资深IC设计工程师的十个阶段,现在的你在哪个阶段 ?
Why does the data center need a set of infrastructure visual management system
C. colonne Swapping [tri + Simulation]
Jstat pour la commande JVM: voir les statistiques JVM
JVM命令之- jmap:导出内存映像文件&内存使用情况
OpenSergo 即将发布 v1alpha1,丰富全链路异构架构的服务治理能力
How to improve website weight
C note 13
原生小程序 之 input切換 text與password類型