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

问题描述 投票:0回答:5

我正在尝试使用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
5个回答
10
投票

您缺少队列名称:

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}'

9
投票

请注意,语法似乎在最近的版本中发生了变化(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}' 

6
投票

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

我没有配置虚拟主机。

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}' 

0
投票

需要注意的是,文档对此端点有以下说明:

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


0
投票

如果您想获取消息并将其保存在文件中,您可以使用 “获取 RabbitMQ 消息” 包。

安装:

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
© www.soinside.com 2019 - 2024. All rights reserved.