The Header Exchange is the most complex and flexible way to route messages in RabbitMQ. This exchange type sends messages to queues according by matching the queue binding arguments with message headers.
At the same time, if the queue listens to several consumers, messages will also be distributed among them (default scaling mechanism).
@broker.subscriber(queue_1,exch)asyncdefbase_handler1(logger:Logger):logger.info("base_handler1")@broker.subscriber(queue_1,exch)# another serviceasyncdefbase_handler2(logger:Logger):logger.info("base_handler2")@broker.subscriber(queue_2,exch)asyncdefbase_handler3(logger:Logger):logger.info("base_handler3")@broker.subscriber(queue_3,exch)asyncdefbase_handler4(logger:Logger):logger.info("base_handler4")
Note
handler1 and handler2 are subscribed to the same exchange using the same queue: within a single service, this does not make sense, since messages will come to these handlers in turn. Here we emulate the work of several consumers and load balancing between them.
Message 6 will be sent to handler3 and handler4 because the message headers completely match the queue keys.
Note
When sending messages to Header exchange, it makes no sense to specify the arguments queue or routing_key, because they will be ignored
Warning
For incredibly complex routes, you can use the option to bind an exchange to another exchange. In this case, all the same rules apply as for queues subscribed to exchange. The only difference is that the signed exchange can further distribute messages according to its own rules.
So, for example, you can combine Topic and Header exchange types.