ปลั๊กอิน Federation สำหรับการทำคลัสเตอร์
สำรวจปลั๊กอิน Federation เพื่อเชื่อมโบรกเกอร์ RabbitMQ หลายตัวเป็นคลัสเตอร์เชิงตรรกะโดยไม่ต้องตั้งค่าเครือข่ายที่ซับซ้อน สร้างระบบส่งข้อความแบบกระจายที่ครอบคลุมศูนย์ข้อมูลหลายแห่ง
ปลั๊กอิน Federation สำหรับการทำคลัสเตอร์ เป็นบทเรียน RabbitMQ Messaging & Async Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน RabbitMQ Messaging & Async Systems และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส RabbitMQ Messaging & Async Systems มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is RabbitMQ Federation?
Imagine you have RabbitMQ brokers in different data centers or cloud regions. How do they share messages without complex network setups?
The Federation plugin allows you to loosely connect these brokers. It enables messages to flow between them, creating a distributed messaging system across geographical boundaries.
Federation vs. Traditional Clustering
It's important to distinguish Federation from traditional RabbitMQ clustering:
- Clustering: Tightly couples nodes, sharing state and data. Best for high availability within a single, low-latency network (e.g., a data center).
- Federation: Loosely connects brokers by replicating messages. Ideal for spanning wide area networks (WANs) or connecting brokers managed by different teams. It doesn't share state like a cluster.
Key Components: Upstreams & Policies
Federation relies on two main concepts:
- Upstreams: These define the source broker from which messages will be pulled. An upstream specifies the connection details (like URI) of the remote RabbitMQ instance.
- Policies: These rules determine which exchanges or queues on your downstream broker should connect to which upstream broker. They use regular expressions to match names.
Enabling the Federation Plugin
Before you can use federation, you need to enable the plugin on all participating RabbitMQ brokers. This is done using the RabbitMQ command-line tool.
Run this command on each broker:
rabbitmq-plugins enable rabbitmq_federation rabbitmq_federation_managementConfiguring an Upstream Link
An 'upstream' tells your local broker where to pull messages from. You define it using the rabbitmqctl set_parameter command.
This example sets up an upstream named 'my-upstream' pointing to a remote broker:
rabbitmqctl set_parameter federation-upstream my-upstream \
'{"uri":"amqp://guest:guest@remote-host:5672","expires":3600000}'Creating a Federation Policy for Exchanges
Once an upstream is defined, you apply it to exchanges using a policy. This policy tells your local broker to federate messages for matching exchanges from the specified upstream.
This policy federates all exchanges starting with 'fed.' from 'my-upstream':
rabbitmqctl set_policy --apply-to exchanges \
fed-exchanges ".^fed\\..*" \
'{"federation-upstream":"my-upstream"}'How Federated Exchanges Work
With a federated exchange:
- A producer publishes messages to an exchange on the upstream broker.
- The federation plugin on the downstream broker pulls these messages from the upstream exchange.
- The messages then arrive at the corresponding federated exchange on the downstream broker, where local consumers can receive them.
The consumer only interacts with its local (downstream) broker.
Client Example: Consuming from Federated Exchange
Here's a Python consumer that connects to a downstream broker and receives messages from a federated exchange. It doesn't need to know the messages originated from an upstream broker.
To run this, ensure you have pika installed (pip install pika).
import pika
import sys
import os
def main():
# Connect to the local (downstream) RabbitMQ broker
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Declare the exchange that is federated from an upstream broker
# (e.g., using the policy created in previous steps for 'fed_logs')
channel.exchange_declare(exchange='fed_logs', exchange_type='fanout', durable=True)
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='fed_logs', queue=queue_name)
print(' [*] Waiting for federated messages. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(f" [x] Received: {body.decode()}")
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
channel.start_consuming()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print(' Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)Federating Queues
You can also federate queues. When a queue is federated, messages published to that queue on the upstream broker will be pulled and added to the corresponding queue on the downstream broker.
The policy setup is very similar, just specify --apply-to queues:
rabbitmqctl set_policy --apply-to queues \
fed-queues ".^fed\\..*" \
'{"federation-upstream":"my-upstream"}'Quick Check on Federation
Which of the following best describes the primary use case for RabbitMQ Federation?
Recap: Federation for Distributed Messaging
In this lesson, you learned about the RabbitMQ Federation plugin:
- It allows you to connect RabbitMQ brokers across different locations.
- It uses upstreams to define source brokers and policies to apply federation rules.
- Federation differs from clustering by offering loose coupling, suitable for WANs.
- You can federate both exchanges and queues, enabling flexible message distribution in a distributed system.
This powerful plugin helps you build robust, geographically distributed messaging architectures.
คำถามที่พบบ่อย
บทเรียน “ปลั๊กอิน Federation สำหรับการทำคลัสเตอร์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ปลั๊กอิน Federation สำหรับการทำคลัสเตอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส RabbitMQ Messaging & Async Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส RabbitMQ Messaging & Async Systems มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ปลั๊กอิน Federation สำหรับการทำคลัสเตอร์”
สำรวจปลั๊กอิน Federation เพื่อเชื่อมโบรกเกอร์ RabbitMQ หลายตัวเป็นคลัสเตอร์เชิงตรรกะโดยไม่ต้องตั้งค่าเครือข่ายที่ซับซ้อน สร้างระบบส่งข้อความแบบกระจายที่ครอบคลุมศูนย์ข้อมูลหลายแห่ง คุณปฏิบัติ RabbitMQ Messaging & Async Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน RabbitMQ Messaging & Async Systems หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน RabbitMQ Messaging & Async Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ปลั๊กอิน Federation สำหรับการทำคลัสเตอร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน RabbitMQ Messaging & Async Systems นี้ได้ไหม
ได้ บทเรียน RabbitMQ Messaging & Async Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ปลั๊กอินสำหรับข้อความล่าช้า
- ปลั๊กอิน Shovel สำหรับการรวมระบบ
- ปลั๊กอิน Federation สำหรับการทำคลัสเตอร์
- ปลั๊กอินขจัดข้อความซ้ำและ Consistent Hash Exchange