Async API 规范
异步 API 规范
免责声明
版本 2.0.0
介绍
1 user/signedup:
2 subscribe:
3 $ref: "#/components/messages/userSignUp"
user/signedup
频道。定义
应用
制作人
消费者
信息
渠道
协议
规格
格式
1 {
2 "field" : [...]
3 }
文件结构
$ref
中的规范中的字段。asyncapi.json
或asyncapi.yaml
.图式
异步 API 对象
固定字段
字段名称 | 类型 | 描述 |
---|---|---|
asyncapi | AsyncAPI Version String | 必需的。 指定正在使用的 AsyncAPI 规范版本。工具规范和客户端可以使用它来解释版本。结构应为major . minor . patch ,其中patch 版本必须与现有的 兼容major 。minor 工装。通常会引入补丁版本来解决文档中的错误,并且工具通常应该与相应的major . minor (1.0.*)。补丁版本将对应本文档的补丁。 |
id | Identifier | AsyncAPI 文档正在定义的应用程序的标识符。 |
info | Info Object | 必需的。提供有关 API 的元数据。如果需要,客户端可以使用元数据。 |
servers | Servers Object | 提供服务器的连接详细信息。 |
channels | Channels Object | 必需 API 的可用通道和消息。 |
components | Components Object | 用于保存规范的各种模式的元素。 |
tags | Tags Object | 规范使用的带有附加元数据的标签列表。列表中的每个标签名称必须是唯一的。 |
externalDocs | External Documentation Object | 额外的外部文档。 |
AsyncAPI 版本字符串
major
. minor
. patch
. patch
可以用连字符和额外的字母数字字符作为后缀。major
。minor
应用于指定 AsyncAPI 规范版本,并将被视为与该 指定的 AsyncAPI 规范兼容major
。minor
版本。补丁版本不会被工具考虑,不区分1.0.0
和1.0.1
。minor
不应干扰为较低次要版本开发的工具的操作。因此,假设的1.1.0
规范应该可用于为 . 设计的工具1.0.0
。标识符
例子
1 {
2 "id": "urn:com:smartylighting:streetlights:server"
3 }
id: 'urn:com:smartylighting:streetlights:server'
1 {
2 "id": "https://github.com/smartylighting/streetlights-server"
3 }
id: 'https://github.com/smartylighting/streetlights-server'
信息对象
固定字段
字段名称 | 类型 | 描述 |
---|---|---|
title | string | 必须的。应用程序的标题。 |
version | string | 必需提供应用程序 API 的版本(不要与规范版本混淆)。 |
description | string | 应用程序的简短描述。CommonMark 语法可用于富文本表示。 |
termsOfService | string | API 服务条款的 URL。必须采用 URL 格式。 |
contact | Contact Object | 公开的 API 的联系信息。 |
license | License Object | 公开的 API 的许可证信息。 |
信息对象示例:
1 {
2 "title": "AsyncAPI Sample App",
3 "description": "This is a sample server.",
4 "termsOfService": "http://asyncapi.org/terms/",
5 "contact": {
6 "name": "API Support",
7 "url": "http://www.asyncapi.org/support",
8 "email": "support@asyncapi.org"
9 },
10 "license": {
11 "name": "Apache 2.0",
12 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
13 },
14 "version": "1.0.1"
15 }
1 title: AsyncAPI Sample App
2 description: This is a sample server.
3 termsOfService: http://asyncapi.org/terms/
4 contact:
5 name: API Support
6 url: http://www.asyncapi.org/support
7 email: support@asyncapi.org
8 license:
9 name: Apache 2.0
10 url: http://www.apache.org/licenses/LICENSE-2.0.html
11 version: 1.0.1
联系对象
固定字段
字段名称 | 类型 | 描述 |
---|---|---|
name | string | 联系人/组织的识别名称。 |
url | string | 指向联系信息的 URL。必须采用 URL 格式。 |
string | 联系人/组织的电子邮件地址。必须采用电子邮件地址的格式。 |
联系人对象示例:
1 {
2 "name": "API Support",
3 "url": "http://www.example.com/support",
4 "email": "support@example.com"
5 }
1 name: API Support
2 url: http://www.example.com/support
3 email: support@example.com
许可证对象
固定字段
字段名称 | 类型 | 描述 |
---|---|---|
name | string | 必需的。用于 API 的许可证名称。 |
url | string | 用于 API 的许可证的 URL。必须采用 URL 格式。 |
许可证对象示例:
1 {
2 "name": "Apache 2.0",
3 "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
4 }
1 name: Apache 2.0
2 url: http://www.apache.org/licenses/LICENSE-2.0.html
服务器对象
图案字段
字段模式 | 类型 | 描述 |
---|---|---|
^[A-Za-z0-9_-]+$ | Server Object | 这个应用程序可以连接到的服务器的定义。 |
服务器对象示例
1 {
2 "production": {
3 "url": "development.gigantic-server.com",
4 "description": "Development server",
5 "protocol": "kafka",
6 "protocolVersion": "1.0.0"
7 }
8 }
1 production:
2 url: development.gigantic-server.com
3 description: Development server
4 protocol: kafka
5 protocolVersion: '1.0.0'
服务器对象
固定字段
字段名称 | 类型 | 描述 |
---|---|---|
url | string | 必需的。目标主机的 URL。此 URL 支持服务器变量并且可以是相对的,以指示主机位置是相对于提供 AsyncAPI 文档的位置。{ 当在括号中命名变量时,将进行变量替换} 。 |
protocol | string | 必需的。此 URL 支持的连接协议。支持的协议包括但不限于:amqp amqps http https jms kafka kafka-secure mqtt secure-mqtt stomp stomps ws wss |
protocolVersion | string | 用于连接的协议版本。例如: AMQP 0.9.1 、 HTTP 2.0 、 Kafka1.0.0 等。 |
description | string | 可选字符串,描述 URL 指定的主机。CommonMark 语法可以用于富文本表示。 |
variables | Map[string , Server Variable Object] | 变量名称与其值之间的映射。该值用于替换服务器的 URL 模板。 |
security | [Security Requirement Object] | 该服务器可以使用哪些安全机制的声明。值列表包括可以使用的替代安全要求对象。只需满足其中一个安全要求对象即可授权连接或操作。 |
bindings | Server Bindings Object | 一个自由格式的映射,其中键描述协议的名称,值描述服务器的协议特定定义。 |
服务器对象示例
1 {
2 "url": "development.gigantic-server.com",
3 "description": "Development server",
4 "protocol": "kafka",
5 "protocolVersion": "1.0.0"
6 }
1 url: development.gigantic-server.com
2 description: Development server
3 protocol: kafka
4 protocolVersion: '1.0.0'
servers
:
1 {
2 "servers": {
3 "development": {
4 "url": "development.gigantic-server.com",
5 "description": "Development server",
6 "protocol": "amqp",
7 "protocolVersion": "0.9.1"
8 },
9 "staging": {
10 "url": "staging.gigantic-server.com",
11 "description": "Staging server",
12 "protocol": "amqp",
13 "protocolVersion": "0.9.1"
14 },
15 "production": {
16 "url": "api.gigantic-server.com",
17 "description": "Production server",
18 "protocol": "amqp",
19 "protocolVersion": "0.9.1"
20 }
21 }
22 }
1 servers:
2 development:
3 url: development.gigantic-server.com
4 description: Development server
5 protocol: amqp
6 protocolVersion: 0.9.1
7 staging:
8 url: staging.gigantic-server.com
9 description: Staging server
10 protocol: amqp
11 protocolVersion: 0.9.1
12 production:
13 url: api.gigantic-server.com
14 description: Production server
15 protocol: amqp
16 protocolVersion: 0.9.1
1 {
2 "servers": [
3 {
4 "url": "{username}.gigantic-server.com:{port}/{basePath}",
5 "description": "The production API server",
6 "protocol": "secure-mqtt",
7 "variables": {
8 "username": {
9 "default": "demo",
10 "description": "This value is assigned by the service provider, in this example `gigantic-server.com`"
11 },
12 "port": {
13 "enum": [
14 "8883",
15 "8884"
16 ],
17 "default": "8883"
18 },
19 "basePath": {
20 "default": "v2"
21 }
22 }
23 }
24 ]
25 }
1 servers:
2 - url: '{username}.gigantic-server.com:{port}/{basePath}'
3 description: The production API server
4 protocol: secure-mqtt
5 variables:
6 username:
7 # note! no enum here means it is an open value
8 default: demo
9 description: This value is assigned by the service provider, in this example `gigantic-server.com`
10 port:
11 enum:
12 - '8883'
13 - '8884'
14 default: '8883'
15 basePath:
16 # open meaning there is the opportunity to use special base paths as assigned by the provider, default is `v2`
17 default: v2
服务器变量对象
固定字段
字段名称 | 类型 | 描述 |
---|---|---|
enum | [string ] | 如果替换选项来自有限集合,则要使用的字符串值的枚举。 |
default | string | 如果未提供替代值,则用于替换和发送的默认值。 |
description | string | 服务器变量的可选描述。CommonMark 语法可以用于富文本表示。 |
examples | [string ] | 服务器变量的示例数组。 |
默认内容类型
application/json
)。当省略 contentType 属性时,模式解析器必须使用此值。默认内容类型示例
1 {
2 "defaultContentType": "application/json"
3 }
defaultContentType: application/json
通道对象
图案字段
字段模式 | 类型 | 描述 |
---|---|---|
{channel} | Channel Item Object | 单个通道的相对路径。字段名称必须采用RFC 6570 URI 模板的形式。不得使用查询参数和片段,而是使用绑定来定义它们。 |
通道对象示例
1 {
2 "user/signedup": {
3 "subscribe": {
4 "$ref": "#/components/messages/userSignedUp"
5 }
6 }
7 }
1 user/signedup:
2 subscribe:
3 $ref: "#/components/messages/userSignedUp"
渠道项目对象
固定字段
字段名称 | 类型 | 描述 |
---|---|---|
$ref | string | 允许对此频道项进行外部定义。引用的结构必须采用 Channel Item Object 的格式。如果引用的定义与此频道项的定义之间存在冲突,则行为为undefined。 |
description | string | 此频道项目的可选描述。CommonMark 语法 可用于富文本表示。 |
subscribe | Operation Object | SUBSCRIBE 操作的定义。 |
publish | Operation Object | 发布操作的定义。 |
parameters | Parameters Object | 通道名称中包含的参数映射。它应该仅在使用带有表达式的通道时出现(如 RFC 6570 第 2.2 节 所定义)。 |
bindings | Channel Bindings Object | 一个自由格式的映射,其中键描述协议的名称,值描述通道的协议特定定义。 |
渠道项目对象示例
1 {
2 "description": "This channel is used to exchange messages about users signing up",
3 "subscribe": {
4 "summary": "A user signed up.",
5 "message": {
6 "description": "A longer description of the message",
7 "payload": {
8 "type": "object",
9 "properties": {
10 "user": {
11 "$ref": "#/components/schemas/user"
12 },
13 "signup": {
14 "$ref": "#/components/schemas/signup"
15 }
16 }
17 }
18 }
19 },
20 "bindings": {
21 "amqp": {
22 "is": "queue",
23 "queue": {
24 "exclusive": true
25 }
26 }
27 }
28 }
1 description: This channel is used to exchange messages about users signing up
2 subscribe:
3 summary: A user signed up.
4 message:
5 description: A longer description of the message
6 payload:
7 type: object
8 properties:
9 user:
10 $ref: "#/components/schemas/user"
11 signup:
12 bindings:
13 amqp:
14 is: queue
15 queue:
16 exclusive: true
oneOf
为每个操作指定多条消息:
1 {
2 "subscribe": {
3 "message": {
4 "oneOf": [
5 { "$ref": "#/components/messages/signup" },
6 { "$ref": "#/components/messages/login" }
7 ]
8 }
9 }
10 }
1 subscribe:
2 message:
3 oneOf:
4 - $ref: '#/components/messages/signup'
5 - $ref: '#/components/messages/login'