如何在 proto 文件中添加两个同名的方法

How can I add two methods with the same name in proto file

我的原型中有多个同名但参数不同的方法,当我尝试生成 grpc 时出现错误:

'the method is already defined'

rpc method_x (Request_1) returns (response_x) {}
rpc method_x(Request_2) returns (response_x) {}

没有。方法解析基于 just 名称,并且 protobuf 有效载荷不是自描述的,因此无法知道特定有效载荷是 X 还是 Y - 编组器/类型已解析来自方法名称。

所以:没有。

您可以做的是使用 oneof - 有一种方法采用单个根消息,该消息具有 (oneof) X, Y, Z, ... 之一作为字段- 本质上是一个受歧视的联盟。并根据反序列化后的内部切换。