![](/img/trans.png)
[英]Google Cloud PubSub - How to send multiple arguments to the Cloud Function
[英]Consume PubSub in Google cloud function
以官方文檔為准
我嘗試使用“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'])
然后我找到一篇文章說“cloud function will send ACK on its invocation”,與官方文檔一致。
但是,當雲端 function 處理完 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)
這樣,消息計數成功減少。
我的問題是:
任何建議表示贊賞,謝謝。
使用 PubSub,您擁有發布者,將消息發布到主題中。 消息在每個現有訂閱中重復(在主題上創建)。 最后,訂閱者可以收聽訂閱。
因此,在這里,您有 1 個主題和 1 個請求訂閱。 您還有一個在主題上部署的雲函數(在 gcloud cli 中,參數--trigger-topic=myTopic
)。 在主題上,而不是在訂閱上。
Go 返回訂閱頁面,您應該會看到 2 個訂閱:您的拉式訂閱,以及對陌生端點的推送訂閱
因此,您的消息在 2 個訂閱中發布。 如果您查看您的 Pull 訂閱,除了您在本地的代碼之外,沒有任何內容會消耗其中的消息。 雲功能中的日志應顯示正確的消息處理。
是不是更清楚了?
編輯
確切地說,您的情況:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.