RabbitMQ - 使用curl从队列中获取消息

时间:2017-04-20 08:04:58

标签: linux curl rabbitmq

我正在尝试使用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"}
你可以指出我的错误吗?我怎样才能收到这些消息?

4 个答案:

答案 0 :(得分:4)

请注意,语法在最近的发行版中已更改(并且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}' 

答案 1 :(得分:3)

您缺少队列名称:

result = test.Insert(test.IndexOf("C"), "Z");

其中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

修改

如果您使用默认的vhost:

[
   {
      "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"
   }
]

答案 2 :(得分:2)

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

  

我没有配置vhost。

RabbitMQ使用" /"默认VHOST的表示法。

enter image description here

" /"在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}' 

答案 3 :(得分:0)

请务必注意,the documentation对此端点进行了以下说明:

请注意,HTTP API中的get路径用于诊断等-它无法实现可靠的传递,因此应将其视为sysadmin的工具,而不是用于消息传递的常规API。