GRPC:客户端流与配置消息 [英] GRPC: Client streaming with configuration message

查看:90
本文介绍了GRPC:客户端流与配置消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是消耗事件流的服务的原始定义 来自客户

Here's a proto definition for a service that consumes a stream of events from a client

message Event {
  // ...
}

service EventService {
  rpc Publisher(stream Event) returns (google.protobuf.Empty);
}

问题在于需要告知服务器如何处理此流. 理想情况下,它首先会收到一条Options消息:

The problem is that the server needs to be told what to do with this stream. Ideally, it would first recieve an Options message:

message Event {
  // ...
}

message Options {
  // ...
}

service EventService {
  rpc Publisher(Options, stream Event) returns (google.protobuf.Empty);
}

但是,grpc仅支持rpc方法的一个参数. 一种解决方案是引入附加的PublishMessage消息,该消息 可以包含OptionsEvent消息.

However, grpc only supports one parameter for rpc methods. One solution is to introduce an additional PublishMessage message which can contain either an Options or Event message.

message PublishMessage {
  oneof content {
    Options options = 1;
    Event event = 2;
  }
}

然后,该服务将期望第一个PublishMessage包含一个Options消息,而所有后续的所有PublishMessage都包含Event消息.这会从包装消息中引入额外的开销,并使api变得笨拙.

The service would then expect the first PublishMessage to contain an Options message, with all subsequent ones containing Event messages. This introduces additional overhead from the wrapping message and makes the api a little clunky.

有没有更干净的方法来达到相同的结果?

Is there a cleaner way to achieve the same result?

推荐答案

在播放多个字段或消息时,建议使用oneof.开销很小,因此通常不必担心.不过有笨拙.

Using oneof is the suggested approach when many fields or messages are in play. The overhead is minimal, so wouldn't generally be a concern. There is the clunkiness though.

如果只有几个字段,则可能需要将选项"和事件"中的字段合并为一条消息.或类似地,将选项"添加到事件"作为字段.您希望选项"字段在第一个请求中出现,而在随后的请求中丢失.当配置字段(例如名称")较少时,这种方法会更好.

If there's only a few fields, you may want to combine the fields from Options and Event into a single message. Or similarly add Options to Event as a field. You'd expect the Options fields to be present on the first request and missing from subsequent. This works better when there's fewer configuration fields, like just a "name."

这篇关于GRPC:客户端流与配置消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆