【问题标题】:How to automatically assign a publisher role and subscriber role on dead letter subscription with pubsub?如何使用 pubsub 在死信订阅中自动分配发布者角色和订阅者角色?
【发布时间】:2020-09-28 12:51:52
【问题描述】:

我正在升级以前使用 RabbitMQ 的队列处理系统。

我目前正在导入我们在 RabbitMQ 中使用的死信和指数退避功能,但是我似乎遇到了几个问题。

主要问题是,当我使用死信策略创建新订阅时,它似乎没有相关权限,以便 Google 将消息转发到我的死信订阅。

如下所示,在检查订阅详情时,google 已经强调项目的 pubsub 服务帐户需要发布者角色和订阅者角色才能发布和转发到死信主题。

在 UI 中提供了添加这些选项的选项,但是我需要完全通过我正在运行的消费者来处理这个,因为这需要是一个自动化的过程,因为这些消费者是由 SupervisorD 在后台运行的。

有谁知道我到底错过了什么,我浏览了所有文档,但并不完全清楚,我尝试将 IAM 中提到的权限添加到关联成员,但没有骰子。

【问题讨论】:

    标签: google-cloud-platform publish-subscribe


    【解决方案1】:

    为了将消息转发到死信主题,Pub/Sub 必须具有 permission 才能执行以下操作:

    • 向死信主题发布消息。
    • 确认转发的消息,将其从订阅中删除。

    如果您想通过您正在运行的消费者授予每个文档所需的权限,我建议您先通过Cloud SDK adding the publisher role

    PUBSUB_SERVICE_ACCOUNT="service-${project-number}@gcp-sa-pubsub.iam.gserviceaccount.com"
    
    gcloud pubsub topics add-iam-policy-binding dead-letter-topic-id \
        --member="serviceAccount:$PUBSUB_SERVICE_ACCOUNT"\
        --role="roles/pubsub.publisher"
        
    

    然后,subscriber role

    PUBSUB_SERVICE_ACCOUNT="service-${project-number}@gcp-sa-pubsub.iam.gserviceaccount.com"
    
    gcloud pubsub subscriptions add-iam-policy-binding subscription-id \
        --member="serviceAccount:$PUBSUB_SERVICE_ACCOUNT"\
        --role="roles/pubsub.subscriber"
        
    

    有了该权限,您现在可以track the delivery attempts 使用您喜欢的语言,例如 Python:

    from concurrent.futures import TimeoutError
    from google.cloud import pubsub_v1
    
    # TODO(developer)
    # project_id = "your-project-id"
    # subscription_id = "your-subscription-id"
    
    subscriber = pubsub_v1.SubscriberClient()
    subscription_path = subscriber.subscription_path(project_id, subscription_id)
    
    def callback(message):
        print("Received message: {}".format(message))
        print("With delivery attempts: {}".format(message.delivery_attempt))
        message.ack()
    
    streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
    print("Listening for messages on {}..\n".format(subscription_path))
    
    # Wrap subscriber in a 'with' block to automatically call close() when done.
    with subscriber:
        # When `timeout` is not set, result() will block indefinitely,
        # unless an exception is encountered first.
        try:
            streaming_pull_future.result(timeout=timeout)
        except TimeoutError:
            streaming_pull_future.cancel()
    

    【讨论】:

      【解决方案2】:

      根据the documentation,您必须授予托管源主题的项目的Service Agent (sa) Pubsub 服务帐户,并在死信主题上授予角色Pubsub Publisher。

      -> 授权源项目在死信主题中发布消息

      您还必须在配置了死信功能的源上授予托管死信主题的项目的服务代理 (sa) Pubsub 服务帐户,角色为 Pubsub 订阅者。

      -> 授权确认死信主题中发布的死信消息。

      这里是 Service Agent PubSub 服务帐户的模式

      service-${project-number}@gcp-sa-pubsub.iam.gserviceaccount.com
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多