han*_*hih 2 python google-cloud-platform google-cloud-pubsub google-cloud-functions
基于官方文件
我尝试使用“PubSub Pull Subscription”触发器创建云函数
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'])
Run Code Online (Sandbox Code Playgroud)

然后我发现一篇文章说“云函数在调用时会发送ACK”,这与官方文档一致。
但是,当云函数处理完 PubSub 消息后,“Unacked message count”并没有减少(如上图所示)
因此,我在本地尝试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)
Run Code Online (Sandbox Code Playgroud)
这样,消息计数成功减少。

我的问题是:
任何建议表示赞赏,谢谢。
使用 PubSub,您有发布者,可以将消息发布到主题中。该消息在每个现有订阅中重复(在主题上创建)。最后,订阅者可以收听订阅。
因此,在这里,您有 1 个主题和 1 个拉取订阅。您还有一个部署在某个主题上的 Cloud Functions (在 gcloud cli, param 中--trigger-topic=myTopic)。ON A TOPIC,而不是订阅。
返回订阅页面,您应该看到 2 个订阅:您的拉订阅,以及对陌生端点的推送订阅
因此,您的消息发布在 2 个订阅中。如果您查看您的 Pull 订阅,除了您在本地的代码之外,没有任何东西会消耗其中的消息。云函数中的日志应显示正确的消息处理。
是不是更清楚了?
编辑
准确地说,你的情况:
| 归档时间: |
|
| 查看次数: |
2703 次 |
| 最近记录: |