rabbitmqctl 命令行管理工具
1. 用户管理
用户管理包括增加用户,删除用户,查看用户列表,修改用户密码。
(1) 新增一个用户
rabbitmqctl add_user Username Password
(2) 删除一个用户
rabbitmqctl delete_user Username
(3) 修改用户的密码
rabbitmqctl change_password Username Newpassword
(4) 查看当前用户列表
rabbitmqctl list_users
2. 用户角色
用户角色可分为五类,超级管理员, 监控者, 策略制定者, 普通管理者以及其他。
(1) 超级管理员(administrator)
可登陆管理控制台(启用management plugin的情况下),可查看所有的信息,并且可以对用户,策略(policy)进行操作。
(2) 监控者(monitoring)
可登陆管理控制台(启用management plugin的情况下),同时可以查看rabbitmq节点的相关信息(进程数,内存使用情况,磁盘使用情况等)
(3) 策略制定者(policymaker)
可登陆管理控制台(启用management plugin的情况下), 同时可以对policy进行管理。但无法查看节点的相关信息(上图红框标识的部分)。
与administrator的对比,administrator能看到这些内容
(4) 普通管理者(management)
仅可登陆管理控制台(启用management plugin的情况下),无法看到节点信息,也无法对策略进行管理。
(5) 其他
无法登陆管理控制台,通常就是普通的生产者和消费者。
了解了这些后,就可以根据需要给不同的用户设置不同的角色,以便按需管理。
设置用户角色的命令为:
rabbitmqctl set_user_tags User Tag
User为用户名, Tag为角色名(对应于上面的administrator,monitoring,policymaker,management,或其他自定义名称)。
也可以给同一用户设置多个角色,例如
rabbitmqctl set_user_tags hncscwc monitoring policymaker
3. 用户权限
用户权限指的是用户对exchange,queue的操作权限,包括配置权限,读写权限。配置权限会影响到exchange,queue的声明和删除。读写权限影响到从queue里取消息,向exchange发送消息以及queue和exchange的绑定(bind)操作。
例如: 将queue绑定到某exchange上,需要具有queue的可写权限,以及exchange的可读权限;向exchange发送消息需要具有exchange的可写权限;从queue里取数据需要具有queue的可读权限。详细请参考官方文档中"How permissions work"部分。
相关命令为:
(1) 设置用户权限
rabbitmqctl set_permissions -p VHostPath User ConfP WriteP ReadP
(2) 查看(指定hostpath)所有用户的权限信息
rabbitmqctl list_permissions [-p VHostPath]
(3) 查看指定用户的权限信息
rabbitmqctl list_user_permissions User
(4) 清除用户的权限信息
rabbitmqctl clear_permissions [-p VHostPath] User
rabbitmqadmin命令行管理工具
[root@contoso ~]# rabbitmq-plugins list
Configured: E = explicitly enabled; e = implicitly enabled
| Status: * = running on rabbit@contoso
|/
[e*] amqp_client 3.6.10
[e*] cowboy 1.0.4
[e*] cowlib 1.0.2
[ ] rabbitmq_amqp1_0 3.6.10
[ ] rabbitmq_auth_backend_ldap 3.6.10
[ ] rabbitmq_auth_mechanism_ssl 3.6.10
[ ] rabbitmq_consistent_hash_exchange 3.6.10
[ ] rabbitmq_event_exchange 3.6.10
[ ] rabbitmq_federation 3.6.10
[ ] rabbitmq_federation_management 3.6.10
[ ] rabbitmq_jms_topic_exchange 3.6.10
[E*] rabbitmq_management 3.6.10
[e*] rabbitmq_management_agent 3.6.10
[ ] rabbitmq_management_visualiser 3.6.10
[ ] rabbitmq_mqtt 3.6.10
[ ] rabbitmq_recent_history_exchange 3.6.10
[ ] rabbitmq_sharding 3.6.10
[ ] rabbitmq_shovel 3.6.10
[ ] rabbitmq_shovel_management 3.6.10
[ ] rabbitmq_stomp 3.6.10
[ ] rabbitmq_top 3.6.10
[ ] rabbitmq_tracing 3.6.10
[ ] rabbitmq_trust_store 3.6.10
[e*] rabbitmq_web_dispatch 3.6.10
[ ] rabbitmq_web_mqtt 3.6.10
[ ] rabbitmq_web_mqtt_examples 3.6.10
[ ] rabbitmq_web_stomp 3.6.10
[ ] rabbitmq_web_stomp_examples 3.6.10
[ ] sockjs 0.3.4
[root@contoso ~]#
Producer、Exchange、Binding、Queue、Consumer 之间的关系:
Routing Key、Binding Key、Exchange Type 的关系:
概念
Item | Comment |
---|---|
Exchange | 消息交换机,它指定消息按什么规则,路由到哪个队列 |
Queue | 消息队列,每个消息都会被投入到一个或多个队列 |
Binding | 绑定,它的作用就是把exchange和queue按照路由规则绑定起来 |
Routing Key | 路由关键字,exchange根据这个关键字进行消息投递 |
Vhost | 虚拟主机,可以开设多个vhost,用作不同用户的权限分离 |
Producer | 消息生产者,就是投递消息的程序 |
Consumer | 消息消费者,就是接受消息的程序 |
Channel | 消息通道,在客户端的每个连接里,可建立多个channel,每个channel代表一个会话任务 |
投递过程
消息队列的使用过程大概如下:
- 1.客户端连接到消息队列服务器,打开一个channel
- 2.客户端声明一个exchange,并设置相关属性
- 3.客户端声明一个queue,并设置相关属性
- 4.客户端使用routing key,在exchange和queue之间建立好绑定关系
- 5.客户端投递消息到exchange
- 6.客户端从指定的queue中消费信息
[root@contoso ~]# rabbitmqadmin list users
+-------+--------------------------------+--------------------------------------------------+---------------+
| name | hashing_algorithm | password_hash | tags |
+-------+--------------------------------+--------------------------------------------------+---------------+
| guest | rabbit_password_hashing_sha256 | FVQaTL7eZ2r5Qym3rv89F0nRP9ewyjpVtCglxZ3/mKrT1QEa | administrator |
+-------+--------------------------------+--------------------------------------------------+---------------+
[root@contoso ~]# rabbitmqctl list_users
Listing users
guest [administrator]
[root@contoso ~]# rabbitmqctl add_user testing testing ##新增一个名称为testing的用户,密码为tesing
Creating user "testing"
[root@contoso ~]# rabbitmqctl list_users
Listing users
testing []
guest [administrator]
[root@contoso ~]# rabbitmqctl set_user_tags testing administrator ##设置用户testing的角色为administrator
Setting tags for user "testing" to [administrator]
[root@contoso ~]# rabbitmqctl list_users ##查看当前用户列表
Listing users
testing [administrator]
guest [administrator]
[root@contoso ~]# rabbitmqadmin list users ## 查看 users
+---------+--------------------------------+--------------------------------------------------+---------------+
| name | hashing_algorithm | password_hash | tags |
+---------+--------------------------------+--------------------------------------------------+---------------+
| guest | rabbit_password_hashing_sha256 | FVQaTL7eZ2r5Qym3rv89F0nRP9ewyjpVtCglxZ3/mKrT1QEa | administrator |
| testing | rabbit_password_hashing_sha256 | o/51IGKGHsAYNqXGXhF14cT5zRat8bfk8va3v4w1p6i/+7ow | administrator |
+---------+--------------------------------+--------------------------------------------------+---------------+
[root@contoso ~]# rabbitmqadmin list users name
+---------+
| name |
+---------+
| guest |
| testing |
+---------+
[root@contoso ~]# rabbitmqadmin list users tags
+---------------+
| tags |
+---------------+
| administrator |
| administrator |
+---------------+
[root@contoso ~]# rabbitmqadmin list vhosts ##查看 vhosts
+------+----------+
| name | messages |
+------+----------+
| / | |
| foo | |
+------+----------+
[root@contoso ~]# rabbitmqadmin list connections ##查看 connections
No items
[root@contoso ~]# rabbitmqadmin list exchanges ##查看 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-new-exchange | fanout |
+--------------------+---------+
[root@contoso ~]# rabbitmqadmin list bindings ##查看 bindings
No items
[root@contoso ~]# rabbitmqadmin list permissions ##查看 permissions
+-------+-----------+------+-------+-------+
| vhost | configure | read | user | write |
+-------+-----------+------+-------+-------+
| / | .* | .* | guest | .* |
+-------+-----------+------+-------+-------+
[root@contoso ~]# rabbitmqadmin list channels ##查看 channels
No items
[root@contoso ~]# rabbitmqadmin list parameters ##查看 parameters
No items
[root@contoso ~]# rabbitmqadmin list consumers ##查看consumers
No items
[root@contoso ~]# rabbitmqadmin list queues ##查看queues
No items
[root@contoso ~]# rabbitmqadmin list policies ##查看policies
No items
[root@contoso ~]# rabbitmqadmin list nodes ##查看nodes
+----------------+------+----------+
| name | type | mem_used |
+----------------+------+----------+
| rabbit@contoso | disc | 58387040 |
+----------------+------+----------+
[root@contoso ~]# rabbitmqadmin show overview ##查看overview
+------------------+----------------+-----------------------+----------------------+
| rabbitmq_version | cluster_name | queue_totals.messages | object_totals.queues |
+------------------+----------------+-----------------------+----------------------+
| 3.6.10 | rabbit@contoso | | 0 |
+------------------+----------------+-----------------------+----------------------+
[root@contoso ~]#
格式化输出
使用 -f
可以指定格式
有如下几种格式 raw_json, long, pretty_json, kvp, tsv, table, bash
默认为 table
[root@contoso ~]# rabbitmqadmin -f raw_json list users[{"name":"guest","password_hash":"FVQaTL7eZ2r5Qym3rv89F0nRP9ewyjpVtCglxZ3/mKrT1QEa","hashing_algorithm":"rabbit_password_hashing_sha256","tags":"administrator"},{"name":"testing","password_hash":"o/51IGKGHsAYNqXGXhF14cT5zRat8bfk8va3v4w1p6i/+7ow","hashing_algorithm":"rabbit_password_hashing_sha256","tags":"administrator"}]
[root@contoso ~]# rabbitmqadmin -f long list users
--------------------------------------------------------------------------------
name: guest
hashing_algorithm: rabbit_password_hashing_sha256
password_hash: FVQaTL7eZ2r5Qym3rv89F0nRP9ewyjpVtCglxZ3/mKrT1QEa
tags: administrator
--------------------------------------------------------------------------------
name: testing
hashing_algorithm: rabbit_password_hashing_sha256
password_hash: o/51IGKGHsAYNqXGXhF14cT5zRat8bfk8va3v4w1p6i/+7ow
tags: administrator
--------------------------------------------------------------------------------
[root@contoso ~]# rabbitmqadmin -f pretty_json list users
[
{
"hashing_algorithm": "rabbit_password_hashing_sha256",
"name": "guest",
"password_hash": "FVQaTL7eZ2r5Qym3rv89F0nRP9ewyjpVtCglxZ3/mKrT1QEa",
"tags": "administrator"
},
{
"hashing_algorithm": "rabbit_password_hashing_sha256",
"name": "testing",
"password_hash": "o/51IGKGHsAYNqXGXhF14cT5zRat8bfk8va3v4w1p6i/+7ow",
"tags": "administrator"
}
]
[root@contoso ~]# rabbitmqadmin -f tsv list users
name hashing_algorithm password_hash tags
guest rabbit_password_hashing_sha256 FVQaTL7eZ2r5Qym3rv89F0nRP9ewyjpVtCglxZ3/mKrT1QEa administrator
testing rabbit_password_hashing_sha256 o/51IGKGHsAYNqXGXhF14cT5zRat8bfk8va3v4w1p6i/+7ow administrator
[root@contoso ~]# rabbitmqadmin -f kvp list users
name="guest" hashing_algorithm="rabbit_password_hashing_sha256" password_hash="FVQaTL7eZ2r5Qym3rv89F0nRP9ewyjpVtCglxZ3/mKrT1QEa" tags="administrator"
name="testing" hashing_algorithm="rabbit_password_hashing_sha256" password_hash="o/51IGKGHsAYNqXGXhF14cT5zRat8bfk8va3v4w1p6i/+7ow" tags="administrator"
[root@contoso ~]# rabbitmqadmin -f table list users
+---------+--------------------------------+--------------------------------------------------+---------------+
| name | hashing_algorithm | password_hash | tags |
+---------+--------------------------------+--------------------------------------------------+---------------+
| guest | rabbit_password_hashing_sha256 | FVQaTL7eZ2r5Qym3rv89F0nRP9ewyjpVtCglxZ3/mKrT1QEa | administrator |
| testing | rabbit_password_hashing_sha256 | o/51IGKGHsAYNqXGXhF14cT5zRat8bfk8va3v4w1p6i/+7ow | administrator |
+---------+--------------------------------+--------------------------------------------------+---------------+
[root@contoso ~]#
定义一个 queue
[root@contoso ~]# rabbitmqadmin declare queue name=test durable=true ## durable=true 代表持久化打开queue declared
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
[root@contoso ~]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
[root@contoso ~]#
发布一条消息
[root@contoso ~]# rabbitmqadmin publish routing_key=test payload="this is a testing" ##未指定exchange默认 exchange name为空
Message published
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
[root@contoso ~]#
消费一条信息
[root@contoso ~]# rabbitmqadmin get queue=test requeue=true+-------------+----------+---------------+-------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+----------+---------------+-------------------+---------------+------------------+------------+-------------+
| test | | 0 | this is a testing | 17 | string | | False |
+-------------+----------+---------------+-------------------+---------------+------------------+------------+-------------+
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
[root@contoso ~]# rabbitmqadmin get queue=test requeue=false
+-------------+----------+---------------+-------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+----------+---------------+-------------------+---------------+------------------+------------+-------------+
| test | | 0 | this is a testing | 17 | string | | True |
+-------------+----------+---------------+-------------------+---------------+------------------+------------+-------------+
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
[root@contoso ~]#
定义一个 exchange
exchange 有以下几种类型
- direct
- topic
- headers
- fanout
- Fanout :进行最简单的广播
- Direct : 最直接的根据整个 routing key 对应
- Topic : 可以使用点分 routing key 的模糊匹配
* (star) can substitute for exactly one word
# (hash) can substitute for zero or more words
headers
系统中默认就有如下 exchange
[root@contoso ~]# 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-new-exchange | fanout | ## 除这条之外
+--------------------+---------+
[root@contoso ~]#
再定义三个exchange 分属三种类型
[root@contoso ~]# rabbitmqadmin declare exchange name=corp.fanout type=fanout
exchange declared
[root@contoso ~]# rabbitmqadmin declare exchange name=corp.direct type=direct
exchange declared
[root@contoso ~]# rabbitmqadmin declare exchange name=corp.topic type=topic
exchange declared
[root@contoso ~]# 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 |
| corp.direct | direct |
| corp.fanout | fanout |
| corp.topic | topic |
| my-new-exchange | fanout |
+--------------------+---------+
[root@contoso ~]#
定义 binding
[root@contoso ~]# rabbitmqadmin list bindings+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=test exchange=corp.fanout payload="this is second testing"
Message published but NOT routed
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 0 |
+------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=test payload="this is third testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
[root@contoso ~]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
[root@contoso ~]#
指定 exchange=corp.fanout 后,报 Message published but NOT routed ,然后检查queue 发现并产生新消息,而直接不指定却成功发布了,原因是没有binding,exchange 并不知道将数据转发给谁
binding 定义了 exchange 与 queue 的关系,并且限定了路由的部分规则
信息路由规则一部分由 exchange的类型决定 ,一部分由 binding 关系决定,binding 的 key 还能起到甄选信息的作用
对于类型为 fanout 的 exchange 比较特别,binding 的 routing_key 会被忽略,直接被 广播
解决Message published but NOT routed,我们创建一个 binding
[root@contoso ~]# rabbitmqadmin list bindings
+--------+-------------+-------------+
| source | destination | routing_key |
+--------+-------------+-------------+
| | test | test |
+--------+-------------+-------------+
[root@contoso ~]# rabbitmqadmin declare binding source=corp.fanout destination=test routing_key=first ##创建一个 binding
binding declared
[root@contoso ~]# rabbitmqadmin list bindings
+-------------+-------------+-------------+
| source | destination | routing_key |
+-------------+-------------+-------------+
| | test | test |
| corp.fanout | test | first |
+-------------+-------------+-------------+
[root@contoso ~]#
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 1 |
+------+----------+
[root@contoso ~]# rabbitmqadmin get queue=test requeue=true
+-------------+----------+---------------+-----------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+----------+---------------+-----------------------+---------------+------------------+------------+-------------+
| test | | 0 | this is third testing | 21 | string | | False |
+-------------+----------+---------------+-----------------------+---------------+------------------+------------+-------------+
[root@contoso ~]# rabbitmqadmin publish routing_key=first exchange=corp.fanout payload="this is first testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 2 |
+------+----------+
[root@contoso ~]#
定义第二个queue ,也使用 corp.fanout binding 起来,取 routing_key 为 second
[root@contoso ~]# rabbitmqadmin list queues
+------+----------+
| name | messages |
+------+----------+
| test | 2 |
+------+----------+
[root@contoso ~]# rabbitmqadmin list bindings
+-------------+-------------+-------------+
| source | destination | routing_key |
+-------------+-------------+-------------+
| | test | test |
| corp.fanout | test | first |
+-------------+-------------+-------------+
[root@contoso ~]# rabbitmqadmin declare queue name=test.fanout durable=true
queue declared
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.fanout | 0 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin list bindings
+-------------+-------------+-------------+
| source | destination | routing_key |
+-------------+-------------+-------------+
| | test | test |
| | test.fanout | test.fanout |
| corp.fanout | test | first |
+-------------+-------------+-------------+
[root@contoso ~]# rabbitmqadmin declare binding source=corp.fanout destination=test.fanout routing_key=second
binding declared
[root@contoso ~]# rabbitmqadmin list bindings
+-------------+-------------+-------------+
| source | destination | routing_key |
+-------------+-------------+-------------+
| | test | test |
| | test.fanout | test.fanout |
| corp.fanout | test | first |
| corp.fanout | test.fanout | second |
+-------------+-------------+-------------+
[root@contoso ~]#
再发一条数据到 corp.fanout 中
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.fanout | 0 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=second exchange=corp.fanout payload="this is just third testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 3 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin get queue=test requeue=true
+-------------+----------+---------------+-----------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+----------+---------------+-----------------------+---------------+------------------+------------+-------------+
| test | | 2 | this is third testing | 21 | string | | True |
+-------------+----------+---------------+-----------------------+---------------+------------------+------------+-------------+
[root@contoso ~]# rabbitmqadmin get queue=test.fanout requeue=true
+-------------+-------------+---------------+----------------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-------------+---------------+----------------------------+---------------+------------------+------------+-------------+
| second | corp.fanout | 0 | this is just third testing | 26 | string | | False |
+-------------+-------------+---------------+----------------------------+---------------+------------------+------------+-------------+
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 3 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]#
这次我们使用 routing_key=first 来投递消息
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 3 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin purge queue name=test
queue purged
[root@contoso ~]# rabbitmqadmin purge queue name=test.fanout
queue purged
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.fanout | 0 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin list bindings
+-------------+-------------+-------------+
| source | destination | routing_key |
+-------------+-------------+-------------+
| | test | test |
| | test.fanout | test.fanout |
| corp.fanout | test | first |
| corp.fanout | test.fanout | second |
+-------------+-------------+-------------+
[root@contoso ~]#
[root@contoso ~]# rabbitmqadmin publish routing_key=first exchange=corp.fanout payload="this is fourth testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin get queue=test requeue=true
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
| first | corp.fanout | 0 | this is fourth testing | 22 | string | | False |
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
[root@contoso ~]# rabbitmqadmin get queue=test.fanout requeue=true
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
| first | corp.fanout | 0 | this is fourth testing | 22 | string | | False |
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
[root@contoso ~]#
direct 的特性
定义第三个queue ,使用 corp.direct binding 起来
[root@contoso ~]# rabbitmqadmin list queues+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin declare queue name=test.direct durable=true
queue declared
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.direct | 0 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin declare binding source=corp.direct destination=test routing_key=third
binding declared
[root@contoso ~]# rabbitmqadmin declare binding source=corp.direct destination=test.direct routing_key=fourth
binding declared
[root@contoso ~]# rabbitmqadmin list bindings
+-------------+-------------+-------------+
| source | destination | routing_key |
+-------------+-------------+-------------+
| | test | test |
| | test.direct | test.direct |
| | test.fanout | test.fanout |
| corp.direct | test | third |
| corp.direct | test.direct | fourth |
| corp.fanout | test | first |
| corp.fanout | test.fanout | second |
+-------------+-------------+-------------+
[root@contoso ~]#
分别使用 third 和 fourth 的 routing_key 来发布消息
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.direct | 0 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=third exchange=corp.direct payload="this is sixth testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.direct | 0 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin get queue=test requeue=true
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
| first | corp.fanout | 1 | this is fourth testing | 22 | string | | True |
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
[root@contoso ~]# rabbitmqadmin get queue=test requeue=false
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
| first | corp.fanout | 1 | this is fourth testing | 22 | string | | True |
+-------------+-------------+---------------+------------------------+---------------+------------------+------------+-------------+
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.direct | 0 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin get queue=test requeue=true
+-------------+-------------+---------------+-----------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-------------+---------------+-----------------------+---------------+------------------+------------+-------------+
| third | corp.direct | 0 | this is sixth testing | 21 | string | | False |
+-------------+-------------+---------------+-----------------------+---------------+------------------+------------+-------------+
[root@contoso ~]#
[root@contoso ~]# rabbitmqadmin publish routing_key=fourth exchange=corp.direct payload="this is seventh testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.direct | 1 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin get queue=test.direct requeue=true
+-------------+-------------+---------------+-------------------------+---------------+------------------+------------+-------------+
| routing_key | exchange | message_count | payload | payload_bytes | payload_encoding | properties | redelivered |
+-------------+-------------+---------------+-------------------------+---------------+------------------+------------+-------------+
| fourth | corp.direct | 0 | this is seventh testing | 23 | string | | False |
+-------------+-------------+---------------+-------------------------+---------------+------------------+------------+-------------+
[root@contoso ~]#
从返回的结果来看,direct 的 exchange 就像点对点通信,fanout 的 exchange 就像是广播
topic 的组播,异步特性
定义第四个queue ,使用 corp.topic binding 起来
[root@contoso ~]# rabbitmqadmin purge queue name=testqueue purged
[root@contoso ~]# rabbitmqadmin purge queue name=test.direct
queue purged
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.direct | 0 |
| test.fanout | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin purge queue name=test.fanout
queue purged
[root@contoso ~]# rabbitmqadmin declare queue name=test.topic durable=true
queue declared
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.direct | 0 |
| test.fanout | 0 |
| test.topic | 0 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin declare binding source=corp.topic destination=test routing_key=*.hard.*
binding declared
[root@contoso ~]# rabbitmqadmin declare binding source=corp.topic destination=test.topic routing_key=cheap.#
binding declared
[root@contoso ~]# rabbitmqadmin declare binding source=corp.topic destination=test.direct routing_key=*.*.food
binding declared
[root@contoso ~]# rabbitmqadmin declare binding source=corp.topic destination=test.fanout routing_key=*.*.food
binding declared
[root@contoso ~]# rabbitmqadmin list bindings
+-------------+-------------+-------------+
| source | destination | routing_key |
+-------------+-------------+-------------+
| | test | test |
| | test.direct | test.direct |
| | test.fanout | test.fanout |
| | test.topic | test.topic |
| corp.direct | test | third |
| corp.direct | test.direct | fourth |
| corp.fanout | test | first |
| corp.fanout | test.fanout | second |
| corp.topic | test | *.hard.* |
| corp.topic | test.direct | *.*.food |
| corp.topic | test.fanout | *.*.food |
| corp.topic | test.topic | cheap.# |
+-------------+-------------+-------------+
[root@contoso ~]#
如果不使用 * 和 # ,那么 topic 的特性就和 direct 一样了
[root@contoso ~]# rabbitmqadmin declare binding source=corp.topic destination=test.fanout routing_key=xtest
binding declared
[root@contoso ~]# rabbitmqadmin list bindings
+-------------+-------------+-------------+
| source | destination | routing_key |
+-------------+-------------+-------------+
| | test | test |
| | test.direct | test.direct |
| | test.fanout | test.fanout |
| | test.topic | test.topic |
| corp.direct | test | third |
| corp.direct | test.direct | fourth |
| corp.fanout | test | first |
| corp.fanout | test.fanout | second |
| corp.topic | test | *.hard.* |
| corp.topic | test.direct | *.*.food |
| corp.topic | test.fanout | *.*.food |
| corp.topic | test.fanout | xtest |
| corp.topic | test.topic | cheap.# |
+-------------+-------------+-------------+
[root@contoso ~]#
使用以上 routing_key 发送消息
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 0 |
| test.direct | 0 |
| test.fanout | 0 |
| test.topic | 0 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=a.hard.b exchange=corp.topic payload="this is eighth testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 1 |
| test.direct | 0 |
| test.fanout | 0 |
| test.topic | 0 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=a.hard.food exchange=corp.topic payload="this is ninth testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.direct | 1 |
| test.fanout | 1 |
| test.topic | 0 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=cheap.soft.food exchange=corp.topic payload="this is tenth testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 2 |
| test.direct | 2 |
| test.fanout | 2 |
| test.topic | 1 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=cheap.hard.drink exchange=corp.topic payload="this is eleventh testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 3 |
| test.direct | 2 |
| test.fanout | 2 |
| test.topic | 2 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=xtest exchange=corp.topic payload="this is twelfth testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 3 |
| test.direct | 2 |
| test.fanout | 3 |
| test.topic | 2 |
+-------------+----------+
[root@contoso ~]# rabbitmqadmin publish routing_key=cheap.hard.food exchange=corp.topic payload="this is thirteenth testing"
Message published
[root@contoso ~]# rabbitmqadmin list queues
+-------------+----------+
| name | messages |
+-------------+----------+
| test | 4 |
| test.direct | 3 |
| test.fanout | 4 |
| test.topic | 3 |
+-------------+----------+
[root@contoso ~]#
[root@contoso ~]# rabbitmqctl delete_vhost /
Deleting vhost "/"
[root@contoso ~]# rabbitmqctl add_vhost /
Creating vhost "/"
[root@contoso ~]# rabbitmqctl set_permissions -p / guest '.*' '.*' '.*'
Setting permissions for user "guest" in vhost "/"
[root@contoso ~]# systemctl restart rabbitmq-server && 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@contoso ~]#
[root@contoso ~]# rabbitmqctl list_vhosts ##列出所有虚拟主机
Listing vhosts
/
[root@contoso ~]# rabbitmqctl set_permissions -p / guest '.*' '.*' '.*' ##设置用户权限
[root@contoso ~]# rabbitmqctl list_permissions -p / ##列出虚拟主机上的所有权限
Listing permissions in vhost "/"
guest .* .* .*
[root@contoso ~]#
[root@contoso ~]# rabbitmqctl list_user_permissions guest ##列出用户权限
Listing permissions for user "guest"
/ .* .* .*
[root@contoso ~]#
[root@contoso ~]# rabbitmqctl list_exchanges -p /
Listing exchanges
amq.rabbitmq.trace topic
amq.rabbitmq.log topic
amq.match headers
amq.headers headers
amq.topic topic
amq.direct direct
amq.fanout fanout
direct
[root@contoso ~]# rabbitmqctl list_channels
Listing channels
<rabbit@contoso.2.11924.0> guest 1 0
[root@contoso ~]#
[root@contoso ~]# rabbitmqadmin declare exchange name=amq.direct type=direct ##定义一个 exchange
[root@contoso ~]# rabbitmqadmin declare exchange name=amq.fanout type=fanout ##定义一个 exchange
[root@contoso ~]# rabbitmqadmin declare exchange name=amq.headers type=headers ##定义一个 exchange
[root@contoso ~]# rabbitmqadmin declare exchange name=amq.match type=headers ##定义一个 exchange
[root@contoso ~]# rabbitmqadmin declare exchange name=amq.topic type=topic ##定义一个 exchange
[root@contoso ~]# rabbitmqadmin declare exchange name=corp.direct type=direct