创建亚马逊 EventBridge 架构 - Amazon EventBridge
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

创建亚马逊 EventBridge 架构

可以使用 OpenAPI 规范JSONSchema Draft4 规范的 JSON 文件来创建架构。您可以使用模板或根据事件的 JSON 生成架构,在中 EventBridge 创建或上传自己的架构。您也可以从事件总线中的事件推断出架构。要使用架构注册表 API 创建 EventBridge 架构,请使用 CreateSchemaAPI 操作。

在 OpenAPI 3 和 JSONSchema Draft4 格式之间进行选择时,请考虑以下区别:

  • JSONSchema 格式支持 OpenAPI 不支持的其他关键字,例如 $schema, additionalItems

  • 关键字的处理方式略有不同,例如 typeformat

  • OpenAPI 不支持 JSON 文档中的 JSONSchema Hyper-Schema 超链接。

  • OpenAPI 的工具往往侧重于构建时,而 JSONSchema 的工具往往侧重于运行时操作,例如用于架构验证的客户端工具。

我们建议使用 JSonSchema 格式来实现客户端验证,以便发送的事件 EventBridge 符合架构。您可以使用 JSONSchema 为有效的 JSON 文档定义合同,然后在发送关联事件之前使用 JSON 架构验证器

有了新架构后,您可以下载代码绑定,以帮助使用该架构为事件创建应用程序。

使用模板创建架构

您可以根据模板创建架构,也可以通过直接在 EventBridge控制台中编辑模板来创建架构。要获取模板,请从控制台下载该模板。您可以编辑模板,使架构与您的事件匹配。然后通过控制台上传您的新模板。

主题
    下载架构模板
    1. 打开亚马逊 EventBridge 控制台,网址为 https://console.aws.amazon.com/events/

    2. 在导航窗格中,选择 Schema registry (架构注册表)

    3. 架构模板下的入门部分,选择下载

    或者,您也可以从以下代码示例中复制 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" } } } } } }

    上传架构模板
    1. 打开亚马逊 EventBridge 控制台,网址为 https://console.aws.amazon.com/events/

    2. 在导航窗格中,选择架构,然后选择创建架构

    3. (可选)选择或创建架构注册表。

    4. 架构详细信息下,输入架构的名称。

    5. (可选)为架构输入描述。

    6. 对于架构类型,请选择 OpenAPI 3.0JSON Schema Draft 4

    7. 创建 选项卡上,将架构文件拖动到文本框中,或粘贴架构源。

    8. 选择创建

    直接在控制台中编辑架构模板

    在控制台中编辑架构
    1. 打开亚马逊 EventBridge 控制台,网址为 https://console.aws.amazon.com/events/

    2. 在导航窗格中,选择架构,然后选择创建架构

    3. (可选)选择或创建架构注册表。

    4. 架构详细信息下,输入架构的名称。

    5. 对于架构类型,请选择 OpenAPI 3.0JSON Schema Draft 4

    6. (可选)为创建的架构输入描述。

    7. 创建选项卡上,选择加载模板

    8. 在文本框中编辑模板,使架构与您的事件匹配。

    9. 选择创建

    根据事件的 JSON 创建架构

    如果您有事件的 JSON,则可以自动为该类型的事件创建架构。

    基于事件的 JSON 创建架构
    1. 打开亚马逊 EventBridge 控制台,网址为 https://console.aws.amazon.com/events/

    2. 在导航窗格中,选择架构,然后选择创建架构

    3. (可选)选择或创建架构注册表。

    4. Schema details (架构详细信息) 下,输入架构的名称。

    5. (可选)输入所创建架构的描述。

    6. 架构类型选择 OpenAPI 3.0

      根据事件的 JSON 创建架构时,不能使用 JSONSchema。

    7. 选择 Discover from JSON (从 JSON 中发现)

    8. 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 } }
    9. 选择 发现架构

    10. 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" } } } } } }
    11. 生成架构后,选择创建

    根据事件总线中的事件创建架构

    EventBridge 可以通过发现事件来推断架构。要推断架构,您可以针对事件总线开启事件发现,并将每个唯一架构添加到架构注册表中,包括跨账户事件的架构。由发现的架构 EventBridge 显示在 “架构” 页面上的 “已发现架构” 注册表中。

    如果事件总线上的事件内容发生变化,则 EventBridge 会创建相关 EventBridge 架构的新版本。

    注意

    在事件总线上启用事件发现会产生成本。每个月处理的前 500 万个事件是免费的。

    注意

    EventBridge 默认情况下,根据跨账户事件推断架构,但您可以通过更新属性将其禁用。cross-account有关更多信息,请参阅《 EventBridge 架构注册表 API 参考》中的 Discoverers

    在事件总线上启用架构发现
    1. 打开亚马逊 EventBridge 控制台,网址为 https://console.aws.amazon.com/events/

    2. 在导航窗格中,选择 Event Buses (事件总线)

    3. 请执行以下操作之一:

      • 要在默认事件总线上启用发现,请选择开始发现

      • 要在自定义事件总线上启用发现,请选择自定义事件总线的单选按钮,然后选择开始发现