Replies: 13 comments 4 replies
-
语言指南 (proto 2) |
Beta Was this translation helpful? Give feedback.
-
语言指南 (proto 3)定义一个消息类型syntax = "proto3";
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 results_per_page = 3;
}
1.1 指定字段类型1.2 分配字段编号1.3 指定字段标签
Mapsprotobuf创建map语法
|
Beta Was this translation helpful? Give feedback.
-
Protobuf风格指南原则:尊重现有风格,关键是一致性 1. 文件格式
2. 文件结构
3. Packages
4. Message 和 field 名
5. Repeated 字段使用复数名作为 rpeated 字段名 repeated string keys = 1;
...
repeated MyMessage accounts = 17; 6. Enums 枚举
enum FooBar {
FOO_BAR_UNSPECIFIED = 0;
FOO_BAR_FIRST_VALUE = 1;
FOO_BAR_SECOND_VALUE = 2;
} 7. Services 服务
service FooService {
rpc GetSomething(GetSomethingRequest) returns (GetSomethingResponse);
rpc ListSomething(ListSomethingRequest) returns (ListSomethingResponse);
} 8. 要避免的注意项
|
Beta Was this translation helpful? Give feedback.
-
Enum 枚举 |
Beta Was this translation helpful? Give feedback.
-
Encoding 编码编码描述了protobuf wire格式,它定义了如何在连接上发送消息以及它在磁盘上消耗多少空间的详细信息。理解protobuf编码知识有利于进行优化。 Protoscope是一种非常简单的语言,用于描述底层wire格式的片段,它可以为各种消息的编码提供可视化参考。 |
Beta Was this translation helpful? Give feedback.
-
Techniques |
Beta Was this translation helpful? Give feedback.
-
第三方插件 |
Beta Was this translation helpful? Give feedback.
-
Field Presence |
Beta Was this translation helpful? Give feedback.
-
Proto 最佳实践
|
Beta Was this translation helpful? Give feedback.
-
API 最佳实践 |
Beta Was this translation helpful? Give feedback.
-
jsonpbjsonpb包提供了PB消息和JSON之间封送和解封送的功能。 索引
|
Beta Was this translation helpful? Give feedback.
-
protojson封送和解封protobuf消息为 JSON 格式。原理遵循规范: https://developers.google.com/protocol-buffers/docs/proto3#JSON 索引 |
Beta Was this translation helpful? Give feedback.
-
golang/protoPackage proto provides functions operating on protocol buffer messages. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Protobuf
PB 是一种语言无关、平台无关 用于序列化结构化数据的可扩展机制
Protobuf提供了一种语言无关、平台无关、可扩展的机制,以向前兼容和向后兼容的方式序列化结构化数据。它类似于JSON,只不过它更小、更快,而且它生成原生的语言绑定。你只需要定义一次你想要的数据结构,然后你就可以很容易的生成读写数据的各种语言代码
Protobuf是定义语言,proto编译器生成数据访问、语言特定运行时库和文件(或网络)上的数据序列化代码
PB 解决了什么问题?
Protobuf为大小不超过几兆字节的类型化、结构化的数据包提供了一种序列化格式。这种格式既适合短期的网络流量,也适合长期的数据存储。可以使用新信息扩展Protobuf,而不会使现有数据无效或要求更新代码。
Protobuf的消息和服务定义在
.proto
文件中,如下是一个消息定义示例:proto编译器构建时通过读取
.proto
文件的定义生成目标语言代码。生成代码包含字段访问器和数据的序列化和解析方法。如下是生成方法的使用示例:protobuf 保持向后兼容性至关重要。protobuf允许在不破坏现有服务的情况下无缝支持任何更改,包括添加新字段和删除现有字段。有关此主题的更多信息,请参阅Updating Proto Definitions Without Updating Code。
PB 使用的好处是什么?
优点
跨语言兼容
用任何受支持的编程语言编写的代码都可以读取相同的消息。
跨项目支持
更新PB原型定义而不更新代码
PB不适合做什么
谁使用PB?
很多项目在使用,例如这些:
PB 是如何工作的?
PB 定义语法
更多的数据类型支持
PB 开源哲学
PB 的历史
开发者社区
其他资源
Beta Was this translation helpful? Give feedback.
All reactions