Toggle Navigation
首页
Golang
PHP
Laravel
Magento
Linux
数据库
MySQL
Redis
Mongodb
热门知识
程序与生活
架构师之路
静态站点
GitHub
主页
Protocol Buffer数据类型
Protocol Buffer数据类型
07-23
Mark
823
先来看一个已经编辑好的proto文件article.proto: ```go syntax = "proto3"; package article; message articleInfo { int64 id = 1; string title = 2; string slug = 3; string summary = 4; string body = 5; string image = 6; int64 views = 7; int32 user_id = 8; string created_at = 9; string updated_at = 10; } message articleList { repeated articleInfo list = 1; } ``` #### Protobuf消息定义: 消息由至少一个字段组合而成,类似于C语言中的结构。每个字段都有一定的格式。 字段格式:限定修饰符① | 数据类型② | 字段名称③ | = | 字段编码值④ **1、限定修饰符:** - Required: 表示是一个必须字段,必须相对于发送方,在发送消息之前必须设置该字段的值,对于接收方,必须能够识别该字段的意思。发送之前没有设置required字段或者无法识别required字段都会引发编解码异常,导致消息被丢弃。 - Optional:表示是一个可选字段,可选对于发送方,在发送消息时,可以有选择性的设置或者不设置该字段的值。对于接收方,如果能够识别可选字段就进行相应的处理,如果无法识别,则忽略该字段,消息中的其它字段正常处理。---因为optional字段的特性,很多接口在升级版本中都把后来添加的字段都统一的设置为optional字段,这样老的版本无需升级程序也可以正常的与新的软件进行通信,只不过新的字段无法识别而已,因为并不是每个节点都需要新的功能,因此可以做到按需升级和平滑过渡。 - Repeated:表示该字段可以包含0~N个元素。其特性和optional一样,但是每一次可以包含多个值。可以看作是在传递一个数组的值。 - 注:在proto3中字段规则移除了 “required”,并把 “optional” 改名为 “singular” **2、 数据类型:** | .proto Type | Notes | Go Type | PHP Type | | ------------ | ------------ | ------------ | ------------ | | double | | float64 | float | | float | | float32 | float | | int32 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. | int32 | integer | | int64 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. | int64 | integer/string | | uint32 | Uses variable-length encoding. | uint32 | integer | | uint64 | Uses variable-length encoding. | uint64 | integer/string | | sint32 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. | int32 | integer | | sint64 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. | int64 | integer/string | | fixed32 | Always four bytes. More efficient than uint32 if values are often greater than 228. | uint32 | integer | | fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 256. | uint64 | integer/string | | sfixed32 | Always four bytes | int32 | integer | | sfixed64 | Always four bytes | int64 | integer/string | | bool | | bool | boolean | | string | A string must always contain UTF-8 encoded or 7-bit ASCII text. | string | string | | bytes | May contain any arbitrary sequence of bytes. | []byte | string | **3、字段名称:** 字段名称的命名与C、C++、Java等语言的变量命名方式几乎是相同的。protobuf建议字段的命名采用以下划线分割的驼峰式。例如 first_name 而不是firstName。 **4、字段编码值:** 有了该值,通信双方才能互相识别对方的字段。当然相同的编码值,其限定修饰符和数据类型必须相同。编码值的取值范围为 1~2^32(4294967296)。其中 1~15的编码时间和空间效率都是最高的,编码值越大,其编码的时间和空间效率就越低(相对于1-15),当然一般情况下相邻的2个值编码效率的是相同的,除非2个值恰好实在4字节,12字节,20字节等的临界区。比如15和16。 protobuf 还建议把经常要传递的值把其字段编码设置为1-15之间的值。消息中的字段的编码值无需连续,只要是合法的,并且不能在同一个消息中有字段包含相同的编码值。
最新文章
Golang内存问题处理
WebSocket详解
Gopher面试中的Coding(三)
Golang用os/signal包实现平滑重载配置文件
Nginx实现负载均衡的几种方式
Golang中子goroutine与主协程同步的四种实现
Dockerfile文件详解
Golang开发微信公众号
最热文章
Magento实现下载csv数据表格
1797
Laravel带有条件搜索的分页
1692
Magento上传文件、缩略图和导出CSV
1690
Magento添加Advanced Dataflow
1641
Golang开发微信公众号
1570
Magento后台添加rule规则
1543
Magento优化 – MySQL读写分离
1509
Magento常用模块分享
1401
热门标签
下载
laravel分页
上传
导出
Dataflow
微信公众号
rule
规则
读写分离
模块
shell
正则
Collection
binlog
命名空间
处理
内存泄漏
Git
string
composer
解锁
锁表
事务处理
函数
redis sentinel
架构
系统
time
package
读写
事务处理
日志分析
进阶
缓存
golang
表分割
schedule
fmt
事务
分布式锁
交叉编译
HTTPS
日志分割
Gopher面试
mongodb
Dockerfile
channel
多数据库
git服务器
gogs
重载配置
负载均衡
日志过滤
php
protobuf
发展方向
版本号
模型关系
goroutine
concat
json
mongodb
get
post
defer
panic
golang算法
算法
docker
elasticsearch
rpc
代码部署
redis
主从配置
写文件
protobuf
laravel
websocket
并发
量级
package
curl
生成插件
直接下载
数据类型
Shell
错误传递
成长
grpc
参数
Go指针
队列
protobuf
方法
Redis持久化
路上
风景
yum