事件驱动架构(EDA) 是一种设计方式,其中组件通过产生和响应事件(发生的某事)进行通信,而不是直接调用。它实现了松耦合、可扩展性和响应性,在现代分布式系统中很常见。
什么是事件驱动架构
Components communicate via EVENTS (a notification that something happened):
→ PRODUCERS emit events ("order placed", "user signed up") without knowing who handles them
→ CONSUMERS react to events they care about (asynchronously)
→ an event BROKER/bus (Kafka, message queue) routes events
→ components are DECOUPLED → they react to events, not call each other directly.
模式和优势
PATTERNS:
→ PUB/SUB → publish events; multiple subscribers react
→ EVENT STREAMING (Kafka) → durable event logs; consumers process streams
→ EVENT SOURCING → store state as a sequence of events (the events ARE the source of truth)
→ CQRS → separate read/write models, often event-driven
BENEFITS:
✓ LOOSE COUPLING → producers/consumers independent (add consumers without changing producers)
✓ SCALABILITY → async, distribute load; consumers scale independently
✓ RESPONSIVENESS → react to events in real time; extensible (new consumers for new needs)
✓ RESILIENCE → events buffered; components fail independently
