Detailed explanation of using rabbitmq routing in spring boot
In the last tutorial, we created a fan out switch. We can deliver messages to multiple consumers in the form of broadcast.
What are you doing? Routing
In this tutorial, a new feature is added so that we can subscribe to only part of the message. For example, we will connect only the colors we are interested in ("orange", "black", "green") and print all the messages on the console.
binding
Switches and queues are a binding relationship. The simple understanding is that the queue is interested in the information from this switch.
Binding can add an additional parameter routingkey. Spring AMQP uses the easy to understand API (builder mode) to make the relationship between them very clear. Put the switch and queue into binding builder, and you can easily bind the queue to the switch with routing key.
This means that the binding key depends on the switch type, and the fan out switch cannot. There are no options that can be bound.
Direct connected exchanger
In the previous tutorial, our message system was broadcast to all consumers. We want to expand the function and add a filter based on color type. For example, we want a program to receive detailed error messages and write them to the hard disk as a log, and do not receive info or warning logs.
Orange, black and green routing keys
As shown in the figure above, two queues are bound on the direct connection switch X. The first queue uses orange as the routing key, and the second has two routing keys, black and green.
In this setting, when a message with the routing key orange is pushed to the switch, the message will be routed to queue Q1. When the routing key used by the message is black or green, it will be routed to Q2. Other messages that do not use routing keys will be discarded.
Parallel binding
Parallel binding
This can achieve functions similar to the fanout switch.
Almost. Look at the code
Config. java
Receiver. java
Send. java
SendTest. java
Test results: if keys [0], there is only receiver0. If keys [1], there are receive0 and receive1. If keys [2], there is only receive1
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.