当前位置:网站首页>[micro service series] protocol buffer dynamic analysis
[micro service series] protocol buffer dynamic analysis
2022-06-26 06:23:00 【shanxiaoshuai】
In recent work grpc. The previous work was based on thrift The microservice framework of , Yes grpc Not very familiar with , Only know grpc It's based on http2 and protobuf Of rpc frame . But the methods of use are similar , be based on idl Generate the corresponding file , On the server side, you can achieve specific service And provide services to the outside world , For the client, you need to introduce client Package initiation rpc call .
Here's the problem , To invoke downstream services, you need to introduce downstream services client, If downstream Services idl If something changes, we need to update it in the code client package , Recompile deployment release . In normal business development, there is no problem with this mode , The rhythm of release and deployment follows the rhythm of business iteration . But in some scenarios, there may be big problems , For example, a gateway service , Many services are registered on the gateway , Gateway through rpc To invoke business services . In this case, if every business change idl The gateway needs to be deployed with the release, which is obviously a big problem .
The above problems were investigated , It is found that dynamic parsing can be used idl Document the way to solve the problem . have access to github.com/jhump/protoreflect To achieve pb and grpc Reflection of . Originally mainly introduced pb Partial content .
List of articles
Code example
Establish a simple proto The documents are as follows , It defines a simple structure HelloReq and HelloResp.
syntax = "proto3";
package test;
option go_package = "data/";
message HelloReq {
int64 ID = 1;
string Name = 2;
}
message HelloResp {
int64 Code = 1;
string Message = 2;
}
According to the general practice , At this time, you should execute protoc --go_out=. ./test.proto Command to generate test.pb.go, Then use the structure to do the corresponding operation .
Let's use the above mentioned protoreflect Package for runtime parsing , The code is as follows .
package main
import (
"encoding/json"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/jhump/protoreflect/dynamic"
"grpc_practice/data"
"log"
)
func main() {
// establish parser object
p := protoparse.Parser{
}
// Use path Some columns of file description objects are obtained by parsing , There is only one file description object
fileDescs, err := p.ParseFiles("./test.proto")
if err != nil {
log.Printf("parse proto file failed, err = %s", err.Error())
return
}
// Get the message contempt object from the file description object according to the message name
helloReqDesc := fileDescs[0].FindMessage("test.HelloReq")
if helloReqDesc == nil {
log.Printf("no message matched")
return
}
// Generate dynamic messages according to the message description object
dMsg := dynamic.NewMessage(helloReqDesc)
// Use this dynamic object to start from GetHelloReq Simulated json Object decoding
_ = dMsg.UnmarshalJSON(GetHelloReq())
log.Printf("req ID = %d, req Name = %s", dMsg.GetFieldByNumber(1), dMsg.GetFieldByNumber(2))
}
func GetHelloReq() []byte {
req := data.HelloReq{
}
req.ID = 1
req.Name = "xiaoshuai"
d, _ := json.Marshal(&req)
return d
}
Principle analysis
above demo It's a little bit easier , Looking at the code above, it is not difficult to find that there are two core objects , One is descriptor The descriptor , It corresponds to proto Objects at different levels in the file ; Two is dynamicMessage Dynamic news , It replaces pb.go Message body in file .
In a nutshell ,descriptor It's parsing proto file , And organize and hold in the form of data structure proto Information in the file .dynamicMessage hold MessageDescriptor object , It is equivalent to knowing all the information of the structure .
Descriptor
The relationships of descriptors at different levels are as follows , There is nothing to go into detail .
dynamicMessage
// Message is a dynamic protobuf message. Instead of a generated struct,
// like most protobuf messages, this is a map of field number to values and
// a message descriptor, which is used to validate the field values and
// also to de-serialize messages (from the standard binary format, as well
// as from the text format and from JSON).
type Message struct {
md *desc.MessageDescriptor
er *ExtensionRegistry
mf *MessageFactory
extraFields map[int32]*desc.FieldDescriptor
values map[int32]interface{
}
unknownFields map[int32][]UnknownField
}
dynamic message The source code is as follows , The main object to see is message descriptor, hold md object dynamic message You have all the information in one message , For example, field name 、 Serial number 、 Type, etc . Then its value is stored in values This map Field , Its value is expressed in interface{} Deposit , So there will be a lot of reflection when using , I will add some later benchmark, Observe its performance .
边栏推荐
- [alluxio & Dachang] the original boss direct employment was applied in this way
- University Information Management System
- Several promotion routines of data governance
- How to select and build a real-time data warehouse scheme
- Volatile application scenarios
- Gof23 - abstract factory pattern
- Install pyinstaller
- Usage of zip (*arg)
- Tencent's 2022 school recruitment of large factories started with salary, and the general contracting of cabbage is close to 40W!
- Typescript type
猜你喜欢

Message queuing - omnidirectional comparison

Keepalived to achieve high service availability
Customer Stories | Netease spring breeze: the "spring breeze" of the fun industry, reaching out to all areas through in-depth interaction

Thread status and stop

TCP connection and disconnection, detailed explanation of state transition diagram

Deeply uncover Ali (ant financial) technical interview process with preliminary preparation and learning direction
The sysdig 2022 cloud native security and usage report found that more than 75% of the running containers have serious vulnerabilities

Go语言学习笔记 1.1

Tencent WXG internship experience (has offered), I hope it will help you!

MySQL-09
随机推荐
Message queue - function, performance, operation and maintenance comparison
EFK昇級到ClickHouse的日志存儲實戰
Volatile application scenarios
The sysdig 2022 cloud native security and usage report found that more than 75% of the running containers have serious vulnerabilities
Handwritten background management framework template (I)
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
数据治理工作的几种推进套路
Lamda expression
DS18B20详解
Evolution history of qunar Bi platform construction
Keepalived to achieve high service availability
Go学习笔记1.3-变量的数据类型篇
Design and practice of low code real-time data warehouse construction system
浏览器的四大内核:Trident,Gecko,Webkit,Blink
Market trend report, technical innovation and market forecast of microencapsulated chemical pesticides in China
TCP连接与断开,状态迁移图详解
消息队列-全方位对比
Installing rainbow in various kubernetes with Helm
Requirement analysis of personal blog system
Self attention and multi head self attention (MSA) in transformer