尝试分别使用 third 和 fourth 的 routing_key 来发布消息
代码语言:javascript复制[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]#
从反馈结果来看,direct 的 exchange 就像点对点通信,fanout 的 exchange 就像是广播
topic 的特性
定义第四个queue ,使用 my.topic binding 起来
代码语言:javascript复制[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.# |
----------- ------------- -------------
[root@h102 rabbitmq]#