[英]Consume PubSub in Google cloud function
Base on official document以官方文档为准
I try to create a cloud function with "PubSub Pull Subscription" trigger我尝试使用“PubSub Pull Subscription”触发器创建云 function
import base64
def hello_pubsub(event, context):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
print("This Function was triggered by messageId {} published at {}".format(context.event_id, context.timestamp))
if 'data' in event:
name = base64.b64decode(event['data']).decode('utf-8')
print('"{}" received!'.format(name))
if 'attributes' in event:
print(event['attributes'])
if '@type' in event:
print(event['@type'])
Then I find an article says that "cloud function will send ACK on its invocation", which is consistent with the official document.然后我找到一篇文章说“cloud function will send ACK on its invocation”,与官方文档一致。
However, when the cloud function finishes processing the PubSub message, "Unacked message count" do not decrease (as shown in the image above)但是,当云端 function 处理完 PubSub 消息后,“Unacked message count”不会减少(如上图所示)
Hence, I try google-cloud-pubsub on local因此,我在本地尝试google-cloud-pubsub
subscription_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
response = subscriber.pull(subscription_path, max_messages=5)
for msg in response.received_messages:
print("Received message:", msg.message.data)
ack_ids = [msg.ack_id for msg in response.received_messages]
subscriber.acknowledge(subscription_path, ack_ids)
In this way, the number of message count decrease successfully.这样,消息计数成功减少。
My questions are:我的问题是:
Any suggestion is appreciated, thank you.任何建议表示赞赏,谢谢。
With PubSub, you have publisher, that publish messages into a topic.使用 PubSub,您拥有发布者,将消息发布到主题中。 The message is duplicated in each existing subscription (created on a topic).消息在每个现有订阅中重复(在主题上创建)。 At the end, subscribers can listen subscription.最后,订阅者可以收听订阅。
So, here, you have have 1 topic, and 1 pull subscription.因此,在这里,您有 1 个主题和 1 个请求订阅。 You also have a Cloud Functions that you deploy on a topic (in gcloud cli, param --trigger-topic=myTopic
).您还有一个在主题上部署的云函数(在 gcloud cli 中,参数--trigger-topic=myTopic
)。 ON A TOPIC , not on a subscription.在主题上,而不是在订阅上。
Go back to the subscription page, you should see 2 subscriptions: Your pull subscription, and a push subscription to a strange endpoint Go 返回订阅页面,您应该会看到 2 个订阅:您的拉式订阅,以及对陌生端点的推送订阅
Therefore, your message is published in the 2 subscriptions.因此,您的消息在 2 个订阅中发布。 If you look your Pull subscription, nothing consume the message in it, except your code on local.如果您查看您的 Pull 订阅,除了您在本地的代码之外,没有任何内容会消耗其中的消息。 The logs in the cloud functions should show the correct message processing.云功能中的日志应显示正确的消息处理。
Is it clearer?是不是更清楚了?
EDIT编辑
To be precise on your case:确切地说,您的情况:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.