【问题标题】:RabbitMQ - Get messages from a queue using curlRabbitMQ - 使用 curl 从队列中获取消息
【发布时间】:2017-09-16 17:49:41
【问题描述】:

我正在尝试使用 rabbitmq 的 HTTP API 从队列中获取一些消息。

我正在关注here 中的文档 我没有配置vhost。

我尝试了以下 curl 命令:

curl -i -u guest:guest -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/foo/get -d'{"count":5,"requeue":true,"encoding":"auto","truncate":50000}'

RabbitMQ 然后回答:

HTTP/1.1 405 Method Not Allowed
vary: origin
Server: MochiWeb/1.1 WebMachine/1.10.0 (never breaks eye contact)
Date: Thu, 20 Apr 2017 08:03:28 GMT
Content-Length: 66
Allow: HEAD, GET, PUT, DELETE, OPTIONS

{"error":"Method Not Allowed","reason":"\"Method Not Allowed\"\n"}

你能指出我的错误吗?我怎样才能得到这些消息?

【问题讨论】:

    标签: linux curl rabbitmq


    【解决方案1】:

    您缺少队列名称:

    curl -i -u guest:guest -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/foo/my_queue/get -d'{"count":5,"requeue":true,"encoding":"auto","truncate":50000}'
    

    其中foo 是虚拟主机,my_queue 是队列名称。

    结果:

    [
       {
          "payload_bytes":4,
          "redelivered":true,
          "exchange":"",
          "routing_key":"my_queue",
          "message_count":5,
          "properties":{
             "delivery_mode":1,
             "headers":{
    
             }
          },
          "payload":"test",
          "payload_encoding":"string"
       },
       {
          "payload_bytes":4,
          "redelivered":true,
          "exchange":"",
          "routing_key":"my_queue",
          "message_count":4,
          "properties":{
             "delivery_mode":1,
             "headers":{
    
             }
          },
          "payload":"test",
          "payload_encoding":"string"
       },
       {
          "payload_bytes":4,
          "redelivered":true,
          "exchange":"",
          "routing_key":"my_queue",
          "message_count":3,
          "properties":{
             "delivery_mode":1,
             "headers":{
    
             }
          },
          "payload":"test",
          "payload_encoding":"string"
       },
       {
          "payload_bytes":4,
          "redelivered":true,
          "exchange":"",
          "routing_key":"my_queue",
          "message_count":2,
          "properties":{
             "delivery_mode":1,
             "headers":{
    
             }
          },
          "payload":"test",
          "payload_encoding":"string"
       },
       {
          "payload_bytes":4,
          "redelivered":true,
          "exchange":"",
          "routing_key":"my_queue",
          "message_count":1,
          "properties":{
             "delivery_mode":1,
             "headers":{
    
             }
          },
          "payload":"test",
          "payload_encoding":"string"
       }
    ]
    

    编辑

    如果您使用的是默认虚拟主机:

    curl -i -u guest:guest -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/%2f/my_queue/get -d'{"count":5,"requeue":true,"encoding":"auto","truncate":50000}'
    

    【讨论】:

    • 我可以添加过滤器吗?例如获取按标题或属性过滤的消息?
    • 再一次,不,这是不可能的,正如我所说:you can consume one message at a time in FIFO way :)!
    【解决方案2】:

    请注意,语法似乎在最近的版本中发生了变化(并且 HTTP API 文档似乎落后了),而不是 requeue 选项需要设置 ack_mode 选项,例如"ack_mode"="ack_requeue_true"

    所以上面针对当前 RabbitMQ 版本的示例是:

    curl -u guest:guest -i -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/%2F/foo/get -d'{"count":5,"ack_mode"="ack_requeue_true","encoding":"auto","truncate":50000}' 
    

    【讨论】:

    • 必须是 {"ackmode":"ack_requeue_true"} 而不是 {"ack_mode"="ack_requeue_true"},否则你会得到 not json 错误。
    【解决方案3】:

    我设法解决了这个问题。关键:

    我没有配置虚拟主机。

    RabbitMQ 使用“/”表示默认 VHOST。

    "/" 在 HTTP 中被翻译成%2F...

    所以正确的调用是:

    curl -u guest:guest -i -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/%2F/foo/get -d'{"count":5,"requeue":true,"encoding":"auto","truncate":50000}' 
    

    【讨论】:

      【解决方案4】:

      请务必注意,the documentation 关于此端点的说明如下:

      请注意,HTTP API 中的获取路径用于诊断等 - 它不实现可靠传递,因此应被视为系统管理员的工具,而不是用于消息传递的通用 API。

      【讨论】:

        【解决方案5】:

        如果您想获取消息并将它们保存在文件中,您可以使用"Get RabbitMQ messages" 包。

        安装:

        pip install rabbitgetapi
        

        例子:

        $ rabbitgetapi getqueue -f <path/fileconf.yaml> -o <outputfile> -c 1000 -s =
        
        Where (get dlq messages):
          path/fileconf.yaml = Configuration file (In this example, the server credentials)
          outputfile = File where the message will be saved  
          c = Counter
          s = Seperator
        

        配置文件内容:

        url: 
         https://f-9e14acc7-ef3c-4a2f-a829-c96a7c8c691f.mq.us-east-1.amazonaws.com 
         
        user: 
         login-user
         
        password: 
         the-secret-password
         
        queue: 
         DLQ_QUEUE
         
        count: 
         100
        

        【讨论】:

          猜你喜欢
          • 2013-05-17
          • 1970-01-01
          • 1970-01-01
          • 2013-12-18
          • 2018-08-10
          • 2013-08-09
          • 2015-11-05
          • 2014-03-19
          • 1970-01-01
          相关资源
          最近更新 更多