Introduction: In RabbitMQ, all messages submitted by producers are accepted by Exchange, which then forwards them to Queue for storage according to a specific policy RabbitMQ provides four types of exchanges: fanout, direct, topic, and header The header mode is less used in practice, and this article only compares the first three modes.
1. Direct Exchange
Any messages sent to Direct Exchange are forwarded to the Queue specified in the RouteKey.
1. In general, you can use the Exchange that comes with rabbitMQ:" (the name of the Exchange is an empty string, hereinafter referred to as the default Exchange).
2. In this mode, there is no need to perform any binding operations on Exchange
3. A "RouteKey" is required for messaging, which can be simply understood as the name of the queue to be sent to.
4. If the queue name specified in the RouteKey does not exist in the vhost, the message is dropped.
2. Fanout Exchange
Any messages sent to the Fanout Exchange are forwarded to all Queues bound to that Exchange.
1. It can be understood as a pattern of the routing table
2. This mode does not require a RouteKey
3. This mode requires binding Exchange to Queue in advance, one Exchange can bind multiple Queues, and one Queue can be bound to multiple Exchanges.
4. If the Exchange that received the message is not bound to any Queue, the message is dropped.
3. Topic Exchange
Any messages sent to the Topic Exchange are forwarded to all Queues that care about the topics specified in the RouteKey
1. This pattern is more complex, simply put, each queue has its own topic of concern, all messages have a "title" (RouteKey), and Exchange will forward the message to all queues that can fuguely match the RouteKey of the topic of concern.
2. This mode requires a RouteKey, perhaps binding Exchange and Queue in advance.
3. When binding, provide a topic that the queue cares about, such as "#.log.#" to indicate that the queue cares about all messages related to logs (a message with a RouteKey of "MQ.log.error" will be forwarded to the queue).
4. "#" means 0 or several keywords, and "" means one keyword. For example, "log." It can match "log.warn", but cannot match "log.warn.timeout"; But "log.#" matches the above.
5. Similarly, if Exchange does not find a Queue that matches the RouteKey, it will drop this message.
|