RabbitMQ combat (3) Exchange in RabbitMQ

This article mainly talks about the exchanges in RabbitMQ.

Instead of sending the message directly to the queue, we first send it to the exchange, and the exchange then delivers our message to the corresponding queue according to the routing key.

type of switch

Each exchange defines a different routing algorithm to deliver messages to the corresponding queue.

The switches in RabbitMQ are mainly divided into four types: direct, fanout, topic and headers. The headers are used less, so I won't talk about them.

direct

Routing key full text match

When you send a message to a direct exchange, it will look for a matching queue bound to this exchange based on the routing key full text match , and then deliver the message.

As shown in the figure above, the producer uses the routing key "fruit.apple" to send a message to the direct exchange, and the exchange will match the routing key in full text according to the binding information, so only the apple queue matches.

fanout

Ignore routing keys

When you send a message to a fanout exchange, it will deliver the message to all queues bound to this exchange, ignoring routing keys .

As shown above, the producer uses the routing key "fruit.apple" to send a message to the fanout exchange, but the exchange ignores the routing key, so the apple queue, banana queue, and orange queue all match.

topic

Routing key wildcard matching

When you send a message to a topic exchange, it will look for a matching queue bound to this exchange according to the routing key wildcard match , and then deliver the message.

As shown in the figure above, the producer uses the routing key "fruit.apple" to send a message to the topic exchange, and the exchange will match the routing key according to the binding information and wildcards, so the apple queue, banana queue, and orange queue all match.

  • All rules can be configured using the operator "#".
  • The "*" operator treats "." as a delimiter.
  • The "#" operator has no concept of blocking, it treats any "." character as the matching part of the keyword.

run the project

Add the running parameters first, because several switch configurations are in one project, which will lead to bean conflicts, so use @Profile("direct"), @Profile("fanout"), and @Profile("topic")different configurations to enable different environments.

  • Test the direct exchange:--spring.profiles.active=direct
  • Test the fanout exchange:--spring.profiles.active=fanout
  • Test the topic exchange:--spring.profiles.active=topic

Run the project, then open the browser and enterhttp://localhost:9999/sendMessage

Test the direct exchange

Test the fanout exchange

Test the topic exchange

Source address

References

RabbitMQ for Windows: Exchange Types

Epilogue

Due to my limited knowledge and ability, if there is something unclear in the text, I hope you can point it out in the comment area to help me write the blog post better.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325457515&siteId=291194637