ตัวแลกเปลี่ยน Direct สำหรับการกำหนดเส้นทาง
เรียนรู้การใช้ตัวแลกเปลี่ยน Direct เพื่อกำหนดเส้นทางข้อความอย่างแม่นยำตามคีย์เส้นทาง ส่งข้อความไปยังคิวเฉพาะเพื่อประมวลผลแบบเจาะจง
ตัวแลกเปลี่ยน Direct สำหรับการกำหนดเส้นทาง เป็นบทเรียน RabbitMQ Messaging & Async Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน RabbitMQ Messaging & Async Systems และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส RabbitMQ Messaging & Async Systems มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Introduction to Direct Exchange
Welcome! In this lesson, we'll explore the Direct Exchange in RabbitMQ. It's a powerful tool for sending messages to specific queues based on a routing key.
Think of it like a postal service that delivers letters only to the exact address specified on the envelope.
What is a Routing Key?
A routing key is a string attribute that producers attach to messages. It's essentially an "address" for the message.
- Producers specify a routing key when publishing.
- Queues bind to an exchange with one or more routing keys.
- Messages are delivered to queues whose binding key exactly matches the message's routing key.
How Direct Exchange Works
When a message arrives at a Direct Exchange, it looks at the message's routing key. The exchange then delivers the message to all queues that are bound to it with an identical routing key.
If no queue is bound with that specific routing key, the message is simply discarded by the exchange.
Producer's Role: Sending with Keys
As a producer, you decide the routing key for each message you send. This key determines which consumer (via its bound queue) will receive the message.
This allows for precise control over message delivery, ensuring only relevant consumers get specific types of messages.
Consumer's Role: Binding with Keys
Consumers define their interest in messages by binding their queues to a Direct Exchange using specific routing keys.
A single queue can be bound with multiple routing keys, allowing it to receive messages matching any of those keys.
Code: Producer Setup & Declare
Let's set up a basic producer that uses a Direct Exchange. We'll declare an exchange named "direct_logs" with type "direct".
This code establishes a connection and channel, then declares the exchange.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class DirectProducerSetup {
private static final String EXCHANGE_NAME = "direct_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost"); // Assuming RabbitMQ is local
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
System.out.println("Exchange '" + EXCHANGE_NAME + "' declared.");
// In a real app, you'd send messages here
// For this setup example, we just declare.
}
}
}Code: Producer Sending Message
Now, let's send a message to our "direct_logs" exchange. We'll specify a routing key, for example, "error".
Run this code after ensuring your RabbitMQ server is running. It will publish a message with the specified key.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.nio.charset.StandardCharsets;
public class DirectProducerSend {
private static final String EXCHANGE_NAME = "direct_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost"); // Assuming RabbitMQ is local
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.exchangeDeclare(EXCHANGE_NAME, "direct"); // Ensure exchange exists
String routingKey = "error";
String message = "A critical error occurred!";
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] Sent '" + routingKey + ":'" + message + "'");
}
}
}Code: Consumer Setup & Binding
On the consumer side, we'll create a queue and bind it to the "direct_logs" exchange using the routing key "error".
This consumer will only receive messages published with the "error" routing key. Run this consumer first, then the producer.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
public class DirectConsumerError {
private static final String EXCHANGE_NAME = "direct_logs";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost"); // Assuming RabbitMQ is local
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String queueName = channel.queueDeclare().getQueue(); // Create a non-durable, exclusive, auto-delete queue
String bindingKey = "error";
channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
System.out.println(" [*] Waiting for messages with routing key '" + bindingKey + "'. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + ":'" + message + "'");
};
channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {});
}
}Multiple Bindings & Use Cases
A single queue can bind to the same Direct Exchange with multiple routing keys. For instance, a "critical alerts" queue might bind to both "error" and "warning" keys to receive both types of messages.
Direct exchanges are perfect for scenarios like:
- Distributing logs based on severity (e.g., info, warn, error).
- Routing internal system events to specific processing modules.
- Targeting updates to particular user segments.
Quick Check: Direct Routing
Consider a Direct Exchange named "my_exchange".
- Queue A is bound with routing key
"report". - Queue B is bound with routing key
"alert". - Queue C is bound with routing keys
"report"and"update".
A producer sends a message to "my_exchange" with the routing key "report".
Recap: Direct Exchange
Great job! You've learned about the Direct Exchange in RabbitMQ.
- It routes messages based on an exact match of the routing key.
- Producers attach a routing key to each message.
- Consumers bind their queues to the exchange with specific routing keys to receive relevant messages.
- This pattern allows for precise, targeted message delivery.
Next, we'll explore the Fanout Exchange, which broadcasts messages to all bound queues, regardless of routing key!
คำถามที่พบบ่อย
บทเรียน “ตัวแลกเปลี่ยน Direct สำหรับการกำหนดเส้นทาง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวแลกเปลี่ยน Direct สำหรับการกำหนดเส้นทาง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส RabbitMQ Messaging & Async Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส RabbitMQ Messaging & Async Systems มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวแลกเปลี่ยน Direct สำหรับการกำหนดเส้นทาง”
เรียนรู้การใช้ตัวแลกเปลี่ยน Direct เพื่อกำหนดเส้นทางข้อความอย่างแม่นยำตามคีย์เส้นทาง ส่งข้อความไปยังคิวเฉพาะเพื่อประมวลผลแบบเจาะจง คุณปฏิบัติ RabbitMQ Messaging & Async Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน RabbitMQ Messaging & Async Systems หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน RabbitMQ Messaging & Async Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวแลกเปลี่ยน Direct สำหรับการกำหนดเส้นทาง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน RabbitMQ Messaging & Async Systems นี้ได้ไหม
ได้ บทเรียน RabbitMQ Messaging & Async Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ตัวแลกเปลี่ยน Fanout สำหรับ Pub/Sub
- ตัวแลกเปลี่ยน Direct สำหรับการกำหนดเส้นทาง
- ตัวแลกเปลี่ยน Topic สำหรับการกำหนดเส้นทางที่ยืดหยุ่น
- Exchange เริ่มต้นและการผูกโดยนัย