如果不使用 * 和 # ,那么 topic 的特性就和 direct 一样了
代码语言:javascript复制[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 发送消息
代码语言:javascript复制[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]#