响应 Azure 应用配置事件Reacting to Azure App Configuration events

通过 Azure 应用配置事件,应用程序能够响应键值更改。Azure App Configuration events enable applications to react to changes in key-values. 实现这一点不需要复杂的代码或成本高昂但效率低下的轮询服务,This is done without the need for complicated code or expensive and inefficient polling services. 而是可以通过 Azure 事件网格向订阅方(如 Azure FunctionsAzure 逻辑应用,或者甚至是你自己的自定义 HTTP 侦听器)推送事件。Instead, events are pushed through Azure Event Grid to subscribers such as Azure Functions, Azure Logic Apps, or even to your own custom http listener. 至关重要的是,你只为你使用的资源付费。Critically, you only pay for what you use.

Azure 应用配置事件会发送到 Azure 事件网格,该服务通过丰富的重试策略和死信传递向应用程序提供可靠的传递服务。Azure App Configuration events are sent to the Azure Event Grid which provides reliable delivery services to your applications through rich retry policies and dead-letter delivery. 若要了解详细信息,请参阅事件网格消息传递和重试To learn more, see Event Grid message delivery and retry.

常见的应用配置事件方案包括刷新应用程序配置、触发部署或任何面向配置的工作流。Common App Configuration event scenarios include refreshing application configuration, triggering deployments, or any configuration-oriented workflow. 基于事件的体系结构对于鲜少更改,但要求立即响应的情况尤为有效。When changes are infrequent, but your scenario requires immediate responsiveness, event-based architecture can be especially efficient.

请参阅将 Azure 应用配置事件路由到自定义 Web 终结点 - CLI,以获取简明示例。Take a look at Route Azure App Configuration events to a custom web endpoint - CLI for a quick example.

事件网格模型

可用的 Azure 应用配置事件Available Azure App Configuration events

事件网格使用事件订阅将事件消息路由到订阅方。Event grid uses event subscriptions to route event messages to subscribers. Azure 应用配置事件订阅可以包括两种类型的事件:Azure App Configuration event subscriptions can include two types of events:

事件名称Event Name 描述Description
Microsoft.AppConfiguration.KeyValueModified 在创建或替换键值时触发Fired when a key-value is created or replaced
Microsoft.AppConfiguration.KeyValueDeleted 在删除键值时触发Fired when a key-value is deleted

事件架构Event schema

Azure 应用配置事件包含响应数据更改所需的全部信息。Azure App Configuration events contain all the information you need to respond to changes in your data. 你可以识别应用配置事件,因为 eventType 属性以“Microsoft.AppConfiguration”开头。You can identify an App Configuration event because the eventType property starts with "Microsoft.AppConfiguration". 关于事件网格事件属性使用情况的其他信息,请参阅文档事件网格事件架构Additional information about the usage of Event Grid event properties is documented in Event Grid event schema.

属性Property 类型Type 说明Description
主题topic 字符串string 发出该事件的应用配置的完整 Azure 资源管理器 ID。Full Azure Resource Manager id of the App Configuration that emits the event.
subjectsubject 字符串string 作为事件主题的键值的 URI。The URI of the key-value that is the subject of the event.
EventTimeeventTime 字符串string 生成事件的日期/时间,采用 ISO 8601 格式。The date/time that the event was generated, in ISO 8601 format.
eventTypeeventType 字符串string “Microsoft.AppConfiguration.KeyValueModified”或“Microsoft.AppConfiguration.KeyValueDeleted”。"Microsoft.AppConfiguration.KeyValueModified" or "Microsoft.AppConfiguration.KeyValueDeleted".
IDId 字符串string 此事件的唯一标识符。A unique identifier of this event.
dataVersiondataVersion stringstring 数据对象的架构版本。The schema version of the data object.
metadataVersionmetadataVersion 字符串string 顶级属性的架构版本。The schema version of top-level properties.
datadata 对象 (object)object Azure 应用配置特定事件数据的集合Collection of Azure App Configuration specific event data
data.keydata.key 字符串string 已修改或已删除的键/值的键。The key of the key-value that was modified or deleted.
data.labeldata.label 字符串string 已修改或已删除的键/值的标签(如果有)。The label, if any, of the key-value that was modified or deleted.
data.etagdata.etag 字符串string 对于 KeyValueModified,为新键/值的 etag。For KeyValueModified the etag of the new key-value. 对于 KeyValueDeleted,为已删除的键/值的 etag。For KeyValueDeleted the etag of the key-value that was deleted.

下面是 KeyValueModified 事件的示例:Here is an example of a KeyValueModified event:

[{
  "id": "84e17ea4-66db-4b54-8050-df8f7763f87b",
  "topic": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testrg/providers/microsoft.appconfiguration/configurationstores/contoso",
  "subject": "https://contoso.azconfig.io/kv/Foo?label=FizzBuzz",
  "data": {
    "key": "Foo",
    "label": "FizzBuzz",
    "etag": "FnUExLaj2moIi4tJX9AXn9sakm0"
  },
  "eventType": "Microsoft.AppConfiguration.KeyValueModified",
  "eventTime": "2019-05-31T20:05:03Z",
  "dataVersion": "1",
  "metadataVersion": "1"
}]

使用事件的做法Practices for consuming events

处理应用配置事件的应用程序应当遵循以下建议的做法:Applications that handle App Configuration events should follow these recommended practices:

  • 可以配置多个订阅,以将事件路由到同一事件处理程序,因此,请勿假定事件来自某个特定源,Multiple subscriptions can be configured to route events to the same event handler, so do not assume events are from a particular source. 而应检查消息的主题,以确保应用配置实例发送该事件。Instead, check the topic of the message to ensure the App Configuration instance sending the event.
  • 检查 eventType,并且不要假定接收的所有事件都会是期望的类型。Check the eventType and do not assume that all events you receive will be the types you expect.
  • 使用 etag 字段以了解有关对象的信息是否仍是最新的。Use the etag fields to understand if your information about objects is still up-to-date.
  • 使用 sequencer 字段以了解任意特定对象上事件的顺序。Use the sequencer fields to understand the order of events on any particular object.
  • 使用 subject 字段以访问修改后的键值。Use the subject field to access the key-value that was modified.

后续步骤Next steps

详细了解事件网格并试用 Azure 应用配置事件:Learn more about Event Grid and give Azure App Configuration events a try: