原文地址 http://soft.dog/2016/04/20/RabbitMQ-cli-rabbitmqadmin/
一顿仇、前言
RabbitMQ 是一個(gè)使用 Erlang 編寫的開源消息隊(duì)列中間件霹琼,被廣泛使用在各種應(yīng)用場(chǎng)景中
一般對(duì)于它的監(jiān)控和管理可以通過web來完成,詳細(xì)可以參考 RabbitMQ 監(jiān)控
但生產(chǎn)環(huán)境中經(jīng)常沒有訪問web管理界面的條件赚楚,只提供了CLI界面夕晓,或者有些自動(dòng)化的需求通過web界面無法完成抄课,這時(shí)有沒有一種直接在CLI環(huán)境下進(jìn)行管理的方法呢虏两,官方提供的 rabbitmqadmin 命令正好可以滿足這類需求
對(duì)于運(yùn)維來說尝苇,個(gè)人感覺更傾向使用CLI的方式铛只,因?yàn)殡m然web的界面更友好,但是明顯不如CLI快捷糠溜,CLI也可以結(jié)合其它命令進(jìn)行更進(jìn)一步的處理淳玩,比如將關(guān)鍵信息查出來后提供給集中的監(jiān)控系統(tǒng)以觸發(fā)報(bào)警
目前 rabbitmqadmin 可以完成以下任務(wù):
- 列出 exchanges, queues, bindings, vhosts, users, permissions, connections and channels
- 看到匯總信息
- 申明和清除 exchanges, queues, bindings, vhosts, users and permissions
- 發(fā)布和獲取消息
- 關(guān)閉連接和清空隊(duì)列
- 導(dǎo)入導(dǎo)出配置
這里分享一下 rabbitmqadmin 的基本操作,詳細(xì)可以參考 http://www.rabbitmq.com/management-cli.html
二非竿、RabbitMQ的安裝
RabbitMQ的安裝請(qǐng)參考我的另一篇總結(jié)01_RabbitMQ的安裝蜕着。
三、rabbitmqadmin
在web UI管理界面中汽馋,的 /cli/ 下可以下載此腳本:
點(diǎn)擊【here】會(huì)呈現(xiàn)腳本內(nèi)容:
右鍵另存為下載即可侮东。默認(rèn)名稱為rabbitmqadmin圈盔。
但是有些情況下,并沒條件使用Web UI悄雅,使用下面的方法一樣可以獲取這個(gè)腳本:
[root@h102 rabbitmq]# wget http://localhost:15672/cli/rabbitmqadmin
--2016-04-18 19:47:50-- http://localhost:15672/cli/rabbitmqadmin
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:15672... failed: Connection refused.
Connecting to localhost|127.0.0.1|:15672... connected.
HTTP request sent, awaiting response... 200 OK
Length: 34504 (34K) [text/plain]
Saving to: “rabbitmqadmin”
100%[===================================================================================================>] 34,504 --.-K/s in 0s
2016-04-18 19:47:50 (155 MB/s) - “rabbitmqadmin” saved [34504/34504]
[root@h102 rabbitmq]# ll rabbitmqadmin
-rw-r--r-- 1 root root 34504 Apr 18 19:29 rabbitmqadmin
[root@h102 rabbitmq]# file rabbitmqadmin
rabbitmqadmin: a /usr/bin/env python script text executable
[root@h102 rabbitmq]#
[root@h102 rabbitmq]# chmod +x rabbitmqadmin
[root@h102 rabbitmq]# ./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]
--path-prefix=PATH_PREFIX
use specific URI path prefix for the RabbitMQ HTTP API
(default: blank string) [default: ]
--vhost=VHOST, -V VHOST
connect to vhost VHOST [default: all vhosts for list,
'/' for declare]
--username=USERNAME, -u USERNAME
connect using username USERNAME [default: guest]
--password=PASSWORD, -p PASSWORD
connect using password PASSWORD [default: guest]
--quiet, -q suppress status messages [default: True]
--ssl, -s connect with ssl [default: False]
--ssl-key-file=SSL_KEY_FILE
PEM format key file for SSL
--ssl-cert-file=SSL_CERT_FILE
PEM format certificate file for SSL
--format=FORMAT, -f FORMAT
format for listing commands - one of [raw_json, long,
pretty_json, kvp, tsv, table, bash] [default: table]
--sort=SORT, -S SORT sort key for listing queries
--sort-reverse, -R reverse the sort order
--depth=DEPTH, -d DEPTH
maximum depth to recurse for listing tables [default:
1]
--bash-completion Print bash completion script [default: False]
--version Display version and exit
More Help
=========
For more help use the help subcommand:
rabbitmqadmin help subcommands # For a list of available subcommands
rabbitmqadmin help config # For help with the configuration file
[root@h102 rabbitmq]#
以上已經(jīng)拿到了rabbitmqadmin這個(gè)python腳本并設(shè)置為可執(zhí)行,接下來設(shè)置為全局可執(zhí)行命令驱敲。
[root@h102 rabbitmq]# vim /etc/profile
將該腳本所在目錄,其添加到/etc/profile中即可宽闲。
PATH后面是所在目錄:
export PATH=$PATH:/usr/local/server/rabbitmq/
保存退出,讓該設(shè)置立即生效:
[root@h102 rabbitmq]# source /etc/profile
四众眨、基礎(chǔ)概念
4.1 架構(gòu)
Producer、Exchange容诬、Binding娩梨、Queue、Consumer 之間的關(guān)系:
Routing Key览徒、Binding Key狈定、Exchange Type 的關(guān)系:
4.2 概念
4.3 投遞過程
消息隊(duì)列的使用過程大概如下:
- 客戶端連接到消息隊(duì)列服務(wù)器,打開一個(gè)channel
- 客戶端聲明一個(gè)exchange习蓬,并設(shè)置相關(guān)屬性
- 客戶端聲明一個(gè)queue纽什,并設(shè)置相關(guān)屬性
- 客戶端使用routing key,在exchange和queue之間建立好綁定關(guān)系
- 客戶端投遞消息到exchange
- 客戶端從指定的queue中消費(fèi)信息
五躲叼、rabbitmqadmin 用法
[root@h102 rabbitmq]# 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]
--path-prefix=PATH_PREFIX
use specific URI path prefix for the RabbitMQ HTTP API
(default: blank string) [default: ]
--vhost=VHOST, -V VHOST
connect to vhost VHOST [default: all vhosts for list,
'/' for declare]
--username=USERNAME, -u USERNAME
connect using username USERNAME [default: guest]
--password=PASSWORD, -p PASSWORD
connect using password PASSWORD [default: guest]
--quiet, -q suppress status messages [default: True]
--ssl, -s connect with ssl [default: False]
--ssl-key-file=SSL_KEY_FILE
PEM format key file for SSL
--ssl-cert-file=SSL_CERT_FILE
PEM format certificate file for SSL
--format=FORMAT, -f FORMAT
format for listing commands - one of [raw_json, long,
pretty_json, kvp, tsv, table, bash] [default: table]
--sort=SORT, -S SORT sort key for listing queries
--sort-reverse, -R reverse the sort order
--depth=DEPTH, -d DEPTH
maximum depth to recurse for listing tables [default:
1]
--bash-completion Print bash completion script [default: False]
--version Display version and exit
More Help
=========
For more help use the help subcommand:
rabbitmqadmin help subcommands # For a list of available subcommands
rabbitmqadmin help config # For help with the configuration file
[root@h102 rabbitmq]# rabbitmqadmin help subcommands
Usage
=====
rabbitmqadmin [options] subcommand
where subcommand is one of:
Display
=======
list users [<column>...]
list vhosts [<column>...]
list connections [<column>...]
list exchanges [<column>...]
list bindings [<column>...]
list permissions [<column>...]
list channels [<column>...]
list parameters [<column>...]
list consumers [<column>...]
list queues [<column>...]
list policies [<column>...]
list nodes [<column>...]
show overview [<column>...]
Object Manipulation
===================
declare queue name=... [node=... auto_delete=... durable=... arguments=...]
declare vhost name=... [tracing=...]
declare user name=... password=... tags=...
declare exchange name=... type=... [auto_delete=... internal=... durable=... arguments=...]
declare policy name=... pattern=... definition=... [priority=... apply-to=...]
declare parameter component=... name=... value=...
declare permission vhost=... user=... configure=... write=... read=...
declare binding source=... destination=... [arguments=... routing_key=... destination_type=...]
delete queue name=...
delete vhost name=...
delete user name=...
delete exchange name=...
delete policy name=...
delete parameter component=... name=...
delete permission vhost=... user=...
delete binding source=... destination_type=... destination=... properties_key=...
close connection name=...
purge queue name=...
Broker Definitions
==================
export <file>
import <file>
Publishing and Consuming
========================
publish routing_key=... [exchange=... payload=... payload_encoding=... properties=...]
get queue=... [count=... requeue=... payload_file=... encoding=...]
* If payload is not specified on publish, standard input is used
* If payload_file is not specified on get, the payload will be shown on
standard output along with the message metadata
* If payload_file is specified on get, count must not be set
[root@h102 rabbitmq]# rabbitmqadmin help config
Usage
=====
rabbitmqadmin [options] subcommand
Configuration File
==================
It is possible to specify a configuration file from the command line.
Hosts can be configured easily in a configuration file and called
from the command line.
Example
=======
# rabbitmqadmin.conf.example START
[host_normal]
hostname = localhost
port = 15672
username = guest
password = guest
declare_vhost = / # Used as default for declare / delete only
vhost = / # Used as default for declare / delete / list
[host_ssl]
hostname = otherhost
port = 15672
username = guest
password = guest
ssl = True
ssl_key_file = /path/to/key.pem
ssl_cert_file = /path/to/cert.pem
# rabbitmqadmin.conf.example END
Use
===
rabbitmqadmin -c rabbitmqadmin.conf.example -N host_normal ...
5.1 查詢
5.1.1 查看 users
[root@h102 rabbitmq]# rabbitmqadmin list users
+-------+-----------------------------+------------------------------+---------------+
| name | hashing_algorithm | password_hash | tags |
+-------+-----------------------------+------------------------------+---------------+
| guest | rabbit_password_hashing_md5 | 7jG486YR6/F0hVLWYfCnqRyxKe4= | administrator |
| test | rabbit_password_hashing_md5 | /iSnvCNZ5m3kvqoqqH4U3dSEqTM= | administrator |
+-------+-----------------------------+------------------------------+---------------+
[root@h102 rabbitmq]# rabbitmqadmin list users name
+-------+
| name |
+-------+
| guest |
| test |
+-------+
[root@h102 rabbitmq]# rabbitmqadmin list users tags
+---------------+
| tags |
+---------------+
| administrator |
| administrator |
+---------------+
[root@h102 rabbitmq]#
5.1.2 查看 vhosts
[root@h102 rabbitmq]# rabbitmqadmin list vhosts
+------+----------+
| name | messages |
+------+----------+
| / | 13 |
+------+----------+
5.1.3 查看 connections
[root@h102 rabbitmq]# rabbitmqadmin list connections
No items
5.1.4 查看 exchanges
[root@h102 rabbitmq]# 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 |
| kk | fanout |
| test | fanout |
+--------------------+---------+
5.1.5 查看 bindings
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | hello | hello |
| | test | test |
| kk | hello | hello |
| kk | test | test |
+--------+-------------+-------------+
5.1.6 查看 permissions
[root@h102 rabbitmq]# rabbitmqadmin list permissions
+-------+-----------+------+-------+-------+
| vhost | configure | read | user | write |
+-------+-----------+------+-------+-------+
| / | .* | .* | guest | .* |
+-------+-----------+------+-------+-------+
[root@h102 rabbitmq]# rabbitmqadmin list permissions read
+------+
| read |
+------+
| .* |
+------+
5.1.7 查看 channels
[root@h102 rabbitmq]# rabbitmqadmin list channels
No items
5.1.8 查看 consumers
[root@h102 rabbitmq]# rabbitmqadmin list consumers
No items
5.1.9 查看 queues
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------+----------+
| name | messages |
+-------+----------+
| hello | 8 |
| test | 5 |
+-------+----------+
5.1.10 查看 查看 policies
[root@h102 rabbitmq]# rabbitmqadmin list policies
No items
5.1.11 查看 nodes
[root@h102 rabbitmq]# rabbitmqadmin list nodes
+-------------+------+----------+
| name | type | mem_used |
+-------------+------+----------+
| rabbit@h102 | disc | 52274376 |
+-------------+------+----------+
5.1.12 查看 overview
[root@h102 rabbitmq]# rabbitmqadmin show overview
+------------------+------------------+-----------------------+----------------------+
| rabbitmq_version | cluster_name | queue_totals.messages | object_totals.queues |
+------------------+------------------+-----------------------+----------------------+
| 3.6.1 | rabbit@h101.temp | 13 | 2 |
+------------------+------------------+-----------------------+----------------------+
5.2 刪除
5.2.1 刪除 queue
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------+----------+
| name | messages |
+-------+----------+
| hello | 8 |
| test | 5 |
+-------+----------+
[root@h102 rabbitmq]# rabbitmqadmin delete queue name=hello
queue deleted
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 5 |
+------+----------+
5.2.2 刪除 user
[root@h102 rabbitmq]# rabbitmqadmin list users
+-------+-----------------------------+------------------------------+---------------+
| name | hashing_algorithm | password_hash | tags |
+-------+-----------------------------+------------------------------+---------------+
| guest | rabbit_password_hashing_md5 | 7jG486YR6/F0hVLWYfCnqRyxKe4= | administrator |
| test | rabbit_password_hashing_md5 | /iSnvCNZ5m3kvqoqqH4U3dSEqTM= | administrator |
+-------+-----------------------------+------------------------------+---------------+
[root@h102 rabbitmq]# rabbitmqadmin delete user name=test
user deleted
[root@h102 rabbitmq]# rabbitmqadmin list users
+-------+-----------------------------+------------------------------+---------------+
| name | hashing_algorithm | password_hash | tags |
+-------+-----------------------------+------------------------------+---------------+
| guest | rabbit_password_hashing_md5 | 7jG486YR6/F0hVLWYfCnqRyxKe4= | administrator |
+-------+-----------------------------+------------------------------+---------------+
5.2.3 刪除 exchange
[root@h102 rabbitmq]# 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 |
| kk | fanout |
| test | fanout |
+--------------------+---------+
[root@h102 rabbitmq]# rabbitmqadmin delete exchange name=test
exchange deleted
[root@h102 rabbitmq]# 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 |
| kk | fanout |
+--------------------+---------+
5.2.4 刪除 binding
[root@h102 rabbitmq]# rabbitmqadmin list bindings source destination_type destination properties_key
+--------+------------------+-------------+----------------+
| source | destination_type | destination | properties_key |
+--------+------------------+-------------+----------------+
| | queue | test | test |
| kk | queue | test | test |
+--------+------------------+-------------+----------------+
[root@h102 rabbitmq]# rabbitmqadmin delete binding source='kk' destination_type=queue destination=test properties_key=test
binding deleted
[root@h102 rabbitmq]# rabbitmqadmin list bindings source destination_type destination properties_key
+--------+------------------+-------------+----------------+
| source | destination_type | destination | properties_key |
+--------+------------------+-------------+----------------+
| | queue | test | test |
+--------+------------------+-------------+----------------+
Note: source芦缰、 destination_type、destination枫慷、properties_key 缺一不可让蕾,否則會(huì)報(bào)錯(cuò)
Tip: vhost、policy或听、parameter探孝、permission 的刪除方法類似 ,connection 的關(guān)閉方法也類似
5.2.5 清空隊(duì)列
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 5 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin purge queue name=test
queue purged
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
5.2.6 格式化輸出
使用 -f 可以指定格式
有如下幾種格式 raw_json, long, pretty_json, kvp, tsv, table, bash
默認(rèn)為 table
[root@h102 rabbitmq]# rabbitmqadmin list users
+-------+-----------------------------+------------------------------+---------------+
| name | hashing_algorithm | password_hash | tags |
+-------+-----------------------------+------------------------------+---------------+
| guest | rabbit_password_hashing_md5 | 7jG486YR6/F0hVLWYfCnqRyxKe4= | administrator |
+-------+-----------------------------+------------------------------+---------------+
[root@h102 rabbitmq]# rabbitmqadmin -f raw_json list users
[{"name":"guest","password_hash":"7jG486YR6/F0hVLWYfCnqRyxKe4=","hashing_algorithm":"rabbit_password_hashing_md5","tags":"administrator"}]
[root@h102 rabbitmq]# rabbitmqadmin -f long list users
--------------------------------------------------------------------------------
name: guest
hashing_algorithm: rabbit_password_hashing_md5
password_hash: 7jG486YR6/F0hVLWYfCnqRyxKe4=
tags: administrator
--------------------------------------------------------------------------------
[root@h102 rabbitmq]# rabbitmqadmin -f pretty_json list users
[
{
"hashing_algorithm": "rabbit_password_hashing_md5",
"name": "guest",
"password_hash": "7jG486YR6/F0hVLWYfCnqRyxKe4=",
"tags": "administrator"
}
]
[root@h102 rabbitmq]# rabbitmqadmin -f kvp list users
name="guest" hashing_algorithm="rabbit_password_hashing_md5" password_hash="7jG486YR6/F0hVLWYfCnqRyxKe4=" tags="administrator"
[root@h102 rabbitmq]# rabbitmqadmin -f tsv list users
name hashing_algorithm password_hash tags
guest rabbit_password_hashing_md5 7jG486YR6/F0hVLWYfCnqRyxKe4= administrator
[root@h102 rabbitmq]# rabbitmqadmin -f table list users
+-------+-----------------------------+------------------------------+---------------+
| name | hashing_algorithm | password_hash | tags |
+-------+-----------------------------+------------------------------+---------------+
| guest | rabbit_password_hashing_md5 | 7jG486YR6/F0hVLWYfCnqRyxKe4= | administrator |
+-------+-----------------------------+------------------------------+---------------+
[root@h102 rabbitmq]# rabbitmqadmin -f bash list users
guest
5.3 聲明定義
5.3.1 定義一個(gè) queue
[root@h102 rabbitmq]# rabbitmqadmin list bindings
No items
[root@h102 rabbitmq]# rabbitmqadmin list queues
No items
[root@h102 rabbitmq]# rabbitmqadmin declare queue name=test durable=true
queue declared
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
PS :
durable=true 代表持久化打開
發(fā)現(xiàn)定義一個(gè)新的queue后神帅,RabbitMQ會(huì)自動(dòng)為之創(chuàng)建一個(gè) binding
5.3.2 發(fā)布一條消息
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=test payload="just for test"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
Tip: 這里有一個(gè)細(xì)節(jié)再姑,我們并未指定任何 exchange ,依舊可以成功發(fā)送消息找御,
是不是不需要 exchange 參與整個(gè)消息的派送過程呢元镀,這和前面說的信息處理流程貌似有沖突呀,先賣個(gè)關(guān)子霎桅,后面再解釋
5.3.3 消費(fèi)一條信息
[root@h102 rabbitmq]# rabbitmqadmin get queue=test requeue=true
+-------------+----------+---------------+---------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+----------+---------------+---------------+---------------+------------------+------------+-------------+
| test | | 0 | just for test | 13 | string | | False |
+-------------+----------+---------------+---------------+---------------+------------------+------------+-------------+
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin get queue=test requeue=false
+-------------+----------+---------------+---------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+----------+---------------+---------------+---------------+------------------+------------+-------------+
| test | | 0 | just for test | 13 | string | | True |
+-------------+----------+---------------+---------------+---------------+------------------+------------+-------------+
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
5.3.4 定義一個(gè) exchange
我們前面發(fā)布消息的過程中并未指定exchange栖疑,依舊成功發(fā)布了,事實(shí)上只是未明確指出滔驶,系統(tǒng)還是幫我們指了遇革,RabbitMQ 的邏輯中是沒法繞過 exchange 而直接給queue發(fā)送消息的
exchange 有以下幾種類型:
direct,topic,headers萝快,fanout
Fanout锻霎、Direct、Topic 三種 Exchange Type 的區(qū)別:
系統(tǒng)中默認(rèn)就有如下 exchange:
[root@h102 rabbitmq]# 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 |
+--------------------+---------+
[root@h102 rabbitmq]#
我們創(chuàng)建了一個(gè) queue 后揪漩,系統(tǒng)自動(dòng)為它定義了一個(gè) binding:
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
從中我們看出了一些端倪旋恼,我們不手動(dòng)指定 exchange時(shí),使用的默認(rèn) exchange是空字符串(系統(tǒng)中的第一個(gè)exchange奄容,binding中的source部分)冰更,并且這個(gè)默認(rèn)的exchange是 direct 類型,這種隱式調(diào)用確保了我的消息準(zhǔn)確投遞昂勒。
這里再定義三個(gè)exchange 分屬三種類型:
[root@h102 rabbitmq]# 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 |
+--------------------+---------+
[root@h102 rabbitmq]# rabbitmqadmin declare exchange name=my.fanout type=fanout
exchange declared
[root@h102 rabbitmq]# rabbitmqadmin declare exchange name=my.direct type=direct
exchange declared
[root@h102 rabbitmq]# rabbitmqadmin declare exchange name=my.topic type=topic
exchange declared
[root@h102 rabbitmq]# 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.direct | direct |
| my.fanout | fanout |
| my.topic | topic |
+--------------------+---------+
[root@h102 rabbitmq]#
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
5.3.5 定義 binding
上面申明了幾個(gè) exchange 蜀细,嘗試發(fā)布一條信息:
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=test exchange=my.fanout payload="just for test"
Message published but NOT routed
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=test payload="just for test2"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
指定 exchange=my.fanout 后,報(bào) Message published but NOT routed 戈盈,然后檢查queue 發(fā)現(xiàn)并產(chǎn)生新消息奠衔,而直接不指定卻成功發(fā)布了,原因是沒有binding奕谭,exchange 并不知道將數(shù)據(jù)轉(zhuǎn)發(fā)給誰
binding 定義了 exchange 與 queue 的關(guān)系涣觉,并且限定了路由的部分規(guī)則
信息路由規(guī)則一部分由 exchange的類型決定 痴荐,一部分由 binding 關(guān)系決定血柳,binding 的 key 還能起到甄選信息的作用。
我們創(chuàng)建一個(gè) binding:
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
[root@h102 rabbitmq]# rabbitmqadmin declare binding source=my.fanout destination=test routing_key=first
binding declared
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+-----------+-------------+-------------+
| source | destination | routing_key |
+-----------+-------------+-------------+
| | test | test |
| my.fanout | test | first |
+-----------+-------------+-------------+
再次嘗試發(fā)布信息:
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin get queue=test requeue=true
No items
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=first exchange=my.fanout payload="just for test1"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=first payload="just for test2"
Message published but NOT routed
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin get queue=test requeue=true
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| first | my.fanout | 0 | just for test1 | 14 | string | | False |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+
5.3.6 fanout 的特性
定義第二個(gè)queue 生兆,也使用 my.fanout binding 起來难捌,取 routing_key 為 second:
[root@h102 rabbitmq]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
[root@h102 rabbitmq]# rabbitmqadmin declare queue name=test.fanout durable=true
queue declared
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.fanout | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+-----------+-------------+-------------+
| source | destination | routing_key |
+-----------+-------------+-------------+
| | test | test |
| | test.fanout | test.fanout |
| my.fanout | test | first |
+-----------+-------------+-------------+
[root@h102 rabbitmq]#
[root@h102 rabbitmq]# rabbitmqadmin declare binding source=my.fanout destination=test.fanout routing_key=second
binding declared
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+-----------+-------------+-------------+
| source | destination | routing_key |
+-----------+-------------+-------------+
| | test | test |
| | test.fanout | test.fanout |
| my.fanout | test | first |
| my.fanout | test.fanout | second |
+-----------+-------------+-------------+
嘗試再發(fā)一條數(shù)據(jù)到 my.fanout 中:
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.fanout | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=second exchange=my.fanout payload="just for test3"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.fanout | 1 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin get queue=test requeue=true
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| first | my.fanout | 1 | just for test1 | 14 | string | | True |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
[root@h102 rabbitmq]# rabbitmqadmin get queue=test.fanout requeue=true
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| second | my.fanout | 0 | just for test3 | 14 | string | | False |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.fanout | 1 |
+-------------+----------+
這次我們使用 routing_key=first 來投遞消息:
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.fanout | 1 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin purge queue name=test
queue purged
[root@h102 rabbitmq]# rabbitmqadmin purge queue name=test.fanout
queue purged
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.fanout | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=first exchange=my.fanout payload="just for test4"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.fanout | 1 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin get queue=test requeue=true
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| first | my.fanout | 0 | just for test4 | 14 | string | | False |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
[root@h102 rabbitmq]# rabbitmqadmin get queue=test.fanout requeue=true
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| first | my.fanout | 0 | just for test4 | 14 | string | | False |
+-------------+-----------+---------------+----------------+---------------+------------------+------------
發(fā)現(xiàn)結(jié)果一樣,應(yīng)證了前面說的 routing_key 會(huì)被忽略的說法鸦难,但是不能不指定根吁,否則會(huì)報(bào)錯(cuò):
[root@h102 rabbitmq]# rabbitmqadmin publish exchange=my.fanout payload="just for test5"
ERROR: mandatory argument "routing_key" required
rabbitmqadmin --help for help
[root@h102 rabbitmq]#
5.3.7 direct 的特性
定義第三個(gè)queue ,使用 my.direct binding 起來:
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.fanout | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin declare queue name=test.direct durable=true
queue declared
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.direct | 0 |
| test.fanout | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin declare binding source=my.direct destination=test routing_key=third
binding declared
[root@h102 rabbitmq]# rabbitmqadmin declare binding source=my.direct destination=test.direct routing_key=fourth
binding declared
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+-----------+-------------+-------------+
| source | destination | routing_key |
+-----------+-------------+-------------+
| | test | test |
| | test.direct | test.direct |
| | test.fanout | test.fanout |
| my.direct | test | third |
| my.direct | test.direct | fourth |
| my.fanout | test | first |
| my.fanout | test.fanout | second |
+-----------+-------------+-------------+
[root@h102 rabbitmq]#
嘗試分別使用 third 和 fourth 的 routing_key 來發(fā)布消息:
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.direct | 0 |
| test.fanout | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=third exchange=my.direct payload="just for test6"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.direct | 0 |
| test.fanout | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin get queue=test requeue=true
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| third | my.direct | 0 | just for test6 | 14 | string | | False |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=fourth exchange=my.direct payload="just for test7"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.direct | 1 |
| test.fanout | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin get queue=test.direct requeue=true
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
| fourth | my.direct | 0 | just for test7 | 14 | string | | False |
+-------------+-----------+---------------+----------------+---------------+------------------+------------+-------------+
[root@h102 rabbitmq]#
從反饋結(jié)果來看合蔽,direct 的 exchange 就像點(diǎn)對(duì)點(diǎn)通信击敌,fanout 的 exchange 就像是廣播
5.3.4 topic 的特性
定義第四個(gè)queue ,使用 my.topic binding 起來
[root@h102 rabbitmq]# rabbitmqadmin purge queue name=test
queue purged
[root@h102 rabbitmq]# rabbitmqadmin purge queue name=test.direct
queue purged
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.direct | 0 |
| test.fanout | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin declare queue name=test.topic durable=true
queue declared
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.direct | 0 |
| test.fanout | 0 |
| test.topic | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin declare binding source=my.topic destination=test routing_key=*.hard.*
binding declared
[root@h102 rabbitmq]# rabbitmqadmin declare binding source=my.topic destination=test.topic routing_key=cheap.#
binding declared
[root@h102 rabbitmq]# rabbitmqadmin declare binding source=my.topic destination=test.direct routing_key=*.*.food
binding declared
[root@h102 rabbitmq]# rabbitmqadmin declare binding source=my.topic destination=test.fanout routing_key=*.*.food
binding declared
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+-----------+-------------+-------------+
| source | destination | routing_key |
+-----------+-------------+-------------+
| | test | test |
| | test.direct | test.direct |
| | test.fanout | test.fanout |
| | test.topic | test.topic |
| my.direct | test | third |
| my.direct | test.direct | fourth |
| my.fanout | test | first |
| my.fanout | test.fanout | second |
| my.topic | test | *.hard.* |
| my.topic | test.direct | *.*.food |
| my.topic | test.fanout | *.*.food |
| my.topic | test.topic | cheap.# |
+-----------+-------------+-------------+
如果不使用 * 和 # 拴事,那么 topic 的特性就和 direct 一樣了:
[root@h102 rabbitmq]# rabbitmqadmin declare binding source=my.topic destination=test.fanout routing_key=xtest
binding declared
[root@h102 rabbitmq]# rabbitmqadmin list bindings
+-----------+-------------+-------------+
| source | destination | routing_key |
+-----------+-------------+-------------+
| | test | test |
| | test.direct | test.direct |
| | test.fanout | test.fanout |
| | test.topic | test.topic |
| my.direct | test | third |
| my.direct | test.direct | fourth |
| my.fanout | test | first |
| my.fanout | test.fanout | second |
| my.topic | test | *.hard.* |
| my.topic | test.direct | *.*.food |
| my.topic | test.fanout | *.*.food |
| my.topic | test.fanout | xtest |
| my.topic | test.topic | cheap.# |
+-----------+-------------+-------------+
[root@h102 rabbitmq]#
嘗試使用以上 routing_key 發(fā)送消息:
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.direct | 0 |
| test.fanout | 0 |
| test.topic | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=a.hard.b exchange=my.topic payload="just for test8"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.direct | 0 |
| test.fanout | 0 |
| test.topic | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=a.hard.food exchange=my.topic payload="just for test9"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.direct | 1 |
| test.fanout | 1 |
| test.topic | 0 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=cheap.soft.food exchange=my.topic payload="just for test10"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.direct | 2 |
| test.fanout | 2 |
| test.topic | 1 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=cheap.hard.drink exchange=my.topic payload="just for test11"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 3 |
| test.direct | 2 |
| test.fanout | 2 |
| test.topic | 2 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=xtest exchange=my.topic payload="just for test12"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 3 |
| test.direct | 2 |
| test.fanout | 3 |
| test.topic | 2 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin publish routing_key=cheap.hard.food exchange=my.topic payload="just for test13"
Message published
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 4 |
| test.direct | 2 |
| test.fanout | 4 |
| test.topic | 3 |
+-------------+----------+
[root@h102 rabbitmq]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 4 |
| test.direct | 3 |
| test.fanout | 4 |
| test.topic | 3 |
+-------------+----------+
[root@h102 rabbitmq]#
到此為止沃斤,我們看到了 topic 的組播,異步特性
六刃宵、rabbitmqadmin命令匯總
wget http://localhost:15672/cli/rabbitmqadmin
rabbitmqadmin --help
rabbitmqadmin help subcommands
rabbitmqadmin help config
rabbitmqadmin list users 查看所有用戶 User
rabbitmqadmin list users name 查看所有用戶名 Username
rabbitmqadmin list users tags 查看所有用戶角色
rabbitmqadmin list vhosts 查看所有虛擬主機(jī)
rabbitmqadmin list connections 查看所有連接
rabbitmqadmin list exchanges 查看所有路由 Exchange
rabbitmqadmin list bindings 查看所有路由與隊(duì)列的關(guān)系綁定binding
rabbitmqadmin list permissions 查看所有角色的權(quán)限 Permission
rabbitmqadmin list channels 查看所有通道 Channel
rabbitmqadmin list consumers 查看所有消費(fèi)者 Consumer
rabbitmqadmin list queues 查看所有消息隊(duì)列 Queue
rabbitmqadmin list nodes 查看所有節(jié)點(diǎn) Node
rabbitmqadmin show overview 概覽 Overview
rabbitmqadmin list bindings source destination_type destination properties_key 查看所有路由與隊(duì)列的關(guān)系綁定的詳細(xì)信息 Binding
rabbitmqadmin purge queue name=test 清空隊(duì)列
rabbitmqadmin -f raw_json list users 格式化輸出
rabbitmqadmin -f long list users 格式化輸出
rabbitmqadmin -f pretty_json list users
rabbitmqadmin -f kvp list users
rabbitmqadmin -f tsv list users
rabbitmqadmin -f table list users
rabbitmqadmin -f bash list users
rabbitmqadmin declare queue name=test durable=true 定義一個(gè)隊(duì)列queue衡瓶,durable=true 代表持久化打開。
rabbitmqadmin publish routing_key=test payload="just for test" 發(fā)布一條消息
rabbitmqadmin get queue=test requeue=true 消費(fèi)一條信息
rabbitmqadmin get queue=test requeue=false
rabbitmqadmin declare exchange name=my.fanout type=fanout 定義一個(gè)Fanout路由
rabbitmqadmin declare exchange name=my.direct type=direct 定義一個(gè)Direct路由
rabbitmqadmin declare exchange name=my.topic type=topic 定義一個(gè)Topic路由
rabbitmqadmin declare binding source=my.fanout destination=test routing_key=first 定義 binding
rabbitmqadmin delete queue name=hello 刪除消息隊(duì)列 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 刪除路由器與消息隊(duì)列的關(guān)系綁定 Binding
rabbitmqadmin publish routing_key=first exchange=my.fanout payload="just for test1" 使用路由轉(zhuǎn)發(fā)消息