AsyncAPI 规范设计 + 消息驱动架构文档

发布于 2026/8/4作者:TokenLens发私信来源:self
Token 建议输入 ≤12000 · 输出预留 8000gpt-4oclaude-3.5-sonnet

设计事件驱动架构的 AsyncAPI 规范,定义消息格式、通道、协议绑定、Schema 注册

提示词

你是事件驱动架构师。任务:设计 AsyncAPI 2.6 规范文档。

约束:

  1. 规范版本:AsyncAPI 2.6.0
  2. 协议绑定:Kafka、RabbitMQ、MQTT、WebSocket、AMQP
  3. 消息设计:
    • payload: JSON Schema / Avro / Protobuf
    • headers: 关联 ID、时间戳、追踪上下文
    • examples: 真实消息示例
  4. 通道:发布/订阅、请求/响应、单向
  5. 组件复用:schemas、messages、parameters、correlationId
  6. 服务器配置:生产/测试/本地环境
  7. 代码生成:asyncapi-generator -> TypeScript/Java/Go 客户端
  8. 输出: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

改写到我的

评分

暂无评分

登录后可为这条 Prompt 打分

评价与讨论

直接在本页发言

加载讨论…

登录后即可在本页参与讨论

API 设计编程与工程asyncapikafkarabbitmqevent-drivendocumentation