











https://github.com/fanqingsong/springboot-kafka-streams-microservices-demo
An E-commerce Microservices Application that demonstrates the usage of Kafka Streams.
/orders./orders.ms-payment and ms-stock services.Look at the Business Logic Flow (steps 3-6):
3. Payment Service → publishes PAYMENT decision to payments topic.
4. Stock Service → publishes STOCK decision to stock topic.
5. Orders Service → Joins BOTH responses → publishes FINAL order.
6. Results → Stored in KTable for querying.
The Challenge:
payments topic.stock topic.Without a framework, this becomes incredibly complex.
| Business Step | Challenge | Without KS | With KS |
|---|---|---|---|
| Step 3-4 | Consume payment & stock concurrently in orders | Manual threading + offset management | Automatic consumer groups + No listeners |
| Step 5a | Buffer both responses | Manual in-memory maps | Built-in state stores |
| Step 5b | Join by order ID within 10 seconds | Complex correlation logic | join() with time windows |
| Step 5c | Handle late/missing responses | Manual timeout logic | Automatic window expiration |
| Step 6a | Persist final orders | Manual database inserts | Automatic KTable store |
| Step 6b | Recovery after crash | Manual changelog implementation | Automatic changelog topics |
| Step 7 | Query persisted orders | Manual database queries | Query KTable directly |
| Feature | Without Kafka Streams | With Kafka Streams |
|---|---|---|
| Join Logic | 200+ lines of buffer management | 3 lines with .join() |
| Time Windows | Manual timestamp tracking & expiration | Automatic window management |
| State Persistence | Build your own changelog system | Automatic changelog topics |
| Exactly-Once | Complex distributed transaction logic | Guaranteed by framework |
| Scaling | Manual partitioning coordination | Automatic dynamic scaling |
| Failure Recovery | Rebuild state from scratch | Replay from changelog topic |
| Production Ready | Months of testing & hardening | Battle-tested in thousands of companies |
格、status、source)。Topic:orders、payments、stock。消息 key 都是 orderId。
POST /orders → createOrder():用 UUID 高位生成正数 ID,设为 NEW,发到 orders。此时 HTTP 只表示「已发出」,最终成败要等 Join 之后。
ms-payment 用独立 consumer group 听 orders:
NEW → reserve():查客户余额。0 < price < amountAvailable 则把金额从 available 转到 reserved,状态 ACCEPT;否则 REJECT。结果发到 payments。NEW(最终单)→ confirm():CONFIRMED 只清 reserved;ROLLBACK 且失败源不是支付时,把 reserved 退回 available。ms-stock 同样听 orders,逻辑对称:
ACCEPTREJECTstockCONFIRMED / ROLLBACK(失败源不是库存)时扣减或退回预留两边失败都会带 source(PAYMENT 或 STOCK),方便另一边判断该不该回滚自己这边。
ms-orders 的 Kafka Streams:
payments、stockOrderService.confirm(payment, stock) 合成一单,再写回 orders合成规则:
这单再次进入 orders 后:
GET /orders / GET /orders/{id} 不查数据库,而是查 Streams 把 orders 物化出来的本地 Key-Value Store(KTable)。所以能查到的,是已经写进 orders 的消息:创建时的 NEW,以及 Join 后的最终状态。
支付和库存是并行、到达时间不确定的。框架负责:按 key 对齐、10 秒窗口、join 状态 changelog、KTable 查询和崩溃恢复。没有 Streams 就要自己做相关 ID、缓冲和超时。
开发数据:支付侧预置几个客户余额,库存侧预置几种商品数量。余额或库存不够就会走 REJECT / ROLLBACK 那条补偿路径。
出处:http://www.cnblogs.com/lightsong/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。