0
0
RabbitMQdevops~30 mins

Topic exchange (pattern matching) in RabbitMQ - Mini Project: Build & Apply

Choose your learning style9 modes available
Topic Exchange with Pattern Matching in RabbitMQ
📖 Scenario: You are setting up a messaging system for a news service. Different news categories like sports, weather, and finance send messages. You want to route messages to queues based on topics using pattern matching.
🎯 Goal: Build a RabbitMQ topic exchange setup where messages are routed to queues based on matching routing keys with patterns.
📋 What You'll Learn
Create a topic exchange named news_topic_exchange
Create two queues named sports_queue and weather_queue
Bind sports_queue with the routing key pattern sports.*
Bind weather_queue with the routing key pattern weather.#
Publish messages with routing keys sports.football and weather.rain
Consume and print messages from both queues
💡 Why This Matters
🌍 Real World
Topic exchanges are used in messaging systems to route messages based on flexible patterns, like categorizing news or logs.
💼 Career
Understanding topic exchanges is important for configuring message brokers in microservices, event-driven architectures, and distributed systems.
Progress0 / 4 steps
1
Create the topic exchange and queues
Create a topic exchange called news_topic_exchange. Then create two queues named sports_queue and weather_queue.
RabbitMQ
Need a hint?

Use exchange_declare with exchange_type='topic' and queue_declare for queues.

2
Bind queues with routing key patterns
Bind the queue sports_queue to news_topic_exchange with the routing key pattern sports.*. Bind the queue weather_queue with the routing key pattern weather.#.
RabbitMQ
Need a hint?

Use queue_bind with the correct routing_key patterns.

3
Publish messages with routing keys
Publish a message with the routing key sports.football and body 'Football news'. Then publish a message with the routing key weather.rain and body 'Rain forecast' to the news_topic_exchange.
RabbitMQ
Need a hint?

Use basic_publish with the correct exchange, routing_key, and body.

4
Consume and print messages from queues
Consume one message from sports_queue and print its body. Then consume one message from weather_queue and print its body.
RabbitMQ
Need a hint?

Use basic_get with auto_ack=True to get messages and print their decoded bodies.