您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

Azure Functions 的缩放和托管Azure Functions scale and hosting

在 Azure 中创建 function app 时,必须为应用选择托管计划。When you create a function app in Azure, you must choose a hosting plan for your app. 适用于 Azure Functions 提供了三个托管计划:消耗计划高级计划,和应用服务计划There are three hosting plans available for Azure Functions: Consumption plan, Premium plan, and App Service plan.

你选择的托管计划决定了以下行为:The hosting plan you choose dictates the following behaviors:

  • 如何缩放函数应用。How your function app is scaled.
  • 可用于每个函数应用实例的资源。The resources available to each function app instance.
  • 支持高级功能,如 VNET 连接。Support for advanced features, such as VNET connectivity.

运行你的代码时,消耗和高级计划自动添加的计算能力。Both Consumption and Premium plans automatically add compute power when your code is running. 应用在需要处理负载时会扩展,在代码停止运行时会缩减。Your app is scaled out when needed to handle load, and scaled down when code stops running. 对于消耗计划中,你还无需为空闲 Vm 付费或提前保留容量。For the Consumption plan, you also don't have to pay for idle VMs or reserve capacity in advance.

高级计划提供其他功能,例如高级计算实例、 使实例保存无限期,热和 VNet 连接。Premium plan provides additional features, such as premium compute instances, the ability to keep instances warm indefinitely, and VNet connectivity.

应用服务计划,可充分利用你管理的专用基础结构。App Service plan allows you to take advantage of dedicated infrastructure, which you manage. 函数应用不会扩展基于事件,这意味着就永远不会减少到零刻度。Your function app doesn't scale based on events, which means is never scales down to zero. (要求始终打开已启用。)(Requires that Always on is enabled.)

备注

您可以通过更改计划属性的函数应用资源消耗和高级计划之间进行切换。You can switch between Consumption and Premium plans by changing the plan property of the function app resource.

托管计划支持Hosting plan support

支持的功能划分为以下两个类别:Feature support falls into the following two categories:

  • 正式版 (GA)) : 完全支持并获得批准在生产环境中使用。Generally available (GA): fully supported and approved for production use.
  • 预览: 尚未完全支持并获得批准在生产环境中使用。Preview: not yet fully supported and approved for production use.

在 Windows 或 Linux 上运行时下, 表指示对于三个托管计划,支持的当前级别:The following table indicates the current level of support for the three hosting plans, when running on either Windows or Linux:

消耗量计划Consumption plan 高级计划Premium plan 专用的计划Dedicated plan
WindowsWindows GAGA 预览preview GAGA
LinuxLinux 预览preview 不适用N/A GAGA

消耗量计划Consumption plan

使用消耗计划时,会根据传入事件数自动添加和删除 Azure Functions 主机实例。When you're using the Consumption plan, instances of the Azure Functions host are dynamically added and removed based on the number of incoming events. 这个无服务器计划会自动缩放,仅在函数运行时,才会产生计算资源费用。This serverless plan scales automatically, and you're charged for compute resources only when your functions are running. 在消费计划中,函数执行在可配置的时间段后超时。On a Consumption plan, a function execution times out after a configurable period of time.

账单将基于执行数量、执行时间和所用内存。Billing is based on number of executions, execution time, and memory used. 账单是基于函数应用内的所有函数聚合而生成的。Billing is aggregated across all functions within a function app. 有关详细信息,请参阅 Azure Functions 定价页For more information, see the Azure Functions pricing page.

消耗计划是默认的宿主计划,它提供了以下优势:The Consumption plan is the default hosting plan and offers the following benefits:

  • 仅当函数正在运行时才产生费用Pay only when your functions are running
  • 可自动扩展,即使是在负载较高期间Scale out automatically, even during periods of high load

在同一区域中的函数应用可以分配给同一消耗计划。Function apps in the same region can be assigned to the same Consumption plan. 没有缺点或对具有相同的消耗计划中运行的多个应用程序的影响。There's no downside or impact to having multiple apps running in the same Consumption plan. 将多个应用分配到相同的消耗计划没有任何影响恢复能力、 可伸缩性,或每个应用程序的可靠性。Assigning multiple apps to the same consumption plan has no impact on resilience, scalability, or reliability of each app.

高级计划 (预览版)Premium plan (preview)

当您使用的高级计划时,Azure Functions 主机的实例添加和删除基于消耗计划一样的传入事件的数目。When you're using the Premium plan, instances of the Azure Functions host are added and removed based on the number of incoming events just like the Consumption plan. 高级计划支持以下功能:Premium plan supports the following features:

  • 永久热实例,以避免任何冷启动Perpetually warm instances to avoid any cold start
  • VNet 连接VNet connectivity
  • 不受限制的执行持续时间Unlimited execution duration
  • 高级实例大小 (一项核心、 两个核心和 4 个核心实例)Premium instance sizes (one core, two core, and four core instances)
  • 更可预测的定价More predictable pricing
  • 高密度应用于包含多个函数应用计划的分配High-density app allocation for plans with multiple function apps

如何配置这些选项的信息可在Azure Functions 高级计划文档Information on how you can configure these options can be found in the Azure Functions premium plan document.

而不是每个执行和内存消耗的计费,计费的高级计划基于的核心秒、 执行时间和所需和保留实例使用的内存数量。Instead of billing per execution and memory consumed, billing for the Premium plan is based on the number of core seconds, execution time, and memory used across needed and reserved instances. 至少一个实例必须为热在所有时间。At least one instance must be warm at all times. 这意味着没有固定的每月费用,每个活动的计划,而不考虑执行的次数。This means that there is a fixed monthly cost per active plan, regardless of the number of executions.

Azure Functions 高级计划在以下情况下的,请考虑:Consider the Azure Functions premium plan in the following situations:

  • 函数应用持续或几乎持续运行。Your function apps run continuously, or nearly continuously.
  • 您需要更多的 CPU 或内存选项比消耗计划通过提供的功能。You need more CPU or memory options than what is provided by the Consumption plan.
  • 你的代码需要运行时间超过允许的最长执行时间基于消耗计划。Your code needs to run longer than the maximum execution time allowed on the Consumption plan.
  • 需要仅在高级计划,例如 VNET VPN 连接中提供的功能。You require features that are only available on a Premium plan, such as VNET/VPN connectivity.

备注

高级计划预览版目前仅支持 Azure Functions 在 Windows 上。The premium plan preview currently only supports Azure Functions on Windows.

当在高级版计划上运行 JavaScript 函数,应选择具有较少 Vcpu 的实例。When running JavaScript functions on a Premium plan, you should choose an instance that has fewer vCPUs. 有关详细信息,请参阅选择单核心高级计划For more information, see the Choose single-core Premium plans.

专用 (应用服务) 计划Dedicated (App Service) plan

函数应用也可以像其他应用服务应用(基本、标准、高级和隔离 SKU)一样在相同的专用 VM 上运行。Your function apps can also run on the same dedicated VMs as other App Service apps (Basic, Standard, Premium, and Isolated SKUs).

应用服务计划在以下情况下的,请考虑:Consider an App Service plan in the following situations:

  • 具有已运行其他应用服务实例的、未充分利用的现成 VM。You have existing, underutilized VMs that are already running other App Service instances.
  • 你想要提供自定义映像在其上运行你的函数。You want to provide a custom image on which to run your functions.

应用服务计划中函数应用的费用与其他应用服务资源(例如 Web 应用)的费用相同。You pay the same for function apps in an App Service Plan as you would for other App Service resources, like web apps. 如需详细了解如何使用应用服务计划,请参阅 Azure 应用服务计划深入概述For details about how the App Service plan works, see the Azure App Service plans in-depth overview.

借助应用服务计划,您可以通过手动扩大添加更多 VM 实例。With an App Service plan, you can manually scale out by adding more VM instances. 此外可以启用自动缩放。You can also enable autoscale. 有关详细信息,请参阅手动或自动缩放实例计数For more information, see Scale instance count manually or automatically. 还可以通过选择不同的应用服务计划来进行增加。You can also scale up by choosing a different App Service plan. 有关详细信息,请参阅增加 Azure 中的应用For more information, see Scale up an app in Azure.

在应用服务计划上运行 JavaScript 函数时,应选择具有较少 vCPU 的计划。When running JavaScript functions on an App Service plan, you should choose a plan that has fewer vCPUs. 有关详细信息,请参阅选择单核应用服务计划For more information, see Choose single-core App Service plans.

Always OnAlways On

如果在应用服务计划上运行,应启用 AlwaysOn 设置,使函数应用能正常运行 。If you run on an App Service plan, you should enable the Always on setting so that your function app runs correctly. 在应用服务计划中,如果函数运行时处于不活动状态,几分钟后就会进入空闲状态,因此只有 HTTP 触发器才能“唤醒”函数。On an App Service plan, the functions runtime goes idle after a few minutes of inactivity, so only HTTP triggers will "wake up" your functions. 只能对应用服务计划使用始终可用。Always on is available only on an App Service plan. 在消耗计划中,平台会自动激活函数应用。On a Consumption plan, the platform activates function apps automatically.

函数应用超时持续时间Function app timeout duration

函数应用的超时持续时间可通过 host.json 项目文件中的 functionTimeout 属性定义。The timeout duration of a function app is defined by the functionTimeout property in the host.json project file. 下表显示两种计划和两种运行时版本的默认值和最大值(以分钟为单位):The following table shows the default and maximum values in minutes for both plans and in both runtime versions:

计划Plan 运行时版本Runtime Version 默认Default 最大值Maximum
消耗Consumption 1.x1.x 55 1010
消耗Consumption 2.x2.x 55 1010
应用服务App Service 1.x1.x 无限制Unlimited 无限制Unlimited
应用服务App Service 2.x2.x 3030 无限制Unlimited

备注

不管函数应用超时设置如何,230 秒是 HTTP 触发的函数在响应请求时需要的最长时间。Regardless of the function app timeout setting, 230 seconds is the maximum amount of time that an HTTP triggered function can take to respond to a request. 这起因于 Azure 负载均衡器的默认空闲超时This is because of the default idle timeout of Azure Load Balancer. 对于处理时间较长的情况,考虑使用 Durable Functions 异步模式延迟实际工作并返回即时响应For longer processing times, consider using the Durable Functions async pattern or defer the actual work and return an immediate response.

即使启用了 AlwaysOn,各函数的执行超时也由 host.json 项目文件中的 functionTimeout 设置控制。Even with Always On enabled, the execution timeout for individual functions is controlled by the functionTimeout setting in the host.json project file.

确定现有应用程序的宿主计划Determine the hosting plan of an existing application

要确定你的函数应用所使用的托管计划,请在 Azure 门户中参阅函数应用的“概览”选项卡中的“应用服务计划/定价层” 。To determine the hosting plan used by your function app, see App Service plan / pricing tier in the Overview tab for the function app in the Azure portal. 对于应用服务计划,还指明了定价层。For App Service plans, the pricing tier is also indicated.

在门户中查看缩放计划

还可以使用 Azure CLI 来确定计划,如下所示:You can also use the Azure CLI to determine the plan, as follows:

appServicePlanId=$(az functionapp show --name <my_function_app_name> --resource-group <my_resource_group> --query appServicePlanId --output tsv)
az appservice plan list --query "[?id=='$appServicePlanId'].sku.tier" --output tsv

此命令的输出为 dynamic 时,函数应用采用消耗量计划。When the output from this command is dynamic, your function app is in the Consumption plan. 此命令的输出为 ElasticPremium 时,函数应用采用高级计划。When the output from this command is ElasticPremium, your function app is in the Premium plan. 所有其他值表示不同的应用服务计划层。All other values indicate different tiers of an App Service plan.

存储帐户要求Storage account requirements

在任何计划中,函数应用需要一个支持 Azure Blob、队列、文件和表存储的常规 Azure 存储帐户。On any plan, a function app requires a general Azure Storage account, which supports Azure Blob, Queue, Files, and Table storage. 这是因为 Functions 依赖于 Azure 存储来执行管理触发器和记录函数执行等操作,但某些存储帐户不支持队列和表。This is because Functions rely on Azure Storage for operations such as managing triggers and logging function executions, but some storage accounts do not support queues and tables. 这些帐户包括仅限 blob 的存储帐户(包括高级存储)和使用区域冗余存储空间复制的常规用途存储帐户,已在创建函数应用时将从现有的“存储帐户”选项中过滤掉 。These accounts, which include blob-only storage accounts (including premium storage) and general-purpose storage accounts with zone-redundant storage replication, are filtered-out from your existing Storage Account selections when you create a function app.

若要了解有关存储帐户类型的详细信息,请参阅 Azure 存储服务简介To learn more about storage account types, see Introducing the Azure Storage services.

消耗计划和高级计划的工作原理How the consumption and premium plans work

在消耗和高级版计划中,Azure Functions 基础结构通过添加其他基于触发函数的事件数的 Functions 主机实例来缩放 CPU 和内存资源。In the consumption and premium plans, the Azure Functions infrastructure scales CPU and memory resources by adding additional instances of the Functions host, based on the number of events that its functions are triggered on. Functions 主机在消耗计划中的每个实例被限制为 1.5 GB 内存和 CPU。Each instance of the Functions host in the consumption plan is limited to 1.5 GB of memory and one CPU. 主机实例是整个函数应用,这意味着函数应用中的所有函数共享某个实例中的资源并同时缩放。An instance of the host is the entire function app, meaning all functions within a function app share resource within an instance and scale at the same time. 共享同一消耗计划的函数应用单独缩放。Function apps that share the same consumption plan are scaled independently. 在高级版计划中,你的计划大小将确定可用内存和 CPU 的所有应用在该实例上的该计划中。In the premium plan, your plan size will determine the available memory and CPU for all apps in that plan on that instance.

函数代码文件存储在函数主要存储帐户中的 Azure 文件共享上。Function code files are stored on Azure Files shares on the function's main storage account. 删除函数应用的主存储帐户时,函数代码文件将被删除并且无法恢复。When you delete the main storage account of the function app, the function code files are deleted and cannot be recovered.

备注

在消耗量计划中使用 blob 触发器时,处理新的 blob 可能会出现长达 10 分钟的延迟。When you're using a blob trigger on a Consumption plan, there can be up to a 10-minute delay in processing new blobs. 函数应用处于空闲时会发生这种延迟。This delay occurs when a function app has gone idle. 函数应用运行后,就会立即处理 Blob。After the function app is running, blobs are processed immediately. 若要避免此冷启动延迟,请使用高级计划,或使用事件网格触发器To avoid this cold-start delay, use the Premium plan, or use the Event Grid trigger. 有关详细信息,请参阅 blob 触发器绑定参考文章For more information, see the blob trigger binding reference article.

运行时缩放Runtime scaling

Azure Functions 使用名为“缩放控制器”的组件来监视事件率以及确定是要扩大或缩小。 Azure Functions uses a component called the scale controller to monitor the rate of events and determine whether to scale out or scale in. 缩放控制器针对每种触发器类型使用试探法。The scale controller uses heuristics for each trigger type. 例如,使用 Azure 队列存储触发器时,它会根据队列长度和最旧队列消息的期限进行缩放。For example, when you're using an Azure Queue storage trigger, it scales based on the queue length and the age of the oldest queue message.

Azure Functions 单位是缩放的 function app。The unit of scale for Azure Functions is the function app. 横向扩展函数应用时,将分配额外的资源来运行 Azure Functions 主机的多个实例。When the function app is scaled out, additional resources are allocated to run multiple instances of the Azure Functions host. 相反,计算需求下降时,扩展控制器将删除函数主机实例。Conversely, as compute demand is reduced, the scale controller removes function host instances. 实例数最终会缩减为零,此时 Function App 中没有任何函数运行。The number of instances is eventually scaled down to zero when no functions are running within a function app.

用于监视事件和创建实例的扩展控制器

了解缩放行为Understanding scaling behaviors

缩放可根据多种因素而异,可根据选定的触发器和语言以不同的方式缩放。Scaling can vary on a number of factors, and scale differently based on the trigger and language selected. 有一些复杂的缩放行为,需要注意的:There are a few intricacies of scaling behaviors to be aware of:

  • 单个函数应用最多只能纵向扩展到 200 个实例。A single function app only scales up to a maximum of 200 instances. 不过,单个实例每次可以处理多个消息或请求,因此,对并发执行数没有规定的限制。A single instance may process more than one message or request at a time though, so there isn't a set limit on number of concurrent executions.
  • 对于 HTTP 触发器,将最多每隔 1 秒分配一次新实例。For HTTP triggers, new instances will only be allocated at most once every 1 second.
  • 对于非 HTTP 触发器,将最多每隔 30 秒分配一次新实例。For non-HTTP triggers, new instances will only be allocated at most once every 30 seconds.

不同触发器还可能有不同的缩放限制,如下所述:Different triggers may also have different scaling limits as well as documented below:

可缩放应用的最佳做法和模式Best practices and patterns for scalable apps

函数应用的许多方面会影响其缩放,包括主机配置、运行时占用空间和资源效率。There are many aspects of a function app that will impact how well it will scale, including host configuration, runtime footprint, and resource efficiency. 有关详细信息,请查看性能注意事项一文的“可扩展”部分For more information, see the scalability section of the performance considerations article. 还要注意随着函数应用的扩展,连接是如何实施的。You should also be aware of how connections behave as your function app scales. 有关详细信息,请参阅如何在 Azure Functions 中管理连接For more information, see How to manage connections in Azure Functions.

计费模式Billing model

对不同的计划的计费详细信息中所述上Azure Functions 定价页Billing for the different plans is described in detail on the Azure Functions pricing page. 使用量在 Function App 级别聚合,只会统计函数代码的执行时间。Usage is aggregated at the function app level and counts only the time that function code is executed. 以下是计费单位:The following are units for billing:

  • 以千兆字节/秒 (GB-s) 计量的资源消耗量 。Resource consumption in gigabyte-seconds (GB-s). 根据内存大小和函数应用中所有函数的执行时间组合计算得出。Computed as a combination of memory size and execution time for all functions within a function app.
  • 执行Executions. 每次为响应事件触发而执行函数时记为一次。Counted each time a function is executed in response to an event trigger.

可以找到有用的查询和如何了解消耗帐单信息计费常见问题Useful queries and information on how to understand your consumption bill can be found on the billing FAQ.

服务限制Service limits

下表指示了在各种托管计划中运行时应用于函数应用的限制:The following table indicates the limits that apply to function apps when running in the various hosting plans:

ResourceResource 消耗计划Consumption plan 高级计划Premium plan 应用服务计划1App Service plan1
横向扩展Scale out 事件驱动Event driven 事件驱动Event driven 手动/自动缩放Manual/autoscale
默认值超时持续时间(分钟)Default time out duration (min) 55 3030 302302
最大超时持续时间(分钟)Max time out duration (min) 1010 unboundedunbounded 不受限制3unbounded3
最大出站连接数 (每个实例)Max outbound connections (per instance) 600 active (1200年总计)600 active (1200 total) unboundedunbounded unboundedunbounded
最大请求大小 (MB)4Max request size (MB)4 100100 100100 100100
最大查询字符串的长度4Max query string length4 40964096 40964096 40964096
请求的最大 URL 长度4Max request URL length4 81928192 81928192 81928192
ACU每个实例ACU per instance 100100 210-840210-840 100-840100-840
最大内存 (GB 每个实例)Max memory (GB per instance) 1.51.5 3.5-143.5-14 1.75-141.75-14
每个计划的 function appFunction apps per plan 100100 100100 unbounded5unbounded5
应用服务计划App Service plans 每 100 个区域100 per region 每个资源组 100 个100 per resource group 每个资源组 100 个100 per resource group
存储6Storage6 1 GB1 GB 250 GB250 GB 50-1000 GB50-1000 GB
每个应用的自定义域数Custom domains per app 50075007 500500 500500
自定义域 SSL 支持Custom domain SSL support 不受支持,通配符证书为 *。 默认情况下可用的 azurewebsites.netNot supported, wildcard certificate for *.azurewebsites.net available by default 不受限制的 SNI SSL 和包含 1 个 IP SSL 连接unbounded SNI SSL and 1 IP SSL connections included 不受限制的 SNI SSL 和包含 1 个 IP SSL 连接unbounded SNI SSL and 1 IP SSL connections included

1有关各种应用服务计划选项的特定限制,请参阅应用服务计划限制1 For specific limits for the various App Service plan options, see the App Service plan limits.
2默认情况下,Functions 1.x 运行时在应用服务计划中的超时时间为无限。2 By default, the timeout for the Functions 1.x runtime in an App Service plan is unbounded.
3需要应用服务计划设置为Always On3 Requires the App Service plan be set to Always On. 以标准支付费率Pay at standard rates.
4这些限制主机中设置4 These limits are set in the host.
5可以托管的函数应用的实际数目取决于应用的活动、 计算机实例和相应的资源利用率的大小。5 The actual number of function apps that you can host depends on the activity of the apps, the size of the machine instances, and the corresponding resource utilization. 6存储限制是内容总大小中临时存储的所有应用相同的应用服务计划中。6 The storage limit is the total content size in temporary storage across all apps in the same App Service plan. 消耗计划使用 Azure 文件进行临时存储。Consumption plan uses Azure Files for temporary storage.
7函数应用中的托管时消耗计划,支持仅 CNAME 选项。7 When your function app is hosted in a Consumption plan, only the CNAME option is supported. 为函数应用中高级版计划应用服务计划,可以将映射自定义域,使用 CNAME 或 A 记录。For function apps in a Premium plan or an App Service plan, you can map a custom domain using either a CNAME or an A record.