0Pricing
RabbitMQ Messaging & Async Systems · บทเรียน

การเชื่อมโยงตัวแลกเปลี่ยนเข้าด้วยกัน

เรียนรู้การเชื่อมโยงตัวแลกเปลี่ยนเข้ากับตัวแลกเปลี่ยนอื่น เพื่อสร้างสายการกำหนดเส้นทางและรูปแบบการกระจายข้อความที่ซับซ้อน สร้างการไหลของข้อความที่ละเอียดภายในระบบ

การเชื่อมโยงตัวแลกเปลี่ยนเข้าด้วยกัน เป็นบทเรียน RabbitMQ Messaging & Async Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน RabbitMQ Messaging & Async Systems และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส RabbitMQ Messaging & Async Systems มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Chain Exchanges?

In RabbitMQ, you've learned how producers send messages to exchanges, which then route them to queues. But what if you need more complex routing?

Sometimes, a message needs to go through multiple "decision points" before reaching its final destination. This is where Exchange-to-Exchange (E2E) bindings come in handy!

Chaining Exchanges Together

Think of E2E bindings as connecting pipelines. Instead of an exchange sending messages directly to a queue, it sends them to another exchange.

  • The first exchange receives a message.
  • It routes the message to a second exchange based on its type and the binding.
  • The second exchange then routes the message further, either to queues or yet another exchange.

This creates powerful routing flows!

Anatomy of an E2E Binding

An E2E binding connects a source exchange to a destination exchange. Messages arriving at the source exchange will be routed to the destination exchange.

Just like exchange-to-queue bindings, E2E bindings also use a routing key. This key determines how messages are routed from the source exchange to the destination exchange, based on the source exchange's type.

Example: Direct to Fanout Chain

Let's consider a practical scenario: You want to route a message to a specific category, and then broadcast it to multiple services interested in that category.

  • A producer sends a message to a Direct Exchange with a routing key like "alerts.critical".
  • This Direct Exchange is bound to a Fanout Exchange with the same routing key.
  • The Fanout Exchange is then bound to several queues, ensuring all services receive the "alerts.critical" message.

Declaring Exchanges for E2E

First, we need to declare our two exchanges: a 'source' Direct exchange and a 'destination' Fanout exchange. The producer will send messages to the 'source' exchange.

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class ExchangeDeclarer {
    private static final String SOURCE_EXCHANGE = "my_direct_source";
    private static final String DEST_EXCHANGE = "my_fanout_destination";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            channel.exchangeDeclare(SOURCE_EXCHANGE, "direct");
            System.out.println("Declared source direct exchange: " + SOURCE_EXCHANGE);

            channel.exchangeDeclare(DEST_EXCHANGE, "fanout");
            System.out.println("Declared destination fanout exchange: " + DEST_EXCHANGE);

        }
    }
}

Binding Exchanges Together

Now, let's create the actual E2E binding. We'll bind our 'source' direct exchange to our 'destination' fanout exchange. We'll use a routing key "my.route" for this specific binding.

Messages sent to my_direct_source with routing key my.route will be forwarded to my_fanout_destination.

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class ExchangeBinder {
    private static final String SOURCE_EXCHANGE = "my_direct_source";
    private static final String DEST_EXCHANGE = "my_fanout_destination";
    private static final String BINDING_KEY = "my.route";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // Ensure exchanges are declared (or declare them here if not done)
            channel.exchangeDeclare(SOURCE_EXCHANGE, "direct");
            channel.exchangeDeclare(DEST_EXCHANGE, "fanout");

            // Bind source to destination with a routing key
            channel.exchangeBind(DEST_EXCHANGE, SOURCE_EXCHANGE, BINDING_KEY); // Note: bind(destination, source, key)
            System.out.println("Bound " + SOURCE_EXCHANGE + " to " + DEST_EXCHANGE + " with key: " + BINDING_KEY);

        }
    }
}

Sending Messages to the Source

Our producer will send a message to the my_direct_source exchange using the routing key my.route. This message will then be routed to our my_fanout_destination exchange due to the E2E binding.

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.nio.charset.StandardCharsets;

public class E2EProducer {
    private static final String SOURCE_EXCHANGE = "my_direct_source";
    private static final String BINDING_KEY = "my.route";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // Ensure source exchange is declared
            channel.exchangeDeclare(SOURCE_EXCHANGE, "direct");

            String message = "Hello via E2E binding!";
            channel.basicPublish(SOURCE_EXCHANGE, BINDING_KEY, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "' with routing key '" + BINDING_KEY + "' to " + SOURCE_EXCHANGE);

        }
    }
}

Consuming from the Destination

Finally, a consumer will receive the message. Since my_fanout_destination is a fanout exchange, we'll bind a temporary queue to it. Any message arriving at the fanout exchange (from our E2E binding) will be delivered to this queue.

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class E2EConsumer {
    private static final String DEST_EXCHANGE = "my_fanout_destination";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(DEST_EXCHANGE, "fanout");

        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, DEST_EXCHANGE, ""); // Fanout ignores routing key

        System.out.println(" [*] Waiting for messages in queue '" + queueName + "'. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {});
    }
}

Routing Keys and E2E Flow

When a message is routed from a source exchange to a destination exchange via an E2E binding, its original routing key is preserved.

The source exchange uses the binding key of the E2E binding to decide if it should forward the message to the destination exchange. Once forwarded, the destination exchange then uses the message's original routing key (not the E2E binding key) to route it to its own bound queues.

  • Direct Source: Matches message's routing key exactly to E2E binding key.
  • Topic Source: Matches message's routing key against E2E binding key (with wildcards).
  • Fanout Source: Ignores E2E binding key, forwards all messages.

Test Your E2E Knowledge

A Direct Exchange 'Source' is bound to a Fanout Exchange 'Dest' with the routing key "alerts.high".

A producer sends a message to 'Source' with the routing key "alerts.high".

'Dest' is bound to two queues: 'Q1' and 'Q2'.

Which queues will receive the message?

E2E Bindings: Summary

You've explored Exchange-to-Exchange (E2E) bindings, a powerful feature for creating sophisticated message routing paths in RabbitMQ!

  • E2E bindings allow an exchange to forward messages to another exchange, creating routing chains.
  • They use a binding key, just like exchange-to-queue bindings, to define the routing logic between exchanges.
  • The message's original routing key is preserved and used by the destination exchange for further routing to queues.
  • E2E bindings enable complex patterns like filtering then fanning out, or cascading messages through multiple processing stages.

คำถามที่พบบ่อย

บทเรียน “การเชื่อมโยงตัวแลกเปลี่ยนเข้าด้วยกัน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเชื่อมโยงตัวแลกเปลี่ยนเข้าด้วยกัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส RabbitMQ Messaging & Async Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส RabbitMQ Messaging & Async Systems มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเชื่อมโยงตัวแลกเปลี่ยนเข้าด้วยกัน”

เรียนรู้การเชื่อมโยงตัวแลกเปลี่ยนเข้ากับตัวแลกเปลี่ยนอื่น เพื่อสร้างสายการกำหนดเส้นทางและรูปแบบการกระจายข้อความที่ซับซ้อน สร้างการไหลของข้อความที่ละเอียดภายในระบบ คุณปฏิบัติ RabbitMQ Messaging & Async Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน RabbitMQ Messaging & Async Systems หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน RabbitMQ Messaging & Async Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การเชื่อมโยงตัวแลกเปลี่ยนเข้าด้วยกัน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน RabbitMQ Messaging & Async Systems นี้ได้ไหม

ได้ บทเรียน RabbitMQ Messaging & Async Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เจาะลึกตัวแลกเปลี่ยน Headers
  2. การเชื่อมโยงตัวแลกเปลี่ยนเข้าด้วยกัน
  3. ตัวแลกเปลี่ยนจดหมายตีกลับ (DLX)
  4. Exchange สำรองสำหรับข้อความที่กำหนดเส้นทางไม่ได้
← กลับไปที่ RabbitMQ Messaging & Async Systems