Azure Cosmos DB 和 Azure Functions 的基于事件的无服务器体系结构Serverless event-based architectures with Azure Cosmos DB and Azure Functions

Azure Functions 提供连接到更改源的最简单方法。Azure Functions provides the simplest way to connect to the change feed. 可以创建小型的反应式 Azure Functions,每当 Azure Cosmos 容器的更改源中出现新事件时,就会将其触发。You can create small reactive Azure Functions that will be automatically triggered on each new event in your Azure Cosmos container's change feed.

基于事件的无服务器 Functions 使用适用于 Cosmos DB 的 Azure Functions 触发器

有了适用于 Cosmos DB 的 Azure Functions 触发器,你就可以利用更改源处理器的缩放与可靠事件检测功能,无需维护任何辅助角色基础结构With the Azure Functions trigger for Cosmos DB, you can leverage the Change Feed Processor's scaling and reliable event detection functionality without the need to maintain any worker infrastructure. 只需专注于 Azure 函数的逻辑,而无需考虑事件溯源管道的剩余。Just focus on your Azure Function's logic without worrying about the rest of the event-sourcing pipeline. 甚至可以将触发器和任何其他 Azure Functions 绑定搭配使用。You can even mix the Trigger with any other Azure Functions bindings.


目前,仅支持将适用于 Cosmos DB 的 Azure Functions 触发器与 Core (SQL) API 配合使用。Currently, the Azure Functions trigger for Cosmos DB is supported for use with the Core (SQL) API only.


若要实现基于事件的无服务器流,需要:To implement a serverless event-based flow, you need:

  • 受监视的容器:受监视的容器是正在受到监视的 Azure Cosmos 容器,它存储生成了更改源的数据。The monitored container: The monitored container is the Azure Cosmos container being monitored, and it stores the data from which the change feed is generated. 对受监视容器的任何插入、更新都会反映在容器的更改源中。Any inserts, updates to the monitored container are reflected in the change feed of the container.
  • 租约容器:租约容器维护多个动态无服务器 Azure 函数实例的状态,并启用动态缩放。The lease container: The lease container maintains state across multiple and dynamic serverless Azure Function instances and enables dynamic scaling. 可以通过适用于 Cosmos DB 的 Azure Functions 触发器手动或自动创建此租约容器。This lease container can be manually or automatically created by the Azure Functions trigger for Cosmos DB. 若要自动创建租约容器,请在配置中设置 CreateLeaseCollectionIfNotExists 标记。To automatically create the lease container, set the CreateLeaseCollectionIfNotExists flag in the configuration. 分区的租约容器需要包含 /id 分区键定义。Partitioned lease containers are required to have a /id partition key definition.

创建适用于 Cosmos DB 的 Azure Functions 触发器Create your Azure Functions trigger for Cosmos DB

现在支持在所有 Azure Functions IDE 和 CLI 集成中创建包含适用于 Cosmos DB 的 Azure Functions 触发器的 Azure 函数:Creating your Azure Function with an Azure Functions trigger for Cosmos DB is now supported across all Azure Functions IDE and CLI integrations:

在本地运行触发器Run your trigger locally

可以使用 Azure Cosmos DB 模拟器在本地运行 Azure 函数,以创建和开发基于事件的无服务器流,而无需使用 Azure 订阅,也不会产生任何费用。You can run your Azure Function locally with the Azure Cosmos DB Emulator to create and develop your serverless event-based flows without an Azure Subscription or incurring any costs.

后续步骤Next steps

接下来,请通过以下文章继续详细了解更改源:You can now continue to learn more about change feed in the following articles: