RabbitMQ——CLI 管理工具 rabbitmqadmin
前言
一般情况下,我们会使用 rabbitmq_management 插件,通过 Web UI 的方式来监控和操作 RabbitMQ(端口 15672),但有时候命令的方式会更加方便一些,
RabbitMQ 提供了 CLI 管理工具 rabbitmqadmin ,其实就是基于 RabbitMQ 的 HTTP API,用 Python 写的一个脚本。
rabbitmqadmin 提供了下面功能:
- 列出 exchanges, queues, bindings, vhosts, users, permissions, connections and channels。
- 创建和删除 exchanges, queues, bindings, vhosts, users and permissions。
- 发布和获取消息,以及消息详情。
- 关闭连接和清空队列。
- 导入导出配置。
安装rabbitmqadmin
如果我们安装了rabbitmq_management
插件,可以打开http://node1:15672/cli/
页面,下载 rabbitmqadmin。
或者直接进行下载:
wget http://node1:15672/cli/rabbitmqadmin
然后移到PATH
目录,并赋予权限:
mv rabbitmqadmin /usr/local/bin
sudo chmod 777 /usr/local/bin/rabbitmqadmin
查看 rabbitmqadmin 命令:
[root@node1 ~]# rabbitmqadmin -help Usage ===== rabbitmqadmin [options] subcommand ...
如果出现以下提示:
[root@zk-mq-1 ~]# rabbitmqadmin -help /usr/bin/env: python3: No such file or directory
则需要安装相应版本的python:
yum install python3
使用rabbitmqadmin
- 创建一个
test
队列:
[root@node1 ~]# rabbitmqadmin declare queue name=test durable=true queue declared [root@node1 ~]# rabbitmqadmin list queues +------+----------+ | name | messages | +------+----------+ | test | 0 | +------+----------+
需要注意:durable=true
表示持久化,如果没有创建 Exchange,RabbitMQ 会使用默认的 Exchange,以及创建一个 Binding,这也就是为什么下面发布消息,可以成功的原因。
- 发布一条消息:
[root@node1 ~]# rabbitmqadmin publish routing_key=test payload="hello world" Message published [root@node1 ~]# rabbitmqadmin list queues +------+----------+ | name | messages | +------+----------+ | test | 1 | +------+----------+
- 消费一条消息(查看消息内容):
[root@node1 ~]# rabbitmqadmin get queue=test requeue=true +-------------+----------+---------------+-------------+---------------+------------------+------------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered | +-------------+----------+---------------+-------------+---------------+------------------+------------+-------------+ | test | | 0 | hello world | 11 | string | | False | +-------------+----------+---------------+-------------+---------------+------------------+------------+-------------+ [root@node1 ~]# rabbitmqadmin list queues +------+----------+ | name | messages | +------+----------+ | test | 1 | +------+----------+ [root@node1 ~]# rabbitmqadmin get queue=test requeue=false +-------------+----------+---------------+-------------+---------------+------------------+------------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered | +-------------+----------+---------------+-------------+---------------+------------------+------------+-------------+ | test | | 0 | hello world | 11 | string | | True | +-------------+----------+---------------+-------------+---------------+------------------+------------+-------------+ [root@node1 ~]# rabbitmqadmin list queues +------+----------+ | name | messages | +------+----------+ | test | 0 | +------+----------+
需要注意:requeue=true
表示查看队列,不消费;requeue=false
表示查看队列,进行消费。
- 我们查看下上面创建队列后,默认创建的 Binding:
[root@node1 ~]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
可以看到,使用的默认 Exchange 是空字符串(系统中的第一个 Exchange,Binding 中的 source 部分),
并且这个默认的 Exchange 是direct
类型(也就是一对一模式),这种隐式调用确保了消息准确投递。
另外,再说一下 Binding 中的三个概念:
Source:源头的意思,其实就是 Exchange。
destination:目的地的意思,其实就是 Queue。
routing_key:路由键的规则,用于 Queue 匹配(比如test.#
)。
- 创建一个 Exchange:
[root@node1 ~]# rabbitmqadmin declare exchange name=my.topic type=topic exchange declared [root@node1 ~]# rabbitmqadmin list exchanges +--------------------+---------+ | name | type | +--------------------+---------+ | | direct | | amq.direct | direct | | amq.fanout | fanout | | amq.headers | headers | | amq.match | headers | | amq.rabbitmq.log | topic | | amq.rabbitmq.trace | topic | | amq.topic | topic | | my.topic | topic | +--------------------+---------+
需要注意:这边我们只创建了一个 Exchange,类型为 Topic,和之前创建的test
队列,目前没有任何关系。
- 然后再创建一个 Binding:
[root@node1 ~]# rabbitmqadmin declare binding source=my.topic destination=test routing_key=my.# binding declared [root@node1 ~]# rabbitmqadmin list bindings +----------+-------------+-------------+ | source | destination | routing_key | +----------+-------------+-------------+ | | test | test | | my.topic | test | my.# | +----------+-------------+-------------+
需要注意:我们上面创建的 Binding(最后那个),就是将名称为my.topic
的 Exchange 和名称为test
的 Queue 关联起来,并且设置的routing_key
规则为my.#
。
- 下面我们发布两条消息:
[root@node1 ~]# rabbitmqadmin publish routing_key=my.test exchange=my.topic payload="hello world by my.test" Message published [root@node1 ~]# rabbitmqadmin publish routing_key=my.test.test exchange=my.topic payload="hello world by my.test.test" Message published [root@node1 ~]# rabbitmqadmin list queues +------+----------+ | name | messages | +------+----------+ | test | 2 | +------+----------+
需要注意,发布的两条消息都指定了my.topic
的 Exchange,但消息的routing_key
不同,但都成功发布到test
队列了,原因是my.test
和my.test.test
都符合my.#
路由规则。
- 下面,我们对两条消息进行消费:
[root@node1 ~]# rabbitmqadmin get queue=test requeue=false +-------------+----------+---------------+------------------------+---------------+------------------+------------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered | +-------------+----------+---------------+------------------------+---------------+------------------+------------+-------------+ | my.test | my.topic | 1 | hello world by my.test | 22 | string | | False | +-------------+----------+---------------+------------------------+---------------+------------------+------------+-------------+ [root@node1 ~]# rabbitmqadmin get queue=test requeue=false +--------------+----------+---------------+-----------------------------+---------------+------------------+------------+-------------+ | routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered | +--------------+----------+---------------+-----------------------------+---------------+------------------+------------+-------------+ | my.test.test | my.topic | 0 | hello world by my.test.test | 27 | string | | False | +--------------+----------+---------------+-----------------------------+---------------+------------------+------------+-------------+ [root@node1 ~]# rabbitmqadmin list queues +------+----------+ | name | messages | +------+----------+ | test | 0 | +------+----------+
rabbitmqadmin 命令汇总
命令 | 解释 |
---|---|
rabbitmqadmin list users | 查看所有用户 User |
rabbitmqadmin list users name | 查看所有用户名 Username |
rabbitmqadmin list users tags | 查看所有用户角色 |
rabbitmqadmin list vhosts | 查看所有虚拟主机 |
rabbitmqadmin list connections | 查看所有连接 |
rabbitmqadmin list exchanges | 查看所有路由 Exchange |
rabbitmqadmin list bindings | 查看所有路由与队列的关系绑定 Binding |
rabbitmqadmin list permissions | 查看所有角色的权限 Permission |
rabbitmqadmin list channels | 查看所有通道 Channel |
rabbitmqadmin list consumers | 查看所有消费者 Consumer |
rabbitmqadmin list queues | 查看所有消息队列 Queue |
rabbitmqadmin list nodes | 查看所有节点 Node |
rabbitmqadmin show overview | 概览 Overview |
rabbitmqadmin list bindings source destination_type destination properties_key | 查看所有路由与队列的关系绑定的详细信息 Binding |
rabbitmqadmin declare queue name=test durable=true | 定义一个队列queue,durable=true代表持久化打开。 |
rabbitmqadmin declare exchange name=my.fanout type=fanout | 定义一个Fanout路由 |
rabbitmqadmin declare exchange name=my.direct type=direct | 定义一个Direct路由 |
rabbitmqadmin declare exchange name=my.topic type=topic | 定义一个Topic路由 |
rabbitmqadmin declare binding source=my.fanout destination=test routing_key=first | 定义 binding |
rabbitmqadmin publish routing_key=test payload="hello world" | 发布一条消息 |
rabbitmqadmin publish routing_key=my.test exchange=my.topic payload="hello world" | 使用路由转发消息 |
rabbitmqadmin get queue=test requeue=true | 查看消息,不消费 |
rabbitmqadmin get queue=test requeue=false | 查看消息,并消费 |
rabbitmqadmin purge queue name=test | 删除队列中的所有消息 |
rabbitmqadmin delete queue name=hello | 删除消息队列 Queue |
rabbitmqadmin delete user name=test | 删除用户 User |
rabbitmqadmin delete exchange name=test | 删除路由器 Exchange |
rabbitmqadmin delete binding source='kk' destination_type=queue destination=test properties_key=test | 删除路由器与消息队列的关系绑定 Binding |
rabbitmqadmin -f raw_json list users | raw_json 格式化输出 |
rabbitmqadmin -f long list users | 格式化输出 |
rabbitmqadmin -f pretty_json list users | pretty_json 格式化输出 |
rabbitmqadmin -f kvp list users | 格式化输出 |
rabbitmqadmin -f tsv list users | 格式化输出 |
rabbitmqadmin -f table list users | table 格式化输出 |
rabbitmqadmin -f bash list users | bash 格式化输出 |
参考资料:
- https://www.cnblogs.com/xishuai/p/rabbitmq-cli-rabbitmqadmin.html
- https://www.jianshu.com/p/8414cd70bef1