以官方文檔為準(zhǔn)我嘗試使用“PubSub Pull Subscription”觸發(fā)器創(chuàng)建云函數(shù)import base64def 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”,和官方文檔是一致的。但是,當(dāng)云函數(shù)完成對 PubSub 消息的處理后,“Unacked message count”并沒有減少(如上圖所示)因此,我在本地嘗試google-cloud-pubsubsubscription_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)這樣,消息計數(shù)成功減少。我的問題是:我的云函數(shù)腳本中是否缺少某些內(nèi)容?我怎樣才能在我的云函數(shù)中實際“使用”PubSub 消息?任何建議表示贊賞,謝謝。
1 回答

互換的青春
TA貢獻(xiàn)1797條經(jīng)驗 獲得超6個贊
使用 PubSub,您有發(fā)布者,將消息發(fā)布到主題中。該消息在每個現(xiàn)有訂閱(在主題上創(chuàng)建)中重復(fù)。最后,訂閱者可以收聽訂閱。
因此,在這里,您有 1 個主題和 1 個請求訂閱。您還有一個部署在主題上的 Cloud Functions (在 gcloud cli 中,param --trigger-topic=myTopic
)。關(guān)于主題,而不是訂閱。
返回訂閱頁面,您應(yīng)該看到 2 個訂閱:您的請求訂閱和對一個陌生端點的推送訂閱
因此,您的消息發(fā)布在 2 個訂閱中。如果您查看您的 Pull 訂閱,除了您在本地的代碼外,沒有任何內(nèi)容會消耗其中的消息。云函數(shù)中的日志應(yīng)該顯示正確的消息處理。
是不是更清楚了?
編輯
準(zhǔn)確地說是你的情況:
您的 Cloud Functions 無法確認(rèn)請求訂閱中的消息,因為它沒有連接到它
您的 Cloud Functions 處理并確認(rèn)在其自己的訂閱中發(fā)布的消息。
添加回答
舉報
0/150
提交
取消