将 CloudEvents v1.0 架构与 Azure 事件网格配合使用CloudEvents v1.0 schema with Azure Event Grid

除了采用默认事件架构的事件,Azure 事件网格本身还支持采用 CloudEvents v1.0 的 JSON 实现HTTP 协议绑定的事件。In addition to its default event schema, Azure Event Grid natively supports events in the JSON implementation of CloudEvents v1.0 and HTTP protocol binding. CloudEvents 是一种用于描述事件数据的开放规范CloudEvents is an open specification for describing event data.

CloudEvents 提供的常用事件架构适合发布和使用基于云的事件,因此可简化互操作性。CloudEvents simplifies interoperability by providing a common event schema for publishing, and consuming cloud based events. 可以通过此架构使用统一的工具、以标准方式路由和处理事件,以及以通用方式反序列化外部事件架构。This schema allows for uniform tooling, standard ways of routing & handling events, and universal ways of deserializing the outer event schema. 使用通用架构可以更轻松地跨平台集成工作。With a common schema, you can more easily integrate work across platforms.

CloudEvents 是由包括 Microsoft 在内的多个协作者通过 Cloud Native Computing Foundation 构建的。CloudEvents is being built by several collaborators, including Microsoft, through the Cloud Native Computing Foundation. 它目前的发布版本为 1.0。It's currently available as version 1.0.

本文介绍 CloudEvents 架构和事件网格。This article describes CloudEvents schema with Event Grid.

使用 CloudEvents 架构的示例事件Sample event using CloudEvents schema

下面以示例方式说明了 CloudEvents 格式的 Azure Blob 存储事件:Here is an example of an Azure Blob Storage event in CloudEvents format:

{
    "specversion": "1.0",
    "type": "Microsoft.Storage.BlobCreated",  
    "source": "/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-account}",
    "id": "9aeb0fdf-c01e-0131-0922-9eb54906e209",
    "time": "2019-11-18T15:13:39.4589254Z",
    "subject": "blobServices/default/containers/{storage-container}/blobs/{new-file}",
    "dataschema": "#",
    "data": {
        "api": "PutBlockList",
        "clientRequestId": "4c5dd7fb-2c48-4a27-bb30-5361b5de920a",
        "requestId": "9aeb0fdf-c01e-0131-0922-9eb549000000",
        "eTag": "0x8D76C39E4407333",
        "contentType": "image/png",
        "contentLength": 30699,
        "blobType": "BlockBlob",
        "url": "https://gridtesting.blob.core.chinacloudapi.cn/testcontainer/{new-file}",
        "sequencer": "000000000000000000000000000099240000000000c41c18",
        "storageDiagnostics": {
            "batchId": "681fe319-3006-00a8-0022-9e7cde000000"
        }
    }
}

此处提供 CloudEvents v1.0 中的可用字段、类型和定义的详细说明。A detailed description of the available fields, their types, and definitions in CloudEvents v1.0 is available here.

在 CloudEvents 架构和事件网格架构中传递的事件的标头值是相同的,但 content-type 除外。The headers values for events delivered in the CloudEvents schema and the Event Grid schema are the same except for content-type. 对于 CloudEvents 架构,该标头值为 "content-type":"application/cloudevents+json; charset=utf-8"For CloudEvents schema, that header value is "content-type":"application/cloudevents+json; charset=utf-8". 对于事件网格架构,该标头值为 "content-type":"application/json; charset=utf-8"For Event Grid schema, that header value is "content-type":"application/json; charset=utf-8".

CloudEvents 的事件网格Event Grid for CloudEvents

可以将事件网格用于 CloudEvents 架构的事件的输入和输出。You can use Event Grid for both input and output of events in CloudEvents schema. 可以将 CloudEvents 用于系统事件(例如 Blob 存储事件和 IoT 中心事件)和自定义事件。You can use CloudEvents for system events, like Blob Storage events and IoT Hub events, and custom events. 它还可以将网络上的这些事件来回转换。It can also transform those events on the wire back and forth.

输入架构Input schema 输出架构Output schema
CloudEvents 格式CloudEvents format CloudEvents 格式CloudEvents format
事件网格格式Event Grid format CloudEvents 格式CloudEvents format
事件网格格式Event Grid format 事件网格格式Event Grid format

对于所有事件架构,事件网格都要求在发布到事件网格主题时以及在创建事件订阅时进行验证。For all event schemas, Event Grid requires validation when publishing to an event grid topic and when creating an event subscription. 有关详细信息,请参阅事件网格安全性和身份验证For more information, see Event Grid security and authentication.

后续步骤Next steps

请参阅如何将 CloudEvents v1.0 架构与事件网格配合使用See How to use CloudEvents v1.0 schema with Event Grid.