0Pricing
Microservices Communication Patterns (Saga, Circuit Breaker) · 课时

事件总线与消息代理

利用消息代理和事件总线,为编舞式 Saga 实现可靠的异步通信。

事件总线与消息代理 是 CoddyKit 上的免费 Microservices Communication Patterns (Saga, Circuit Breaker) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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, publishes PaymentProcessed
  • Shipping Service subscribes to PaymentProcessed, publishes ShipmentScheduled

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 UserSaved event 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!

常见问题解答

「事件总线与消息代理」课时是免费的吗?

是的 — 「事件总线与消息代理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Microservices Communication Patterns (Saga, Circuit Breaker) 课程的其余内容,请升级到 CoddyKit PRO。 Microservices Communication Patterns (Saga, Circuit Breaker) 课程共包含 4 节课。

「事件总线与消息代理」这节课中我会学到什么?

利用消息代理和事件总线,为编舞式 Saga 实现可靠的异步通信。 你通过在浏览器中直接运行的动手代码来练习 Microservices Communication Patterns (Saga, Circuit Breaker),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Microservices Communication Patterns (Saga, Circuit Breaker) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Microservices Communication Patterns (Saga, Circuit Breaker) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「事件总线与消息代理」课时需要多长时间?

大多数 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)