以官方文檔為準我嘗試使用“PubSub Pull Subscription”觸發器創建云函數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”,和官方文檔是一致的。但是,當云函數完成對 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)這樣,消息計數成功減少。我的問題是:我的云函數腳本中是否缺少某些內容?我怎樣才能在我的云函數中實際“使用”PubSub 消息?任何建議表示贊賞,謝謝。
1 回答

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