当前位置:网站首页>Protobuf data exchange format
Protobuf data exchange format
2022-07-28 13:11:00 【Love in Nanxin】
brief introduction
ProtoBuf(Google Protocol Buffer) By google Sequential structured data formats used by companies for data exchange , With cross platform 、 Cross language 、 Scalability , The type is suitable for the commonly used XML And JSON,
But with a smaller transport volume 、 Higher coding 、 Decoding ability , Especially suitable for data storage 、 Network data transmission and so on to the storage volume 、 Areas with high real-time requirements . With .proto For the suffix , Has its own compiler protoc, protoc2 and protoc3 edition .
Simple understanding : comparison json More efficient , Efficient serialization and deserialization , A data exchange format that can compress data into smaller sizes ( For the time being, it is simply understood as more efficient and powerful json).gRPC The default is based on protobuf Data format for data exchange , therefore
protobuf You must learn before you become gRPC Prepare for learning . Write it well .protoc After file , Use protoc And according to the input parameters , Select the type of language to convert , Can be converted into the code of various programming languages .
protobuf Examples
Next, there is no need to go over the grammar word by word , I pass the commonly used grammar through a protobuf Documents can be elaborated , Make a summary . Just go to the official for detailed documents . I'm sure to learn in half an hour ,
It's not as complicated as expected , He Xue json almost . The details are as follows :
// notes : 1. // Single-line comments 2. /* */ Multiline comment
syntax = "proto3"; // Use... Explicitly protobuf edition Do not write by default It is proto2
package com.test; // Definition package package
// Official documents : https://developers.google.com/protocol-buffers/docs/proto3
import "./pk2.proto"; // Import protoW file
option go_package = "proto"; // If you use protoc Compiled into java Code Then generate according to the package name Ignore com.test
option java_package = "com.hello"; // If it is compiled as go Code According to com.hello Package name generation
// Definition message Information
message Keyword {
string value = 1;
}
// Define a message message Parameters
message QueryData{
// Basic data type field definitions Basic data types can cover string bool int double bytes wait
string name = 1;
string orderBy = 2;
double weight = 3;
float searchWeight = 4;
int32 type = 5;
uint64 id = 6;
fixed32 type2 = 7;
fixed64 type3 = 8;
bool isOk = 9;
bytes passWord = 10;
// Define an array similar []string
repeated string cases = 11;
// Definition map type string=>string
map <string,string> myMap = 15;
// oneof Optional fields There can only be one or 0 individual
oneof OptionField {
string selectWord = 12;
string queryType = 13;
}
// Define enumeration types
enum WeekDay {
SUNDAY = 0; // Enumeration type The first value must be 0
MONDAY = 1;
TUESDAY = 2;
}
WeekDay selectDay = 14;
// keyword Compound field type Quote the above defined Keyword type
Keyword kw = 16;
}
// Define interface return message Format
message QueryResponse {
int32 code = 1;
string msg = 2;
string htmlData = 3;
}
// Definition rpc service Generally refer to gRPC More
service QueryApi {
// 1.grpc One yuan in rpc Interface Very common It can be understood as normal http The interface is almost
rpc Query (QueryData) returns (QueryResponse){
};
// 2. Server based " streaming "rpc Interface After establishing the connection The server can send data to the client
rpc ServerQueryStyle(QueryData) returns (stream QueryResponse) {
};
// 3. Server based " streaming "rpc Interface Different from the first one This way is based on " flow " transmission Instead of sending a request response and then disconnecting
rpc ClientQueryStyle(stream QueryData) returns (QueryResponse) {
};
// 4. client-based , Server side " Two way flow "rpc Interface Full duplex communication Both parties can send data to each other
rpc ClientAndServerStreamQuery(stream QueryData) returns (stream QueryResponse){
};
}
边栏推荐
- How to open the power saving mode of win11 system computer
- Code layered management of interface testing based on RF framework
- The largest rectangle in leetcode84 histogram
- 机器学习实战-神经网络-21
- CTO of youhaoda, MVP of Huawei cloud, and Zhang Shanyou: build cloud native applications based on kubernetes and dapr
- [embedded C foundation] Part 9: basic usage of C language pointer
- Which big model is better? Openbmb releases bmlist to give you the answer!
- 【嵌入式C基础】第5篇:原码/反码/补码
- 机器学习基础-贝叶斯分析-14
- 机器学习基础-主成分分析PCA-16
猜你喜欢

Vs code is not in its original position after being updated

LeetCode每日一题(2196. Create Binary Tree From Descriptions)

Machine learning practice - integrated learning-23

Cloud native - runtime environment

03 pyechars rectangular coordinate system chart (example code + effect drawing)

Merge sort

Brother bird talks about cloud native security best practices

Machine learning Basics - integrated learning-13

Leetcode94. Middle order traversal of binary trees

CTO of youhaoda, MVP of Huawei cloud, and Zhang Shanyou: build cloud native applications based on kubernetes and dapr
随机推荐
[embedded C foundation] Part 9: basic usage of C language pointer
[graduation design] smart home system based on ZigBee - single chip microcomputer Internet of things stm32
Detailed explanation of the usage of C # static
Transaction of MySQL underlying principle (2)
[FPGA]: Joint Simulation of FPGA and MATLAB
How to open the power saving mode of win11 system computer
03 pyechars rectangular coordinate system chart (example code + effect drawing)
Review the IO stream again, and have an in-depth understanding of serialization and deserialization
机器学习实战-决策树-22
What is C generic, generic cache, generic constraint
Summary: idea problem record
Full disclosure! Huawei cloud distributed cloud native technology and Practice
Machine learning practice - neural network-21
A brief introduction to the for loop. Some of the code involves arrays
面试必问,敲重点!讲一下 Android Application 启动流程及其源码?
Application and download of dart 3D radiative transfer model
Leetcode:704 binary search
[June 28 event preview] low code Summit
2020-12-13
Merge sort