# HTTP API 接口管理

RabbitMQ Management 插件不仅提供了 Web 管理界面,还提供了 HTTP API 接口。

# 创建队列

比如创建一个队列,可以通过 PUT 方法调用 /api/queues/vhost/name 实现

[root@study ~]# curl -i -u admin:root -H "content-type:application/json" -XPUT -d '{"auto_delete":false,"durable":true,"node":"rabbit@study"}' http://192.168.4.250:15672/api/queues/%2F/queue

HTTP/1.1 201 Created
server: Cowboy
date: Mon, 29 Jun 2020 02:04:22 GMT
content-length: 0
content-type: application/json
vary: accept, accept-encoding, origin

# rabbit@study 是你的 rabbitmq 节点,可以通过 rabbitmqctl status 查看到
# %2F:就是 / 这个是个特殊符号,所以转移了,默认 vhost 就是 /
1
2
3
4
5
6
7
8
9
10
11

对于 -u 和 oath 类似,使用的是 HTTP 基础认证。

这里的 HTTP API 是完全基于 RESTful 风格的。

  • GET 方法一般用来获取集群、节点、队列、交换器等信息

  • PUT 方法用来创建资源:如交换器、队列之类

  • DELETE 方法用来删除资源

  • POST 方法也是用来创建资源

    与 PUT 不同的是,POST 创建的是无法用具体名称的资源。比如绑定关系、和发布消息无法指定一个具体的名称

# 获取队列信息

下面展示使用 GET 来获取前面创建的队列

[root@study ~]# curl -i -u admin:root -XGET http://192.168.4.250:15672/api/queues/%2F/queue
HTTP/1.1 200 OK
server: Cowboy
date: Mon, 29 Jun 2020 02:05:03 GMT
content-length: 1275
content-type: application/json
vary: accept, accept-encoding, origin
cache-control: no-cache

{"consumer_details":[],"incoming":[],"deliveries":[],"messages_details":{"rate":0.0},"messages":0,"messages_unacknowledged_details":{"rate":0.0},"messages_unacknowledged":0,"messages_ready_details":{"rate":0.0},"messages_ready":0,"reductions_details":{"rate":0.0},"reductions":2038,"node":"rabbit@study","arguments":{},"exclusive":false,"auto_delete":false,"durable":true,"vhost":"/","name":"queue","message_bytes_paged_out":0,"messages_paged_out":0,"backing_queue_status":{"mode":"default","q1":0,"q2":0,"delta":["delta","undefined",0,0,"undefined"],"q3":0,"q4":0,"len":0,"target_ram_count":"infinity","next_seq_id":0,"avg_ingress_rate":0.0,"avg_egress_rate":0.0,"avg_ack_ingress_rate":0.0,"avg_ack_egress_rate":0.0},"head_message_timestamp":null,"message_bytes_persistent":0,"message_bytes_ram":0,"message_bytes_unacknowledged":0,"message_bytes_ready":0,"message_bytes":0,"messages_persistent":0,"messages_unacknowledged_ram":0,"messages_ready_ram":0,"messages_ram":0,"garbage_collection":{"minor_gcs":0,"fullsweep_after":65535,"min_heap_size":233,"min_bin_vheap_size":46422,"max_heap_size":0},"state":"running","recoverable_slaves":null,"consumers":0,"exclusive_consumer_tag":null,"policy":null,"consumer_utilisation":null,"idle_since":"2020-06-29 1:51:47","memory":10792}
1
2
3
4
5
6
7
8
9
10

# 删除队列

[root@study ~]# curl -i -u admin:root -XDELETE http://192.168.4.250:15672/api/queues/%2F/queue
HTTP/1.1 204 No Content
server: Cowboy
date: Mon, 29 Jun 2020 02:06:44 GMT
content-length: 0
content-type: application/json
vary: accept, accept-encoding, origin
1
2
3
4
5
6
7

# 完整 API

这可以访问 web 管理界面的 API 端点获取

http://192.168.4.250:15672/api
1

这里就不记录书上完整的列表了。

WEB API 通常方便客户端调用。在命令行有类似的工具 :rabbitmqadmin

# rabbitmqadmin

需要你安装该工具(是一个专用的 HTTP 客户端),从 官方下载,也可以通过本机 api 下载

 http://{hostname}:15672/cli/rabbitmqadmin
1

笔者这里通过如下方式下载


[root@study ~]# wget http://192.168.4.250:15672/cli/rabbitmqadmin
--2020-06-29 10:30:31--  http://192.168.4.250:15672/cli/rabbitmqadmin
正在连接 192.168.4.250:15672... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:37698 (37K) [application/octet-stream]
正在保存至: “rabbitmqadmin”

100%[===========================================================================================================================================================>] 37,698      --.-K/s 用时 0s

2020-06-29 10:30:31 (739 MB/s) - 已保存 “rabbitmqadmin” [37698/37698])

# 添加执行权限
[root@study ~]# chmod +x rabbitmqadmin
# 如果使用中出现不能使用的情况,可能是缺少 Python,版本为 2.x; 
# 版本根据你用的 RabbitMQ 版本不同而不同,上面官方页面也介绍了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

就可以通过该工具访问了,如下

# 创建 queue1 的队列
[root@study ~]# ./rabbitmqadmin -u admin -p root declare queue name=queue1
queue declared

# 查看队列列表
[root@study ~]# ./rabbitmqadmin list queues
+------------+----------+
|    name    | messages |
+------------+----------+
| queue1     | 0        |
| queue2     | 0        |
| queue33    | 0        |
| queue_demo | 149      |
+------------+----------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14

还可以通过 --help 来自力更生,查看支持哪些语法参数


[root@study ~]# ./rabbitmqadmin --help
Usage
=====
  rabbitmqadmin [options] subcommand

Options
=======
--help, -h              show this help message and exit
--config=CONFIG, -c CONFIG
                        configuration file [default: ~/.rabbitmqadmin.conf]
--node=NODE, -N NODE    node described in the configuration file [default:
                        'default' only if configuration file is specified]
--host=HOST, -H HOST    connect to host HOST [default: localhost]
--port=PORT, -P PORT    connect to port PORT [default: 15672]
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16