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

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

创建亚马逊EventBridge架构

您可以使用符合 O penAPI 规范或 jsonSchema Draft4 规范的 J SON 文件创建架构。您可以使用模板或基于事件EventBridge的 JSON 生成架构,来创建或上传自己的架构。您也可以从事件总线上的事件推断出架构。要使用架构注册表 API 创建EventBridge架构,请使用 CreateSchemaAPI 操作。

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

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

  • 关键字的处理方式有细微差别,例如typeformat

  • OpenAPI 不支持 JSON 文档中的 jsonSchema 超架构超链接。

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

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

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

使用模板创建架构

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

主题
    下载架构模板
    1. 通过 https://console.aws.amazon.com/events/ 打开亚马逊EventBridge控制台。

    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. 通过 https://console.aws.amazon.com/events/ 打开亚马逊EventBridge控制台。

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

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

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

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

    6. 对于架构类型,选择 OpenAPI 3.0JSON 架构草案 4

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

    8. 选择 Create (创建)

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

    在控制台中编辑架构
    1. 通过 https://console.aws.amazon.com/events/ 打开亚马逊EventBridge控制台。

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

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

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

    5. 对于架构类型,选择 OpenAPI 3.0JSON 架构草案 4

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

    7. 在 “创建” 选项卡上,选择 “加载模板”。

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

    9. 选择 Create (创建)

    根据事件的 JSON 创建架构

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

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

    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. 选择 Discover schema (发现架构)

    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 参考中的发现者

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

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

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

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

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