บัสเหตุการณ์และตัวกลางข้อความ
ใช้ตัวกลางข้อความและบัสเหตุการณ์เพื่ออำนวยความสะดวกในการสื่อสารแบบไม่ประสานเวลาที่เชื่อถือได้สำหรับ Saga แบบประสานงานด้วยเหตุการณ์
บัสเหตุการณ์และตัวกลางข้อความ เป็นบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Microservices Communication Patterns (Saga, Circuit Breaker) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Reliable Choreography Sagas
In a choreography saga, services communicate by exchanging events. For this to work reliably in a distributed system, we need robust communication mechanisms.
Imagine an order being created, then needing payment, and finally shipping. Each step is an event triggering the next service.
- Order Service publishes
OrderCreated - Payment Service subscribes to
OrderCreated, publishesPaymentProcessed - Shipping Service subscribes to
PaymentProcessed, publishesShipmentScheduled
What's an Event Bus?
An Event Bus is a pattern that enables different parts of an application (or different services) to communicate by sending and receiving events without knowing about each other directly.
Think of it as a central hub where events are broadcast. Any interested party can 'listen' for specific events.
It decouples services, meaning they don't need to know who will process their events, only that an event occurred.
Event Bus in Action
Consider an internal event bus within a single application:
- A user clicks a 'Save' button.
- The UI component publishes a
UserSavedevent to the event bus. - A logging module, a notification module, and a data synchronization module might all subscribe to
UserSaved.
They all react to the event without the UI knowing about them. This is simple and effective for in-process communication.
Introducing Message Brokers
While an event bus is great for in-process communication, distributed microservices need more. This is where Message Brokers come in.
A message broker is a dedicated service (like RabbitMQ, Kafka, or AWS SQS) that acts as an intermediary for messages between different applications or services.
It provides advanced features essential for reliable communication across networks.
Broker Benefits for Sagas
Message brokers offer crucial benefits for choreography sagas:
- Durability: Messages persist even if services are down, preventing data loss.
- Guaranteed Delivery: Ensures messages reach their intended consumers.
- Decoupling: Senders and receivers don't know each other, promoting service independence.
- Load Balancing: Distributes messages efficiently among multiple instances of a consuming service.
- Asynchronous: Services don't wait for responses, improving performance and responsiveness.
Bus vs. Broker: The Difference
While both facilitate event-driven communication, an Event Bus is typically a lightweight, in-memory mechanism within a single application or process.
A Message Broker is a robust, external system designed for inter-process and inter-service communication across a network. It provides features like message queues, topics, persistence, and complex routing.
You can think of a message broker as a powerful, distributed implementation of an event bus concept.
Brokers Enable Choreography
Message brokers are the backbone of choreography sagas. They allow services to:
- Publish Events: A service completes a step and publishes an event to a specific topic (a named channel on the broker).
- Subscribe to Events: Other services interested in that event subscribe to the topic and receive the event to trigger their next step.
This decentralized flow, driven by events through a broker, defines the choreography pattern.
Example: Publishing an Event
Here's a simplified example of how an OrderService might prepare and conceptually "publish" an OrderCreated event using a message broker.
The broker handles the actual sending to subscribers.
public class OrderService {
public static void main(String[] args) {
String orderId = "ORD_456";
String customer = "Alice";
String item = "Widget";
// Imagine this event goes to a message broker
String eventPayload = "{ \"type\": \"OrderCreated\", \"orderId\": \"" + orderId + "\", \"customer\": \"" + customer + "\", \"item\": \"" + item + "\" }";
System.out.println("OrderService prepares event:");
System.out.println(eventPayload);
System.out.println("This event would be sent to a 'order_events' topic on a message broker.");
}
}Example: Subscribing to an Event
Now, let's see how a PaymentService would conceptually "subscribe" to and process the OrderCreated event from the broker.
It listens on the order_events topic.
public class PaymentService {
public static void main(String[] args) {
// Imagine PaymentService receives this event from a message broker
String receivedEvent = "{ \"type\": \"OrderCreated\", \"orderId\": \"ORD_456\", \"customer\": \"Alice\", \"item\": \"Widget\" }";
System.out.println("PaymentService received event:");
System.out.println(receivedEvent);
// Parse and process the event
if (receivedEvent.contains("\"type\": \"OrderCreated\"")) {
System.out.println("Processing payment for order: ORD_456...");
System.out.println("Payment processed successfully!");
} else {
System.out.println("Unknown event type received.");
}
}
}Check Your Understanding
Message brokers are crucial for choreography sagas. Which of the following are key benefits provided by message brokers for reliable asynchronous communication?
Recap: Event Bus & Brokers
In this lesson, we explored how Event Buses and Message Brokers facilitate reliable asynchronous communication, especially for choreography sagas.
- An Event Bus is a pattern for in-process event communication.
- A Message Broker is an external, robust system that implements this pattern for distributed services, offering features like durability and guaranteed delivery.
- They enable services to publish events and subscribe to topics, forming the core of how choreography sagas progress.
Next, we'll look at how to handle compensation when things go wrong in these event-driven flows!
เรียนรู้ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “บัสเหตุการณ์และตัวกลางข้อความ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “บัสเหตุการณ์และตัวกลางข้อความ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “บัสเหตุการณ์และตัวกลางข้อความ”
ใช้ตัวกลางข้อความและบัสเหตุการณ์เพื่ออำนวยความสะดวกในการสื่อสารแบบไม่ประสานเวลาที่เชื่อถือได้สำหรับ Saga แบบประสานงานด้วยเหตุการณ์ คุณปฏิบัติ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Microservices Communication Patterns (Saga, Circuit Breaker) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Microservices Communication Patterns (Saga, Circuit Breaker) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “บัสเหตุการณ์และตัวกลางข้อความ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) นี้ได้ไหม
ได้ บทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การออกแบบ Saga ที่ขับเคลื่อนด้วยเหตุการณ์
- บัสเหตุการณ์และตัวกลางข้อความ
- การจัดการการชดเชยด้วยเหตุการณ์
- การสร้างตัวรับเหตุการณ์ที่ไม่ขึ้นต่อการทำซ้ำ