本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
创建亚马逊EventBridge架构
您可以使用符合 O penAPI 规范或 jsonSchema Draft4 规范的CreateSchema
API 操作。
当你在 OpenAPI 3 和 jsonSchema Draft4 格式之间进行选择时,请考虑以下区别:
-
jsonSchema 格式支持 OpenAPI 不支持的其他关键字,例如。
$schema, additionalItems
-
关键字的处理方式有细微差别,例如
type
和format
。 -
OpenAPI 不支持 JSON 文档中的 jsonSchema 超架构超链接。
-
OpenAPI 的工具往往侧重于构建时,而 jsonSchema 的工具往往侧重于运行时操作,例如用于架构验证的客户端工具。
我们建议使用 jsonSchema 格式实现客户端验证,以便发送的事件EventBridge符合架构。您可以使用 jsonSchema 为有效的 JSON 文档定义合约,然后在发送关联事件之前使用 JSON 架构验证器
有了新架构后,您可以下载代码绑定以帮助使用该架构为事件创建应用程序。
使用模板创建架构
您可以通过模板或直接在EventBridge控制台中编辑模板来创建架构。要获取模板,请从控制台下载该模板。您可以编辑模板,使架构与您的事件相匹配。然后通过控制台上传您的新模板。
主题
下载架构模板
通过 https://console.aws.amazon.com/events/
打开亚马逊EventBridge控制台。 -
在导航窗格中,选择 Schema registry (架构注册表)。
-
在 “架构模板” 下的 “入门” 部分中,选择下载。
或者,您可以从以下代码示例中复制 JSON 模板。
{ "openapi": "3.0.0", "info": { "version": "1.0.0", "title": "Event" }, "paths": {}, "components": { "schemas": { "Event": { "type": "object", "properties": { "ordinal": { "type": "number", "format": "int64" }, "name": { "type": "string" }, "price": { "type": "number", "format": "double" }, "address": { "type": "string" }, "comments": { "type": "array", "items": { "type": "string" } }, "created_at": { "type": "string", "format": "date-time" } } } } } }
上传架构模板
通过 https://console.aws.amazon.com/events/
打开亚马逊EventBridge控制台。 -
在导航窗格中,选择架构,然后选择创建架构。
-
(可选)选择或创建架构注册表。
-
在架构详细信息下,输入架构的名称。
-
(可选)输入架构的描述。
-
对于架构类型,选择 OpenAPI 3.0 或 JSON 架构草案 4。
-
在创建选项卡的文本框中,将架构文件拖到文本框中,或者粘贴架构源。
-
选择 Create (创建)。
直接在控制台中编辑架构模板
在控制台中编辑架构
通过 https://console.aws.amazon.com/events/
打开亚马逊EventBridge控制台。 -
在导航窗格中,选择架构,然后选择创建架构。
-
(可选)选择或创建架构注册表。
-
在架构详细信息下,输入架构的名称。
-
对于架构类型,选择 OpenAPI 3.0 或 JSON 架构草案 4。
-
(可选)输入要创建的架构的描述。
-
在 “创建” 选项卡上,选择 “加载模板”。
-
在文本框中,编辑模板,使架构与您的事件相匹配。
-
选择 Create (创建)。
根据事件的 JSON 创建架构
如果您有事件的 JSON,则可以自动为该类型的事件创建架构。
基于事件的 JSON 创建架构
通过 https://console.aws.amazon.com/events/
打开亚马逊EventBridge控制台。 -
在导航窗格中,选择架构,然后选择创建架构。
-
(可选)选择或创建架构注册表。
-
在 Schema details (架构详细信息) 下,输入架构的名称。
-
(可选)输入您创建的架构的描述。
-
对于架构类型,选择 OpenAPI 3.0。
当你从事件的 JSON 创建架构时,你不能使用 jsonSchema。
-
选择 Discover from JSON (从 JSON 中发现)
-
在 JSON 下的文本框中,粘贴或拖动事件的 JSON 源。
例如,如果执行失败,您可以粘贴此Amazon Step Functions事件的源代码。
{ "version": "0", "id": "315c1398-40ff-a850-213b-158f73e60175", "detail-type": "Step Functions Execution Status Change", "source": "aws.states", "account": "012345678912", "time": "2019-02-26T19:42:21Z", "region": "us-east-1", "resources": [ "arn:aws:states:us-east-1:012345678912:execution:state-machine-name:execution-name" ], "detail": { "executionArn": "arn:aws:states:us-east-1:012345678912:execution:state-machine-name:execution-name", "stateMachineArn": "arn:aws:states:us-east-1:012345678912:stateMachine:state-machine", "name": "execution-name", "status": "FAILED", "startDate": 1551225146847, "stopDate": 1551225151881, "input": "{}", "output": null } }
-
选择 Discover schema (发现架构)。
-
EventBridge为该事件生成一个 OpenAPI 架构。例如,以下架构是为前面的 Step Functions 事件生成的。
{ "openapi": "3.0.0", "info": { "version": "1.0.0", "title": "StepFunctionsExecutionStatusChange" }, "paths": {}, "components": { "schemas": { "AWSEvent": { "type": "object", "required": ["detail-type", "resources", "detail", "id", "source", "time", "region", "version", "account"], "x-amazon-events-detail-type": "Step Functions Execution Status Change", "x-amazon-events-source": "aws.states", "properties": { "detail": { "$ref": "#/components/schemas/StepFunctionsExecutionStatusChange" }, "account": { "type": "string" }, "detail-type": { "type": "string" }, "id": { "type": "string" }, "region": { "type": "string" }, "resources": { "type": "array", "items": { "type": "string" } }, "source": { "type": "string" }, "time": { "type": "string", "format": "date-time" }, "version": { "type": "string" } } }, "StepFunctionsExecutionStatusChange": { "type": "object", "required": ["output", "input", "executionArn", "name", "stateMachineArn", "startDate", "stopDate", "status"], "properties": { "executionArn": { "type": "string" }, "input": { "type": "string" }, "name": { "type": "string" }, "output": {}, "startDate": { "type": "integer", "format": "int64" }, "stateMachineArn": { "type": "string" }, "status": { "type": "string" }, "stopDate": { "type": "integer", "format": "int64" } } } } } }
-
生成架构后,选择创建。
根据事件总线上的事件创建架构
EventBridge可以通过发现事件来推断架构。要推断架构,请在事件总线上开启事件发现,然后将每个唯一的架构添加到架构注册表中,包括跨账户事件的架构。发现的架构EventBridge出现在 “架构” 页面的已发现架构注册表中。
如果事件总线上的事件内容发生变化,则EventBridge创建相关EventBridge架构的新版本。
注意
在事件总线上启用事件发现可能会产生费用。每个月处理的前 500 万个事件是免费的。
注意
EventBridge默认情况下,从跨账户事件中推断出架构,但您可以通过更新属性将其禁用。cross-account
有关更多信息,请参阅EventBridge架构注册表 API 参考中的发现者。
在事件总线上启用架构发现
通过 https://console.aws.amazon.com/events/
打开亚马逊EventBridge控制台。 -
在导航窗格中,选择 Event Buses (事件总线)。
-
请执行下列操作之一:
-
要在默认事件总线上启用发现,请选择开始发现。
-
要在自定义事件总线上启用发现,请选择自定义事件总线的单选按钮,然后选择开始发现。
-