التواصل بين الخدمات باستخدام قوائم انتظار الرسائل
تعلّموا كيف تتواصل الخدمات المصغّرة معًا بشكل غير متزامن باستخدام قوائم انتظار رسائل مثل RabbitMQ، مع فصل الخدمات وتحسين قدرتها على الصمود
التواصل بين الخدمات باستخدام قوائم انتظار الرسائل درس مجاني في Node.js Backend Development Bootcamp على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Node.js Backend Development Bootcamp، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Synchronous vs Asynchronous Communication
Microservices can talk in two ways:
- Synchronous (HTTP/gRPC): the caller waits for a reply
- Asynchronous (messaging): the caller sends a message and moves on
Async communication decouples services so a slow or down consumer does not block the producer.
What is a Message Queue?
A message queue is a buffer that holds messages until a consumer is ready to process them. Producers push messages in; consumers pull them out — usually in FIFO order.
Popular brokers include RabbitMQ, Kafka, and Redis Streams.
Key Benefits
Queues bring real advantages to a microservice system:
- Decoupling: services do not need to know about each other
- Resilience: messages wait if a consumer is down
- Load leveling: bursts are smoothed out
- Scalability: add more consumers to process faster
Core Concepts
Three roles define the pattern:
- Producer: publishes messages
- Queue: stores them
- Consumer: receives and processes them
In RabbitMQ an exchange sits between producer and queue, deciding routing.
Connecting from Node.js
The amqplib package connects Node.js to RabbitMQ. You open a connection, then a channel for sending and receiving.
const amqp = require('amqplib');
const conn = await amqp.connect('amqp://localhost');
const channel = await conn.createChannel();Declaring a Queue
Before sending, declare the queue so it exists. The durable option makes it survive a broker restart.
await channel.assertQueue('orders', { durable: true });Publishing a Message
Send a message with sendToQueue. Messages are buffers, so serialize objects to JSON first.
const order = { id: 7, total: 99 };
channel.sendToQueue('orders', Buffer.from(JSON.stringify(order)), {
persistent: true
});Consuming Messages
A consumer subscribes with consume. Each delivered message is parsed and processed.
channel.consume('orders', (msg) => {
const order = JSON.parse(msg.content.toString());
console.log('Processing order', order.id);
});Acknowledgements
To avoid losing work if a consumer crashes, RabbitMQ waits for an ack. Only after you call channel.ack(msg) is the message removed from the queue.
channel.consume('orders', (msg) => {
handle(JSON.parse(msg.content.toString()));
channel.ack(msg);
});Publish/Subscribe with Fanout
Sometimes many services need the same event (e.g. order placed). A fanout exchange broadcasts a message to every bound queue, enabling the publish/subscribe pattern.
await channel.assertExchange('events', 'fanout');
channel.publish('events', '', Buffer.from('order.created'));Handling Failures
Robust messaging plans for errors:
- Retries for transient failures
- Dead-letter queues for messages that keep failing
- Idempotency so reprocessing the same message is safe
Quick Check
Test your messaging knowledge.
Recap
You learned asynchronous inter-service communication:
- Message queues decouple producers from consumers
- Connect with
amqplib, declare durable queues - Publish with
sendToQueue, consume withconsume - Use
ackto guarantee delivery - Fanout exchanges enable pub/sub; dead-letter queues handle failures
Messaging makes microservice systems resilient and scalable.
الأسئلة الشائعة
هل درس «التواصل بين الخدمات باستخدام قوائم انتظار الرسائل» مجاني؟
نعم — نص درس «التواصل بين الخدمات باستخدام قوائم انتظار الرسائل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Node.js Backend Development Bootcamp، انتقل إلى CoddyKit PRO. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.
ماذا ستتعلم في «التواصل بين الخدمات باستخدام قوائم انتظار الرسائل»؟
تعلّموا كيف تتواصل الخدمات المصغّرة معًا بشكل غير متزامن باستخدام قوائم انتظار رسائل مثل RabbitMQ، مع فصل الخدمات وتحسين قدرتها على الصمود تتمرن على Node.js Backend Development Bootcamp مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Node.js Backend Development Bootcamp؟
لا تُشترط خبرة سابقة. Node.js Backend Development Bootcamp على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التواصل بين الخدمات باستخدام قوائم انتظار الرسائل»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Node.js Backend Development Bootcamp هذا؟
نعم. كل درس في Node.js Backend Development Bootcamp يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مقدمة إلى معمارية الخدمات المصغّرة
- تطوير الخدمات المصغّرة باستخدام Node.js
- تطبيق بوابة API
- التواصل بين الخدمات باستخدام قوائم انتظار الرسائل