【问题标题】:How data is sent from client to (multi-service) server in gRPCgRPC 中数据如何从客户端发送到(多服务)服务器
【发布时间】:2019-03-13 04:25:41
【问题描述】:

我在我的一个应用程序(语音识别)中使用 gRPC 客户端服务器框架。我想通过我的观察来澄清一些重要的事情。

1.可选数据字段在客户端未填充时如何发送?

让我们看看下面的例子:(假设使用了 proto3,所以默认情况下所有字段都是可选的)

service NameStudent {
    rpc GetRoll(Student) returns (Details) {}
}

#Student'd details
message Student{
    int32 roll = 1;
    string name = 2;
    string gender = 4;
    int32 age = 3;
    DOB dateofbirth = 5;
}

#Students Date of Birth
message DOB {
    int32 dd = 1;
    int32 mm = 2;
    int32 yy = 3;
}

#Parent's details
message Parent{
    string parent =1;
}

#Students all details (includes student + parent)
message Details {
    Student student = 1;
    Parent parent = 4;
}

假设服务获取(来自客户端的输入)一些学生详细信息,例如姓名、姓名和年龄,并返回该学生的(所有) 详细信息

所以现在如果不是发送所有 3 个详细信息(即滚动、姓名和年龄),甚至可以发送任何一两个详细信息,并且(从逻辑上假设)服务有效。

在这种情况下,服务器会接收所有字段(省略字段为空白/NULL)还是客户端根本不发送那些省略的信息? (见下文二进制数据的表示从客户端发送)

// roll and name filled
// age is left blank
// gender and DOB are always sent blank from client
{
    roll: 170012,
    name: "John Doe",
    age: ,
    gender: "",
    dateofbirth: {
           dd: ,
           mm: ,
           yy: 
    }
}

//only roll and name is sent and rest is just not sent
{
    roll: 170012,
    name: "John Doe"
}

2。可以为两个服务连接单个存根吗?

如果服务器提供 2 项服务并且我正在制作客户端存根,我能否将来自同一个存根的 2 个通道连接到访问它的 2 个不同服务的同一台服务器?

【问题讨论】:

    标签: web-services protocol-buffers grpc protobuf-c


    【解决方案1】:

    问题 1

    看看this protobuf documentation。特别是:

    对于 proto3 中的任何非重复字段,或 proto2 中的可选字段, 编码的消息可能有也可能没有与之对应的键值对 字段编号。

    但在实践中,我观察到在序列化中省略了具有默认值的可选字段。当 protobuf 被反序列化时,解析器会将缺失的字段解释为默认值。您可以通过在 Python protobuf 对象上使用 SerializeToString() 方法自己观察此行为。

    问题 2

    绝对有可能将多个 gRPC 服务附加到同一个服务器并与来自同一个客户端通道的多个服务进行交互。 gRPC 使用HTTP2 paths 来区分连接到同一服务器的多个服务。看看this gRPC Python generated code 就是一个例子。 add_GreeterServicer_to_server 将用户定义的处理程序与路径 /helloworld.Greeter/SayHello 相关联,然后存根使用该路径来识别服务器上的该服务。

    【讨论】:

    • 嘿@Richard 很好地起草了答案(对此表示赞同)。我已经得到了问题的答案,但了解您在Answer2中分享的示例会有所帮助。我不是 python 人,所以我会尽力一步一步地完成这个例子。同时,如果您可以/想要对此添加更多解释,请这样做...再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2013-04-11
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多