Microservices Communication Patterns (Saga, Circuit Breaker) · บทเรียน

การออกแบบ Saga ที่ขับเคลื่อนด้วยเหตุการณ์

ออกแบบลำดับการทำงานของ Saga แบบประสานงานด้วยเหตุการณ์ โดยใช้เหตุการณ์กระตุ้นการทำงานข้ามไมโครเซอร์วิสต่าง ๆ

บทเรียน 1 จาก 412 ขั้นตอน

การออกแบบ Saga ที่ขับเคลื่อนด้วยเหตุการณ์ เป็นบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Microservices Communication Patterns (Saga, Circuit Breaker) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

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

Event-Driven Saga Design

Welcome! In this lesson, we'll learn how to design the flow of a Choreography Saga. This pattern helps manage complex business transactions that span multiple services.

We'll focus on using events as the primary way services communicate and trigger each other's actions.

Choreography Refresher

Remember Choreography Sagas? In this style, services communicate directly by publishing and subscribing to events.

  • No central orchestrator: Services react to events from others.
  • Decentralized decisions: Each service decides its next step based on the event it receives.

Our Business Scenario

Let's design a saga for an "Online Order Placement" system. This common scenario involves several steps across different microservices:

  • Customer places order.
  • Inventory is checked and reserved.
  • Payment is processed.
  • Order is shipped.

Identifying Core Saga Steps

For our "Order Placement" saga, we can break it down into these main steps, each potentially handled by a different service:

  • Order Service: Creates the order.
  • Inventory Service: Reserves items.
  • Payment Service: Processes payment.
  • Shipping Service: Schedules shipment.

Events Drive the Flow

In a choreography saga, each step is initiated by an event. When one service completes its task, it publishes an event.

Other services, interested in that event, subscribe to it and react accordingly. This creates a chain reaction.

Kicking Off the Saga

Every saga needs a starting point. For our "Order Placement," the Order Service initiates the process.

When a customer places an order, the Order Service creates it and publishes an OrderCreatedEvent.

Inventory Reacts to Order

The Inventory Service subscribes to OrderCreatedEvent. Upon receiving it, it attempts to reserve the items for the order.

After processing, it publishes either an InventoryReservedEvent or an InventoryFailedEvent.

class OrderCreatedEvent {
    String orderId;
    String customerId;
    // ... item details
}

class InventoryService {
    public void handleOrderCreated(OrderCreatedEvent event) {
        System.out.println("Inventory Service: Received OrderCreatedEvent for " + event.orderId);
        // Logic to reserve items...
        boolean success = true; // Assume success for now
        if (success) {
            System.out.println("Inventory Service: Items reserved. Publishing InventoryReservedEvent.");
            // In a real system, publish InventoryReservedEvent
        } else {
            System.out.println("Inventory Service: Inventory failed. Publishing InventoryFailedEvent.");
            // In a real system, publish InventoryFailedEvent
        }
    }
}

public class Main {
    public static void main(String[] args) {
        InventoryService inventory = new InventoryService();
        OrderCreatedEvent newOrder = new OrderCreatedEvent();
        newOrder.orderId = "ORD123";
        newOrder.customerId = "CUST456";

        inventory.handleOrderCreated(newOrder);
    }
}

Payment Reacts to Inventory

The Payment Service subscribes to InventoryReservedEvent. This means payment only proceeds if inventory is successfully reserved.

It processes the payment and then publishes either a PaymentProcessedEvent or a PaymentFailedEvent.

Shipping Reacts to Payment

Finally, the Shipping Service subscribes to PaymentProcessedEvent. It schedules the shipment only after payment is confirmed.

It then publishes an OrderShippedEvent to complete the main saga flow.

Planning for Rollbacks

What if a step fails? For example, if payment fails after inventory is reserved? In a choreography saga, we use compensation events.

The Payment Service would publish a PaymentFailedEvent. The Inventory Service would subscribe to this event and publish an InventoryReleasedEvent to undo its previous action.

Saga Flow Check

Consider our Order Placement saga. What is the correct sequence of events if the entire process is successful?

Recap: Designing Event Flows

We've learned how to design a choreography saga by breaking down a business transaction into steps.

  • Each step publishes an event to trigger the next.
  • We identified the initial event and subsequent events for a successful flow.
  • We also briefly considered how compensation events are used to handle failures.

Next, we'll explore how message brokers facilitate this communication!

เริ่มต้นได้ฟรี

เรียนรู้ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

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

บทเรียน “การออกแบบ Saga ที่ขับเคลื่อนด้วยเหตุการณ์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การออกแบบ Saga ที่ขับเคลื่อนด้วยเหตุการณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบ Saga ที่ขับเคลื่อนด้วยเหตุการณ์”

ออกแบบลำดับการทำงานของ Saga แบบประสานงานด้วยเหตุการณ์ โดยใช้เหตุการณ์กระตุ้นการทำงานข้ามไมโครเซอร์วิสต่าง ๆ คุณปฏิบัติ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Microservices Communication Patterns (Saga, Circuit Breaker) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Microservices Communication Patterns (Saga, Circuit Breaker) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การออกแบบ Saga ที่ขับเคลื่อนด้วยเหตุการณ์” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) นี้ได้ไหม

ได้ บทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การออกแบบ Saga ที่ขับเคลื่อนด้วยเหตุการณ์
  2. บัสเหตุการณ์และตัวกลางข้อความ
  3. การจัดการการชดเชยด้วยเหตุการณ์
  4. การสร้างตัวรับเหตุการณ์ที่ไม่ขึ้นต่อการทำซ้ำ
← กลับไปที่ Microservices Communication Patterns (Saga, Circuit Breaker)