Google protobuf:如果我从一侧添加字段会破坏另一个字段吗?

Vit*_*lii 5 protocol-buffers

我有两个微服务。第一个服务使用google protobufgoogle docs)对消息进行编码并发送到第二个。第二个解码此消息并使用数据。

现在我需要向这个消息对象添加一个字段。如果我只在一侧进行,它会破坏另一侧吗?

例如,如果我添加一些东西,json这不会破坏任何东西。真的一样google protobuf吗?

小智 11

从 Goole Protobuf 文档中引用了关于从这里扩展 Protobuf 的扩展:

you must not change the tag numbers of any existing fields.
you must not add or delete any required fields.
you may delete optional or repeated fields.
you may add new optional or repeated fields but you must use fresh tag numbers (i.e. tag numbers that were never used in this protocol buffer, not even by deleted fields).
Run Code Online (Sandbox Code Playgroud)

在您的情况下,如果您在一侧的更改没有违反上述任何规则,则不会破坏另一侧。


It'*_*ete 5

从文档(见这里:https : //developers.google.com/protocol-buffers/docs/overview):

您可以在不破坏向后兼容性的情况下向消息格式添加新字段;旧的二进制文件在解析时会简单地忽略新字段。因此,如果您有一个使用协议缓冲区作为其数据格式的通信协议,您可以扩展您的协议,而不必担心破坏现有代码。