使用 Azure Functions 计时器触发器缩放 Azure Cosmos DB 吞吐量Scale Azure Cosmos DB throughput by using Azure Functions Timer trigger

Azure Cosmos 帐户的性能基于以每秒请求单位数(RU/秒)表示的预配吞吐量的数量。The performance of an Azure Cosmos account is based on the amount of provisioned throughput expressed in Request Units per second (RU/s). 预配粒度为秒,并且基于最高的 RU/秒按小时计费。The provisioning is at a second granularity and is billed based upon the highest RU/s per hour. 这种预配的容量模型使服务能够提供可预测且一致的吞吐量,保证低延迟、高可用性。This provisioned capacity model enables the service to provide a predictable and consistent throughput, guaranteed low latency, and high availability. 大多数生产工作负荷都具有这些特征。Most production workloads these features. 但是,在开发和测试环境中,如果 Azure Cosmos DB 只在工作时间使用,则可以在上午提高吞吐量,在晚上在工作时间过后降低吞吐量。However, in development and testing environments where Azure Cosmos DB is only used during working hours, you can scale up the throughput in the morning and scale back down in the evening after working hours.

可以通过 Azure 资源管理器模板Azure CLIPowerShell 为 Core (SQL) API 帐户设置吞吐量,或者使用语言特定的 Azure Cosmos DB SDK。You can set the throughput via Azure Resource Manager Templates, Azure CLI, and PowerShell, for Core (SQL) API accounts, or by using the language-specific Azure Cosmos DB SDKs. 使用资源管理器模板、Azure CLI 或 PowerShell 的好处在于它们支持所有 Azure Cosmos DB 模型 API。The benefit of using Resource Manager Templates, Azure CLI or PowerShell is that they support all Azure Cosmos DB model APIs.

吞吐量计划程序示例项目Throughput scheduler sample project

为了简化按计划缩放 Azure Cosmos DB 的过程,我们创建了一个名为 Azure Cosmos 吞吐量计划程序的示例项目。To simplify the process to scale Azure Cosmos DB on a schedule we've created a sample project called Azure Cosmos throughput scheduler. 此项目是一个 Azure Functions 应用,它有两个计时器触发器:ScaleUpTrigger 和 ScaleDownTrigger。This project is an Azure Functions app with two timer triggers- "ScaleUpTrigger" and "ScaleDownTrigger". 触发器运行一个 PowerShell 脚本,该脚本根据每个触发器的 resources.json 文件中的定义在每个资源上设置吞吐量。The triggers run a PowerShell script that sets the throughput on each resource as defined in the resources.json file in each trigger. ScaleUpTrigger 配置为在 UTC 上午 8 点运行,ScaleDownTrigger 配置为在 UTC 下午 6 点运行,可以在每个触发器的 function.json 文件内轻松更新这些时间。The ScaleUpTrigger is configured to run at 8 AM UTC and the ScaleDownTrigger is configured to run at 6 PM UTC and these times can be easily updated within the function.json file for each trigger.

你可以在本地克隆此项目,对其进行修改,以指定要增加和减少的 Azure Cosmos DB 资源以及要运行的计划。You can clone this project locally, modify it to specify the Azure Cosmos DB resources to scale up and down and the schedule to run. 稍后,你可以将其部署到 Azure 订阅中,并使用托管服务标识以及基于角色的访问控制 (RBAC) 权限对其进行保护(使用“Azure Cosmos DB 操作员”角色),以在 Azure Cosmos 帐户上设置吞吐量。Later you can deploy it in an Azure subscription and secure it using managed service identity with Role-based Access Control (RBAC) permissions with the "Azure Cosmos DB operator" role to set throughput on your Azure Cosmos accounts.

后续步骤Next Steps