Message Bindings
为 WebSocket、MQTT、Kafka 和其他协议配置协议特定设置
AsyncAPI 中的绑定允许您为服务器、通道、操作和消息指定协议特定信息。这使您能够为不同的消息协议提供详细配置。
asyncapi.yml
channels:user/notifications:address: user/notificationsmessages:UserNotification:$ref: '#/components/messages/UserNotification'bindings:ws:method: GETquery:type: objectproperties:token:type: stringdescription: Authentication token
WebSocket 绑定
为实时通信配置 WebSocket 特定设置:
asyncapi.yml
channels:chat/room:address: chat/room/{roomId}bindings:ws:method: GETheaders:type: objectproperties:Authorization:type: stringquery:type: objectproperties:userId:type: stringroomId:type: stringoperations:sendMessage:action: sendchannel:$ref: '#/channels/chat~1room'bindings:ws:type: requestmethod: message
MQTT 绑定
为 IoT 和消息应用程序配置 MQTT 特定设置:
asyncapi.yml
channels:device/telemetry:address: device/{deviceId}/telemetrybindings:mqtt:qos: 1retain: truemessageExpiryInterval: 3600servers:mqtt-broker:host: mqtt.example.comprotocol: mqttbindings:mqtt:clientId: device-clientcleanSession: falsekeepAlive: 60will:topic: device/statuspayload: "offline"qos: 1retain: true
Kafka 绑定
为高吞吐量事件流配置 Kafka 特定设置:
asyncapi.yml
channels:user.events:address: user.eventsbindings:kafka:topic: user.eventspartitions: 10replicas: 3topicConfiguration:cleanup.policy: ["delete"]retention.ms: 604800000segment.ms: 86400000operations:publishUserEvent:action: sendchannel:$ref: '#/channels/user.events'bindings:kafka:groupId: user-event-processorskey:type: stringdescription: User ID for partitioning
服务器绑定
配置协议特定的服务器设置:
asyncapi.yml
servers:websocket-server:host: ws.example.comprotocol: wssdescription: WebSocket server with SSLbindings:ws:headers:type: objectproperties:X-API-Version:type: stringconst: "v1"kafka-cluster:host: kafka.example.com:9092protocol: kafkabindings:kafka:schemaRegistryUrl: https://schema-registry.example.comschemaRegistryVendor: confluent
消息绑定
配置特定协议如何处理消息:
asyncapi.yml
components:messages:OrderEvent:name: OrderEventcontentType: application/jsonpayload:$ref: '#/components/schemas/Order'bindings:kafka:key:type: stringdescription: Order ID for consistent partitioningheaders:type: objectproperties:eventType:type: stringenum: [created, updated, cancelled]mqtt:qos: 2retain: false
HTTP 绑定
用于基于 HTTP 的协议,如 Server-Sent Events:
asyncapi.yml
channels:events/stream:address: /events/streambindings:http:type: responsemethod: GETquery:type: objectproperties:lastEventId:type: stringdescription: Last received event ID for resumptionoperations:streamEvents:action: sendchannel:$ref: '#/channels/events~1stream'bindings:http:type: responsestatusCode: 200headers:type: objectproperties:Content-Type:type: stringconst: text/event-stream
绑定提供了灵活性,可以利用协议特定功能,同时在不同传输机制之间维护统一的 API 规范。