The Topic Exchange is a powerful RabbitMQ routing tool. This type of exchange sends messages to the queue in accordance with the pattern specified when they are connected to exchange and the routing_key of the message itself.
At the same time, if the queue listens to several consumers, messages will also be distributed among them (default scaling mechanism).
At the same time, in the routing_key of our queues, we specify the pattern of routing keys that will be processed by this queue.
Then we sign up several consumers using the advertised queues to the exchange we created:
13141516171819202122232425
@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")
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.