【问题标题】:Using pika, is it possible to read rabbitmq binding arguments?使用 pika,是否可以读取 rabbitmq 绑定参数?
【发布时间】:2018-10-23 07:50:30
【问题描述】:

看下面的截图

我的队列与名为foo 的交换机绑定,只接收带有路由键bar 的消息。我还定义了一对参数{baz: qux}。现在我有以下代码:

credentials = pika.PlainCredentials(...)
parameters = pika.ConnectionParameters(...)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback, queue='this_queue')

并且回调具有以下签名:

def callback(channel, method, properties, body):
    ....

现在的问题是:如何在回调中访问参数 ({baz: qux})。这可能吗?

【问题讨论】:

    标签: python rabbitmq pika python-pika


    【解决方案1】:

    这是不可能的,因为 AMQP 没有在对basic.consume 的响应中提供该信息。只要您启用了rabbitmq_management 插件,就可以使用HTTP API 检索该信息(如果您拍摄了该屏幕截图,则必须这样做)。


    注意:RabbitMQ 团队监控the rabbitmq-users mailing list,并且有时只回答 StackOverflow 上的问题。

    【讨论】:

      【解决方案2】:

      您可以使用我的RabbitMQamqpstorm 来获取此类信息。

      from amqpstorm.management import ManagementApi
      
      API = ManagementApi('http://127.0.0.1:15672', 'guest', 'guest')
      print(API.queue.bindings('simple_queue', virtual_host='/'))
      

      结果看起来像这样。

      [
          {
              "source": "",
              "vhost": "/",
              "destination": "simple_queue",
              "destination_type": "queue",
              "routing_key": "simple_queue",
              "arguments": {},
              "properties_key": "simple_queue"
          },
          {
              "source": "amq.direct",
              "vhost": "/",
              "destination": "simple_queue",
              "destination_type": "queue",
              "routing_key": "test",
              "arguments": {},
              "properties_key": "test"
          }
      ]
      

      更多示例可用here

      【讨论】:

        猜你喜欢
        • 2014-03-30
        • 1970-01-01
        • 2021-06-23
        • 1970-01-01
        • 2012-09-19
        • 1970-01-01
        • 1970-01-01
        • 2014-05-21
        • 2015-01-19
        相关资源
        最近更新 更多