【问题标题】:how to add protobuf file for topic in gcloud pubsub?如何在 gcloud pubsub 中为主题添加 protobuf 文件?
【发布时间】:2018-04-30 22:57:03
【问题描述】:

在 google cloud 的 pubsub 中,我可以看到在创建新主题时,我必须创建一条新消息。我可以在那里存储一个 protobuf 文件,而不必在键值对中写入整个消息结构吗?对于要编写的 protobuf 代码,我的意思是this。如果 protobuf 不打算放在 gcloud pubsub 上,我如何将它与 grpc 客户端一起使用?

【问题讨论】:

  • “在创建新主题时,我必须创建新消息”以及您想“在此处存储 protobuf 文件”是什么意思? CreateSubscription 是您需要通过客户端库、gRPC 或 HTTP 调用的 Pub/Sub API 的一部分。您要存储的 protobuf 文件是什么?您的意思是当您通过 Publish API 向主题发送消息时?
  • 是的,这就是我的意思。是否可以为消息制作基于 protobuf 的格式,以便轻松填写数据并使消息的结构更加严格。

标签: publish-subscribe google-cloud-pubsub


【解决方案1】:

如果您想通过Publish API 发送ProtoBuf 消息,您应该将其序列化为ByteString,然后将其设置为消息的data 字段。例如,如果您使用的是Java client library,并且您有一些 ProtoBuf 类型的 Publisherobj,那么您可以执行以下操作:

PubsubMessage message = PubsubMessage.newBuilder()
    .setData(obj.toByteString())
    .build();
ApiFuture<String> response = publisher.publish(message);
...

在订阅端,您将解码MessageReceiver 中的消息:

public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
  ProtoBufMessage obj;
  try {
    obj = ProtoBufMessage.parseFrom(message.getData());
  } catch (Exception e) {
    // Handle improperly encoded message
  }
  ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 2023-03-29
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2019-02-25
    相关资源
    最近更新 更多