这次我们使用 routing_key=first 来投递消息
代码语言:javascript复制[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 |
------------- ----------- --------------- ---------------- --------------- ------------------ ------------ -------------
[root@h102 rabbitmq]#
发现结果一样,应证了前面说的 routing_key 会被忽略的说法,但是不能不指定,否则会报错
代码语言:javascript复制[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]#
direct 的特性
定义第三个queue ,使用 my.direct binding 起来
代码语言:javascript复制[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]#