• #创建队列">#创建队列
  • #获取队列信息">#获取队列信息
  • #删除队列">#删除队列
  • #完整 API">#完整 API
  • #rabbitmqadmin">#rabbitmqadmin

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

    #创建队列

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

    1. [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
    2. HTTP/1.1 201 Created
    3. server: Cowboy
    4. date: Mon, 29 Jun 2020 02:04:22 GMT
    5. content-length: 0
    6. content-type: application/json
    7. vary: accept, accept-encoding, origin
    8. # rabbit@study 是你的 rabbitmq 节点,可以通过 rabbitmqctl status 查看到
    9. # %2F:就是 / 这个是个特殊符号,所以转移了,默认 vhost 就是 /
    10. Copied!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    对于 -u 和 oath 类似,使用的是 HTTP 基础认证。
    这里的 HTTP API 是完全基于 RESTful 风格的。

    • GET 方法一般用来获取集群、节点、队列、交换器等信息
    • PUT 方法用来创建资源:如交换器、队列之类
    • DELETE 方法用来删除资源
    • POST 方法也是用来创建资源与 PUT 不同的是,POST 创建的是无法用具体名称的资源。比如绑定关系、和发布消息无法指定一个具体的名称

      #获取队列信息

      下面展示使用 GET 来获取前面创建的队列
    1. [root@study ~]# curl -i -u admin:root -XGET http://192.168.4.250:15672/api/queues/%2F/queue
    2. HTTP/1.1 200 OK
    3. server: Cowboy
    4. date: Mon, 29 Jun 2020 02:05:03 GMT
    5. content-length: 1275
    6. content-type: application/json
    7. vary: accept, accept-encoding, origin
    8. cache-control: no-cache
    9. {"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}
    10. Copied!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    #删除队列

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

    1
    2
    3
    4
    5
    6
    7

    #完整 API

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

    1. http://192.168.4.250:15672/api
    2. Copied!

    1

    这里就不记录书上完整的列表了。
    WEB API 通常方便客户端调用。在命令行有类似的工具 :rabbitmqadmin

    #rabbitmqadmin

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

    1. http://{hostname}:15672/cli/rabbitmqadmin
    2. Copied!

    1

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

    1. [root@study ~]# wget http://192.168.4.250:15672/cli/rabbitmqadmin
    2. --2020-06-29 10:30:31-- http://192.168.4.250:15672/cli/rabbitmqadmin
    3. 正在连接 192.168.4.250:15672... 已连接。
    4. 已发出 HTTP 请求,正在等待回应... 200 OK
    5. 长度:37698 (37K) [application/octet-stream]
    6. 正在保存至: rabbitmqadmin
    7. 100%[===========================================================================================================================================================>] 37,698 --.-K/s 用时 0s
    8. 2020-06-29 10:30:31 (739 MB/s) - 已保存 rabbitmqadmin [37698/37698])
    9. # 添加执行权限
    10. [root@study ~]# chmod +x rabbitmqadmin
    11. # 如果使用中出现不能使用的情况,可能是缺少 Python,版本为 2.x;
    12. # 版本根据你用的 RabbitMQ 版本不同而不同,上面官方页面也介绍了
    13. Copied!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

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

    1. # 创建 queue1 的队列
    2. [root@study ~]# ./rabbitmqadmin -u admin -p root declare queue name=queue1
    3. queue declared
    4. # 查看队列列表
    5. [root@study ~]# ./rabbitmqadmin list queues
    6. +------------+----------+
    7. | name | messages |
    8. +------------+----------+
    9. | queue1 | 0 |
    10. | queue2 | 0 |
    11. | queue33 | 0 |
    12. | queue_demo | 149 |
    13. +------------+----------+
    14. Copied!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

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

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