zl程序教程

您现在的位置是:首页 >  其他

当前栏目

RabbitMQ 的CLI管理工具 rabbitmqadmin(15)

2023-03-20 14:52:05 时间

发布一条消息

[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        |
+------+----------+
[root@h102 rabbitmq]#

Tip: 这里有一个细节,我们并未指定任何 exchange ,依旧可以成功发送消息,是不是不需要 exchange 参与整个消息的派送过程呢,这和前面说的信息处理流程貌似有冲突呀,先卖个关子,后面再解释


消费一条信息

[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        |
+------+----------+
[root@h102 rabbitmq]#

定义一个 exchange

我们前面发布消息的过程中并未指定exchange,依旧成功发布了,事实上只是未明确指出,系统还是帮我们指了,RabbitMQ 的逻辑中是没法绕过 exchange 而直接给queue发送消息的

In RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.

The core idea in the messaging model in RabbitMQ is that the producer never sends any messages directly to a queue. Actually, quite often the producer doesn’t even know if a message will be delivered to any queue at all.

exchange 就是用来进行信息交换的逻辑

An exchange is a very simple thing. On one side it receives messages from producers and the other side it pushes them to queues.

exchange 的类型决定了其行为特性

The exchange must know exactly what to do with a message it receives. Should it be appended to a particular queue? Should it be appended to many queues? Or should it get discarded. The rules for that are defined by the exchange type

exchange 有以下几种类型

  • direct
  • topic
  • headers
  • fanout