如何使用 python 列出或发现 RabbitMQ 交换上的队列?
我需要一个 python 客户端,它可以在重新启动的 RabbitMQ 服务器交换上发现队列,然后启动客户端以恢复使用每个队列中的消息。如何从一些与 RabbitMQ 兼容的 python api/库中发现队列?
I need to have a python client that can discover queues on a restarted RabbitMQ server exchange, and then start up a clients to resume consuming messages from each queue. How can I discover queues from some RabbitMQ compatible python api/library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
由于我是 RabbitMQ 初学者,所以对此持保留态度,但有一个有趣的 管理插件,它公开了一个 HTTP 接口,“从这里您可以管理交换、队列、绑定、虚拟主机、用户和权限。这是相当不言自明的。”
http://www.rabbitmq.com/blog /2010/09/07/management-plugin-preview-release/
Since I am a RabbitMQ beginner, take this with a grain of salt, but there's an interesting Management Plugin, which exposes an HTTP interface to "From here you can manage exchanges, queues, bindings, virtual hosts, users and permissions. Hopefully the UI is fairly self-explanatory."
http://www.rabbitmq.com/blog/2010/09/07/management-plugin-preview-release/
我使用 https://github.com/bkjones/pyrabbit。它直接与 RabbitMQ 的 mgmt 插件的 API 接口对话,并且对于询问 RabbitMQ 非常方便。
I use https://github.com/bkjones/pyrabbit. It's talks directly to RabbitMQ's mgmt plugin's API interface, and is very handy for interrogating RabbitMQ.
管理功能将在 AMQP 的未来版本中提供。因此,现在您必须等待具有该功能的新版本。
Management features are due in a future version of AMQP. So for now you will have to wait till for a new version that will come with that functionality.
我发现这对我有用,/els 是我的演示虚拟主机名..
I found this works for me, /els being my demo vhost name..
Pyrabbit 对我来说效果不太好;但是,管理插件本身有自己的命令行脚本,您可以从自己的管理 GUI 下载该脚本并稍后使用(例如,我从其中下载了我的脚本
以供本地使用)
pyrabbit didn't work so well for me; However, the Management Plugin itself has its own command line script that you can download from your own admin GUI and use later on (for example, I downloaded mine from
for local use)
我会简单地使用这个:
只需将 user(default= guest)、passwd(default= guest) 和 port 替换为您的值即可。
从这里引用: https://gist.github.com/hiroakis/ 5088513#file-example_rabbitmq_api-py-L2
I would use simply this:
Just replace the user(default= guest), passwd(default= guest) and port with your values.
Referred from here: https://gist.github.com/hiroakis/5088513#file-example_rabbitmq_api-py-L2
似乎没有直接的 AMQP 方式来管理服务器,但有一种方法可以通过 Python 来实现。我建议结合使用 subprocess 模块和
rabbitmqctl
命令来检查队列的状态。我假设您在 Linux 上运行它。从命令行运行:
将导致:(
好吧,由于我的特定队列,在我的情况下确实如此)
在您的代码中,使用此代码获取
rabbitmqctl
的输出:然后,只需出现使用您自己的代码来解析
stdout_value
供您自己使用。There does not seem to be a direct AMQP-way to manage the server but there is a way you can do it from Python. I would recommend using a subprocess module combined with the
rabbitmqctl
command to check the status of the queues.I am assuming that you are running this on Linux. From a command line, running:
will result in:
(well, it did in my case due to my specific queues)
In your code, use this code to get output of
rabbitmqctl
:Then, just come up with your own code to parse
stdout_value
for your own use.你可以添加插件rabbitmq_management
然后使用rest-api
我在这个例子中使用requests库,但它并不重要。
我还找到了为我们做这件事的库 - pyrabbit
You can add plugin rabbitmq_management
Then use rest-api
I'm using requests library in this example, but it is not significantly.
Also I found library that do it for us - pyrabbit
据我所知,没有任何办法可以做到这一点。这与 Python 无关,而是因为 AMQP 没有定义任何队列发现方法。
无论如何,在 AMQP 中,是客户端(消费者)声明队列:发布者使用路由键将消息发布到交换器,消费者确定这些路由键进入哪些队列。所以在没有消费者的情况下谈论队列是没有意义的。
As far as I know, there isn't any way of doing this. That's nothing to do with Python, but because AMQP doesn't define any method of queue discovery.
In any case, in AMQP it's clients (consumers) that declare queues: publishers publish messages to an exchange with a routing key, and consumers determine which queues those routing keys go to. So it does not make sense to talk about queues in the absence of consumers.