protocol-buffers - google.protobuf.Empty 向后兼容是否危险?

标签 protocol-buffers grpc

specgoogle.protobuf.Empty状态:

A generic empty message that you can re-use to avoid defining duplicated empty messages in your APIs. A typical example is to use it as the request or the response type of an API method.



我一直在内部提倡使用空消息包装器,以保持向后兼容性。例如,假设我们有一个 FooService :
service Foo {
    rpc List(google.protobuf.Empty) returns (ListResponse) {}
}

message ListResponse {
    repeated Foo results = 1;
}

message Foo {...}

如果将来我们需要向这个列表请求添加分页,我们需要引入一个请求包装器:
message ListRequest {
    int limit = 1;
    int offset = 2;
}

然后更新 rpc 签名:
rpc List(ListRequest) returns (ListResponse) {}

这是向后不兼容的更改,还是 protobuf 格式可以优雅地处理这个?

最佳答案

有线格式很好地处理了这个问题。但是,大多数使用 gRPC stub 的代码都会中断,因为类型安全语言会注意到不兼容的类型。

如果您认为您可能需要字段,请继续为该案例制作一条特殊消息,即使它是空的。如果有疑问,就去做。如果你确信你永远不需要任何字段(“删除”的响应消息是一个常见的例子),那么使用 Empty很好。

我在我的 Modifying gRPC Services Over Time 中提到了这个具体案例讲话。提供幻灯片和视频录制。

关于protocol-buffers - google.protobuf.Empty 向后兼容是否危险?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50993815/

相关文章:

node.js - 使用 kubernetes 部署时无法连接 gRPC

java - Google Protocol Buffer 和 HTTP

c++ - protobuf,如何在我不太了解的 protobuf 消息中遍历所有设置字段?(C++)

c# - 如果我的 gRPC 项目中缺少 Google.Api.AnnotationsReflection,我缺少什么库?

json - 在 Go 中将带有枚举的 Protobuf3 转换为 JSON

java - 当 ServerCall 在第一个 ServerInterceptro 中关闭时如何忽略 gRPC(java) ServerInterceptor 的其余部分?

protocol-buffers - 我可以定义带有空请求或响应的 grpc 调用吗?

java - proto 生成从 jar 文件导入的 java

javascript - node js grpc server forceShutdown 没有正常关闭

c++ - 检测 grpc 服务器中关闭的客户端连接