接收消息 ID(未来)是否表示消息已在 PubSub 中发送?

Does recieving message id (future) indicate message is sent in PubSub?

我正在使用回调函数向 google pubsub 发送消息,该回调函数会从未来读回消息 ID。使用以下代码:

"""Publishes multiple messages to a Pub/Sub topic with an error handler."""
import time

from google.cloud import pubsub_v1

# ...

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_name)

def get_callback(f, data):
    def callback(f):
        try:
            print(f.result())
        except:  # noqa
            print('Please handle {} for {}.'.format(f.exception(), data))

    return callback

for i in range(10):
    data = str('message')
    # When you publish a message, the client returns a future.
    future = publisher.publish(
        topic_path, data=data.encode('utf-8')  # data must be a bytestring.
    )
    # Publish failures shall be handled in the callback function.
    future.add_done_callback(get_callback(future, data))

print('Published message with error handler.')

我现在 errors/exceptions 成功收到消息 ID,但是我发现有些消息没有读入 pubsub(从 GCP 控制台查看它们时)。

消息 ID 打印在回调函数中的 print(f.result()) 行。

我的问题是: 假设消息在收到消息 ID 后成功发送到 Pubsub 是否安全?

如果是这样,'dropped' 条消息的原因可能是什么?

如果发布已成功返回消息 ID,那么是的,云 Pub/Sub 保证将消息传递给订阅者。如果您没有看到该消息,可能有多种原因:

  1. 发布消息时订阅不存在。如果订阅是在消息发布之前创建的,云 Pub/Sub 仅将消息传递给订阅者。
  2. 订阅的另一个订阅者已经 运行 并收到了消息。如果您在订阅者启动时使用 GCP 控制台获取消息,并且 运行,订阅者可能收到了消息。
  3. 如果您在控制台上收到过一次消息,然后重新加载以再次收到消息,您可能不会再次看到该消息,直到确认截止时间已过,默认为 10 秒。
  4. 单个拉取请求(这是 GCP 控制台 "View Messages" 功能所做的)可能不足以检索消息。如果您多次单击它或 start up a basic subscriber,您可能会看到该消息。