การสื่อสารระหว่างบริการด้วยคิวข้อความ
เรียนรู้วิธีที่ไมโครเซอร์วิสสื่อสารกันแบบอะซิงโครนัสโดยใช้คิวข้อความอย่าง RabbitMQ เพื่อแยกบริการออกจากกันและเพิ่มความทนทาน
การสื่อสารระหว่างบริการด้วยคิวข้อความ เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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.
เรียนรู้ JavaScript ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 22
- บทเรียน
- 92
คำถามที่พบบ่อย
บทเรียน “การสื่อสารระหว่างบริการด้วยคิวข้อความ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสื่อสารระหว่างบริการด้วยคิวข้อความ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสื่อสารระหว่างบริการด้วยคิวข้อความ”
เรียนรู้วิธีที่ไมโครเซอร์วิสสื่อสารกันแบบอะซิงโครนัสโดยใช้คิวข้อความอย่าง RabbitMQ เพื่อแยกบริการออกจากกันและเพิ่มความทนทาน คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่สถาปัตยกรรมไมโครเซอร์วิส
- การพัฒนาไมโครเซอร์วิสด้วย Node.js
- การใช้งานเกตเวย์ API
- การสื่อสารระหว่างบริการด้วยคิวข้อความ