Topic exchange allows filtering by criteria with # (substitute for 0 or more words) and * (substitute for exactly one word). Routing keys can use dot (.) as the separator of words. For example:
#— all messages will be routed to the queue bound to this routing key (#essentially means all)app.*— all messages with routing key that starts withapp.then followed by a word*.debug— all messages with routing key that starts with any word followed by.debug
In the example below, Q1 queues received all messages and other queues received the messages based on the criteria set on the binding’s routing key.
➜ docker exec -it rabbitmq rabbitmqadmin declare exchange --name=logs_topic --type=topic --durable=true
➜ docker exec -it rabbitmq rabbitmqadmin declare queue --name=Q1 --durable=true
➜ docker exec -it rabbitmq rabbitmqadmin declare queue --name=Q2 --durable=true
➜ docker exec -it rabbitmq rabbitmqadmin declare queue --name=Q3 --durable=true
➜ docker exec -it rabbitmq rabbitmqadmin declare binding --source=logs_topic --destination-type=queue --destination=Q1 --routing-key=#
➜ docker exec -it rabbitmq rabbitmqadmin declare binding --source=logs_topic --destination-type=queue --destination=Q2 --routing-key="app.*"
➜ docker exec -it rabbitmq rabbitmqadmin declare binding --source=logs_topic --destination-type=queue --destination=Q3 --routing-key="*.debug"
➜ docker exec -it rabbitmq rabbitmqadmin publish message --exchange=logs_topic --routing-key=app.debug --payload="app.debug"
Message published and routed successfully
➜ docker exec -it rabbitmq rabbitmqadmin publish message --exchange=logs_topic --routing-key=app.info --payload="app.info"
Message published and routed successfully
➜ docker exec -it rabbitmq rabbitmqadmin publish message --exchange=logs_topic --routing-key=rabbitmq.debug --payload="rabbitmq.debug"
Message published and routed successfully
➜ docker exec -it rabbitmq rabbitmqadmin publish message --exchange=logs_topic --routing-key=rabbitmq.warn --payload="rabbitmq.warn"
Message published and routed successfully
➜ docker exec -it rabbitmq rabbitmqadmin get messages --queue=Q1 --count=10
┌───────────────┬─────────────┬────────────┬────────────────┬───────────────┬────────────┬────────────────┬──────────────────┐
│ payload_bytes │ redelivered │ exchange │ routing_key │ message_count │ properties │ payload │ payload_encoding │
├───────────────┼─────────────┼────────────┼────────────────┼───────────────┼────────────┼────────────────┼──────────────────┤
│ 9 │ false │ logs_topic │ app.debug │ 3 │ │ app.debug │ string │
├───────────────┼─────────────┼────────────┼────────────────┼───────────────┼────────────┼────────────────┼──────────────────┤
│ 8 │ false │ logs_topic │ app.info │ 2 │ │ app.info │ string │
├───────────────┼─────────────┼────────────┼────────────────┼───────────────┼────────────┼────────────────┼──────────────────┤
│ 14 │ false │ logs_topic │ rabbitmq.debug │ 1 │ │ rabbitmq.debug │ string │
├───────────────┼─────────────┼────────────┼────────────────┼───────────────┼────────────┼────────────────┼──────────────────┤
│ 13 │ false │ logs_topic │ rabbitmq.warn │ 0 │ │ rabbitmq.warn │ string │
└───────────────┴─────────────┴────────────┴────────────────┴───────────────┴────────────┴────────────────┴──────────────────┘
➜ docker exec -it rabbitmq rabbitmqadmin get messages --queue=Q2 --count=10
┌───────────────┬─────────────┬────────────┬─────────────┬───────────────┬────────────┬───────────┬──────────────────┐
│ payload_bytes │ redelivered │ exchange │ routing_key │ message_count │ properties │ payload │ payload_encoding │
├───────────────┼─────────────┼────────────┼─────────────┼───────────────┼────────────┼───────────┼──────────────────┤
│ 9 │ false │ logs_topic │ app.debug │ 1 │ │ app.debug │ string │
├───────────────┼─────────────┼────────────┼─────────────┼───────────────┼────────────┼───────────┼──────────────────┤
│ 8 │ false │ logs_topic │ app.info │ 0 │ │ app.info │ string │
└───────────────┴─────────────┴────────────┴─────────────┴───────────────┴────────────┴───────────┴──────────────────┘
➜ docker exec -it rabbitmq rabbitmqadmin get messages --queue=Q3 --count=10
┌───────────────┬─────────────┬────────────┬────────────────┬───────────────┬────────────┬────────────────┬──────────────────┐
│ payload_bytes │ redelivered │ exchange │ routing_key │ message_count │ properties │ payload │ payload_encoding │
├───────────────┼─────────────┼────────────┼────────────────┼───────────────┼────────────┼────────────────┼──────────────────┤
│ 9 │ false │ logs_topic │ app.debug │ 1 │ │ app.debug │ string │
├───────────────┼─────────────┼────────────┼────────────────┼───────────────┼────────────┼────────────────┼──────────────────┤
│ 14 │ false │ logs_topic │ rabbitmq.debug │ 0 │ │ rabbitmq.debug │ string │
└───────────────┴─────────────┴────────────┴────────────────┴───────────────┴────────────┴────────────────┴──────────────────┘