AsyncAPI 规范设计 + 消息驱动架构文档
设计事件驱动架构的 AsyncAPI 规范,定义消息格式、通道、协议绑定、Schema 注册
提示词
你是事件驱动架构师。任务:设计 AsyncAPI 2.6 规范文档。
约束:
- 规范版本:AsyncAPI 2.6.0
- 协议绑定:Kafka、RabbitMQ、MQTT、WebSocket、AMQP
- 消息设计:
- payload: JSON Schema / Avro / Protobuf
- headers: 关联 ID、时间戳、追踪上下文
- examples: 真实消息示例
- 通道:发布/订阅、请求/响应、单向
- 组件复用:schemas、messages、parameters、correlationId
- 服务器配置:生产/测试/本地环境
- 代码生成:asyncapi-generator -> TypeScript/Java/Go 客户端
- 输出:asyncapi.yaml + 生成代码 + 架构决策记录 (ADR)
输出格式:
asyncapi.yaml:
// generated/client.ts
ADR: 事件驱动架构选型:
少样本示例
展示 AsyncAPI 完整结构、Kafka 绑定、消息头、Schema、安全
输入
业务:订单域事件。事件:OrderCreated、OrderPaid、OrderShipped、OrderCancelled。消息队列:Kafka。需求:Schema 演进、向后兼容、消费者契约测试。
输出
asyncapi.yaml:
asyncapi: 2.6.0
info:
title: Order Events API
version: 1.0.0
description: 订单领域事件流
servers:
production:
url: kafka.prod.example.com:9092
protocol: kafka
security:
- sasl-scram: []
development:
url: localhost:9092
protocol: kafka
channels:
order.events:
description: 订单域事件主题
publish:
operationId: publishOrderEvent
summary: 发布订单事件
message:
$ref: '#/components/messages/OrderEvent'
subscribe:
operationId: subscribeOrderEvent
summary: 订阅订单事件
message:
$ref: '#/components/messages/OrderEvent'
components:
messages:
OrderEvent:
name: OrderEvent
title: 订单事件
summary: 订单生命周期事件
correlationId:
location: '$message.header#/correlationId'
headers:
type: object
required: [eventId, occurredAt, traceId]
properties:
eventId: { type: string, format: uuid }
occurredAt: { type: string, format: date-time }
traceId: { type: string }
eventType: { type: string, enum: [OrderCreated, OrderPaid, OrderShipped, OrderCancelled] }
payload:
$ref: '#/components/schemas/OrderEventPayload'
schemas:
OrderEventPayload:
type: object
required: [orderId, customerId, amount, currency]
properties:
orderId: { type: string, format: uuid }
customerId: { type: string, format: uuid }
amount: { type: integer, minimum: 0 }
currency: { type: string, enum: [CNY, USD] }
items:
type: array
items:
type: object
required: [productId, quantity, unitPrice]
properties:
productId: { type: string }
quantity: { type: integer, minimum: 1 }
unitPrice: { type: integer, minimum: 0 }
securitySchemes:
sasl-scram:
type: sasl-scram
mechanism: SCRAM-SHA-512