1. syntax = "proto3";
    文件的第一行指定了你使用的是proto3的语法:如果你不指定,protocol buffer 编译器就会认为你使用的是proto2的语法。这个语句必须出现在.proto文件的非空非注释的第一行。
  2. message SearchRequest {......}
    message 定义实体
  3. 基本数据类型
  4. 注释符号: 双斜线,如://xxxxxxxxxxxxxxxxxxx
  5. 字段唯一数字标识(用于在二进制格式中识别各个字段,上线后不宜再变动):Tags
    1到15使用一个字节来编码,包括标识数字和字段类型(你可以在Protocol Buffer 编码中查看更多详细);16到2047占用两个字节。因此定义proto文件时应该保留1到15,用作出现最频繁的消息类型的标识。记得为将来会继续增加并可能频繁出现的元素留一点儿标识区间,也就是说,不要一下子把1—15全部用完,为将来留一点儿。
    标识数字的合法范围:最小是1,最大是 229 - 1,或者536,870,911。
    另外,不能使用19000 到 19999之间的数字(FieldDescriptor::kFirstReservedNumber through FieldDescriptor::kLastReservedNumber),因为它们被Protocol Buffers保留使用
  6. 字段修饰符:
    required:值不可为空
    optional:可选字段
    singular:符合语法规则的消息包含零个或者一个这样的字段(最多一个)
    repeated:一个字段在合法的消息中可以重复出现一定次数(包括零次)。重复出现的值的次序将被保留。在proto3中,重复出现的值类型字段默认采用压缩编码。你可以在这里找到更多关于压缩编码的东西: Protocol Buffer Encoding
    默认值: optional PhoneType type = 2 [default = HOME];
  7. 代理类生成
    1. C++, 每一个.proto 文件可以生成一个 .h 文件和一个 .cc 文件
    2. Java, 每一个.proto文件可以生成一个 .java 文件
    3. Python, 每一个.proto文件生成一个模块,其中为每一个消息类型生成一个静态的描述器,在运行时,和一个metaclass一起使用来创建必要的Python数据访问类
    4. Go, 每一个.proto生成一个 .pb.go 文件
    5. Ruby, 每一个.proto生成一个 .rb 文件
    6. Objective-C, 每一个.proto 文件可以生成一个 pbobjc.h 和一个pbobjc.m 文件
    7. C#, 每一个.proto文件可以生成一个.cs文件.
  8. 字段默认值
    1. strings, 默认值是空字符串(empty string)
    2. bytes, 默认值是空bytes(empty bytes)
    3. bools, 默认值是false
    4. numeric, 默认值是0
    5. enums, 默认值是第一个枚举值(value必须为0)
    6. message fields, the field is not set. Its exact value is langauge-dependent. See the generated code guide for details.
    7. repeated fields,默认值为empty,通常是一个空list
  9. 枚举

    第一个元素的value必须为0;

  10. Maps字段类型:map<key_type, value_type> map_field = N;
    1. map字段不能是repeated.
    2. Wire format ordering and map iteration ordering of map values is undefined, so you cannot rely on your map items being in a particular order.
    3. When generating text format for a .proto, maps are sorted by key. Numeric keys are sorted numerically.
    4. When parsing from the wire or when merging, if there are duplicate map keys the last key seen is used. When parsing a map from text format, parsing will fail if there are duplicate keys.
  11. 更新.proto文件
    当原有的.proto结构不能满足业务需求时,可以修改.proto文件,重新生成代理类,修改时需遵循以下原则:
    1. 不能修改已存在字段的唯一数字标识符(tags)
    2. If you add new fields, any messages serialized by code using your "old" message format can still be parsed by your new generated code. You should keep in mind the default values for these elements so that new code can properly interact with messages generated by old code. Similarly, messages created by your new code can be parsed by your old code: old binaries simply ignore the new field when parsing. Note that unknown fields are discarded when the message is deserialized, so if the message is passed on to new code, the new fields will not still be available (this is different behaviour to proto2, where unknown fields are serialized along with the message).
    3. 对废弃的字段,它的tags不能被再次使用,可以不删除,而是给字段添加“OBSOLETE_ ”前缀,这样可以避免将来重新使用这个tags
    4. int32, uint32, int64, uint64, 和bool是完全兼容的,这些字段类型之间可以直接修改
    5. sint32 和 sint64 是兼容的,但是和其他的数字类型不兼容
    6. string 和 bytes 是兼容的,由于bytes也是 UTF-8格式
    7. Embedded messages are compatible with bytes if the bytes contain an encoded version of the message.
    8. fixed32 和 sfixed32兼容, fixed64和sfixed64兼容
    9. enum 和 int32, uint32, int64, and uint64 in terms of wire format (note that values will be truncated if they don't fit). However be aware that client code may treat them differently when the message is deserialized: for example, unrecognized proto3 enum types will be preserved in the message, but how this is represented when the message is deserialized is language-dependent. Int fields always just preserve their value.
  12. Packages支持
  13. 定义Services
  14. 其他Options:
    1. java_package(file option):option java_package = "com.example.foo";
    2. java_outer_classname(file option ):option java_outer_classname = "Ponycopter";
    3. optimize_for(file option ):值为SPEED, CODE_SIZE或LITE_RUNTIME;如:option optimize_for = CODE_SIZE;
        • SPEED (default): The protocol buffer compiler will generate code for serializing, parsing, and performing other common operations on your message types. This code is extremely highly optimized.
        • CODE_SIZE: The protocol buffer compiler will generate minimal classes and will rely on shared, reflection-based code to implement serialialization, parsing, and various other operations. The generated code will thus be much smaller than with SPEED, but operations will be slower. Classes will still implement exactly the same public API as they do in SPEED mode. This mode is most useful in apps that contain a very large number .proto files and do not need all of them to be blindingly fast.
        • LITE_RUNTIME: The protocol buffer compiler will generate classes that depend only on the "lite" runtime library (libprotobuf-lite instead of libprotobuf). The lite runtime is much smaller than the full library (around an order of magnitude smaller) but omits certain features like descriptors and reflection. This is particularly useful for apps running on constrained platforms like mobile phones. The compiler will still generate fast implementations of all methods as it does in SPEED mode. Generated classes will only implement the MessageLite interface in each language, which provides only a subset of the methods of the full Message interface.
    4. cc_enable_arenas (file option ): 生成C++代码时启用arena allocation.
    5. objc_class_prefix (file option ): 设置 Objective-C class前缀,which is prepended to all Objective-C generated classes and enums from this .proto. There is no default. You should use prefixes that are between 3-5 uppercase characters as recommended by Apple. Note that all 2 letter prefixes are reserved by Apple.
    6. deprecated (field option): 如果设置为true,则该字段被置为“deprecated”,在新的代码中不能被继续使用,In most languages this has no actual effect. In Java, this becomes a @Deprecated annotation. In the future, other language-specific code generators may generate deprecation annotations on the field's accessors, which will in turn cause a warning to be emitted when compiling code which attempts to use the field. If the field is not used by anyone and you want to prevent new users from using it, consider replacing the field declaration with a reserved statement.
      int32 old_field = 6 [deprecated=true];
 
示例:
 

 
 

 
 

GRpc-Proto3语法的更多相关文章

  1. Proto3语法翻译

    本文主要对proto3语法翻译.参考网址:https://developers.google.com/protocol-buffers/docs/proto3 defining a message t ...

  2. proto3语法记录

    protobuf 是谷歌的语言无关,平台无关,可扩展的,高效的结构化数据序列化机制,比xml和json的序列化的速度更快,此处记录一下 proto3 的语法,防止以后忘记. 注意:proto3 语法需 ...

  3. .net core 用grpc实现微服务

    GRPC 是Google发布的一个开源.高性能.通用RPC(Remote Procedure Call)框架.提供跨语言.跨平台支持.以下以.NET Core 使用控制台.docker中演示如何使用G ...

  4. Protobuf3语法详解

    定义一个消息类型 先来看一个非常简单的例子.假设你想定义一个"搜索请求"的消息格式,每一个请求含有一个查询字符串.你感兴趣的查询结果所在的页数,以及每一页多少条查询结果.可以采用如 ...

  5. Protobuf 语言指南(proto3)

    Protobuf 语言指南(proto3) Protocol Buffer是Google的语言中立的,平台中立的,可扩展机制的,用于序列化结构化数据 - 对比XML,但更小,更快,更简单.您可以定义数 ...

  6. 开始食用grpc(之一)

    开始食用grpc(之一) 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9501353.html ```   记一次和一锅们压马路,路过一咖啡厅(某巴克),随口 ...

  7. Protobuf3 语法指南

    目录 [−] 定义一个消息类型 指定字段类型 分配标识号 指定字段规则 添加更多消息类型 添加注释 保留标识符(Reserved) 从.proto文件生成了什么? 标量数值类型 默认值 枚举 使用其他 ...

  8. protobuf3 语法解析

    定义一个消息类型 先来看一个非常简单的例子.假设你想定义一个“搜索请求”的消息格式,每一个请求含有一个查询字符串.你感兴趣的查询结果所在的页数,以及每一页多少条查询结果.可以采用如下的方式来定义消息类 ...

  9. gRPC入门

    一.gRPC简介 在介绍gRPC之前先说一下RPC(Remote Procedure Call),也叫远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.相比 ...

  10. 使用protobuf (proto3, C++和go语言)

    在这里,我先讲述C++使用protobuf,之后,会补充使用go语言使用protobuf. 使用protobuf需要有如下步骤: 在.proto文件中定义消息(message)格式. 使用protob ...

随机推荐

  1. day27-1 numpy模块

    目录 numpy array 一维数组 二维数组(用的最多) np.array和list的区别 获取多维数组的行和列 多维数组的索引 高级功能 多维数组的元素替换 多维数组的合并 通过函数方法创建多维 ...

  2. java调用第三方命令,process.waitfor()挂起(你不知道的坑)

    我们常在java中运行第三方程序,如sh.python,java提供一个Runtime.exec()方法,生成一个Process对象.今天在使用这个方法的时候,发现接口半天没有返回数据.查了一下,原来 ...

  3. LSTM比较RNN

    LSTM只能避免RNN的梯度消失(gradient vanishing),但是不能对抗梯度爆炸问题(Exploding Gradient). 梯度膨胀(gradient explosion)不是个严重 ...

  4. bzoj 1293: [SCOI2009]生日礼物 问题转化 + 性质分析 + 滚动数组优化

    Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩 ...

  5. luogu p3918[国家集训队]特技飞行 贪心

    开始没看出来是贪心,一度以为是动态规划,还是太弱了呀-.. 不难分析出,两个相同的飞行动作之间夹一个相同的动作是多余的,所以就贪心一下,按Ci从大到小排序,依次加到左右两端点,知道加不了为止. 代码: ...

  6. 算法46----移除K位数字

    一.题目:移除K位数字 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. num 不会包含任何前导零. 示 ...

  7. Python数据分析3------数据预处理

    一.数据清洗 这一个步骤可以和数据探索并行. (1)标签分类数据处理:LabelEncoder[将分类数据变成0-n的值] from sklearn.prepocessing import Label ...

  8. concurrent.futures 学习笔记

    concurrent.futures 先看下官方介绍 The asynchronous execution can be performed with threads, using ThreadPoo ...

  9. Code VS 1002 搭桥

    题目描述 Description 有一矩形区域的城市中建筑了若干建筑物,如果某两个单元格有一个点相联系,则它们属于同一座建筑物.现在想在这些建筑物之间搭建一些桥梁,其中桥梁只能沿着矩形的方格的边沿搭建 ...

  10. BZOJ 1197 [HNOI2006]花仙子的魔法 (数学题)

    题面:洛谷传送门 BZOJ传送门 非常有意思的一道数学题,浓浓的$CF$风,然而我并没有想出来.. 我们想把一个$n$维空间用$n$维球分成尽可能多的块 而新增加一个$n$维球时,肯定要尽可能多地切割 ...