简体   繁体   English

在谷歌云中使用 PubSub function

[英]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:我的问题是:

  • Am I missing something in my cloud function script?我的云 function 脚本中是否缺少某些内容?
  • How can I actually "consume" the PubSub message in my cloud function?我如何才能真正“使用”我的云 function 中的 PubSub 消息?

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:确切地说,您的情况:

  • Your Cloud Functions can't ack the messages in the pull subscription, because it is not connected to it您的 Cloud Functions 无法确认拉取订阅中的消息,因为它没有连接到它
  • Your Cloud Functions process and ack messages published in its own subscription.您的 Cloud Functions 处理并确认在其自己的订阅中发布的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud PubSub - 如何将多个 arguments 发送到云端 Function - Google Cloud PubSub - How to send multiple arguments to the Cloud Function Google Cloud Function - ImportError:无法从“google.cloud”(未知位置)导入名称“pubsub” - Google Cloud Function - ImportError: cannot import name 'pubsub' from 'google.cloud' (unknown location) Python Google Cloud PubSub超时异常 - Python Google Cloud PubSub TimeOut Exception 未通过回调处理的Google Cloud PubSub消息 - Google Cloud PubSub messages not processed by callback 无法在Python中正确调用Google Cloud PubSub - Google Cloud PubSub not being called properly in Python 获取 Google Cloud PubSub 中单条消息的大小 - Get the size of a single message in Google Cloud PubSub Google Cloud Dataflow - 从 PubSub 到 Parquet - Google Cloud Dataflow - From PubSub to Parquet BASE64 在解码 pubsub 消息时在谷歌云 function 中应用时解码不起作用 - BASE64 decoding not working when applied in google Cloud function while decoding pubsub message Cloud Function 从 PubSub 检索属性 - Python - Cloud Function to retrieve attributes from PubSub - Python Google Cloud PubSub:不发送/接收来自 Cloud Functions 的所有消息 - Google Cloud PubSub: Not sending/receiving all messages from Cloud Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM