当前位置:网站首页>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){
};
}
边栏推荐
- DART 三维辐射传输模型申请及下载
- Phpstudy steps to quickly build a website (teach you to build it by hand)
- Leetcode206 reverse linked list
- RGB game atmosphere light touch chip-dlt8s04a-jericho
- Cloud native - runtime environment
- Ruan Bonan of Green Alliance Technology: cloud native security from the open source shooting range
- Full disclosure! Huawei cloud distributed cloud native technology and Practice
- Stepless dimming colorful RGB mirror light touch chip-dlt8s12a-jericho
- How to open the power saving mode of win11 system computer
- 机器学习实战-集成学习-23
猜你喜欢

Aragon创建DAO polygon BSC测试网

How does kotlin help you avoid memory leaks?

【嵌入式C基础】第1篇:基本数据类型

Introduction to border border attribute

Change the document type in endnode and import it in word

How can non-standard automation equipment enterprises do well in product quality management with the help of ERP system?

Ccf201912-2 recycling station site selection

Linear classifier (ccf20200901)

Brief introduction to JS operator

Leetcode94. Middle order traversal of binary trees
随机推荐
Block reversal (summer vacation daily question 7)
【嵌入式C基础】第2篇:进制转换与BCD编码
Leetcode daily question (2196. create binary tree from descriptions)
[basic teaching of Bi design] detailed explanation of OLED screen use - single chip microcomputer Internet of things
【嵌入式C基础】第1篇:基本数据类型
Sub thread update UI full solution
A brief introduction to the for loop. Some of the code involves arrays
How does kotlin help you avoid memory leaks?
Brief introduction to JS operator
2020-12-13
Machine learning practice - logistic regression-19
Comments are not allowed in JSON
CTO of youhaoda, MVP of Huawei cloud, and Zhang Shanyou: build cloud native applications based on kubernetes and dapr
Change the document type in endnode and import it in word
Interview must ask, focus! Tell me about the Android application startup process and its source code?
[graduation design] smart home system based on ZigBee - single chip microcomputer Internet of things stm32
如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
Black cat takes you to learn EMMC Protocol Part 26: hardware reset operation of EMMC (h/w reset)
.NET的求复杂类型集合的差集、交集、并集
Leetcode206 reverse linked list