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

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

在 Azure 中创建函数应用时,必须为应用选择托管计划。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. (要求启用Always on 。)(Requires that Always on is enabled.)

托管计划支持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 GAGA GAGA
LinuxLinux GAGA GAGA 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.

若要详细了解在消耗计划中运行时如何估算成本,请参阅了解消耗计划成本To learn more about how to estimate costs when running in a Consumption plan, see Understanding Consumption plan costs.

高级计划Premium plan

使用高级计划时,会根据传入事件的数目,添加和删除 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
  • 高级实例大小(一个核心、两个核心和四个核心实例)Premium instance sizes (one core, two core, and four core instances)
  • 更可预测的定价More predictable pricing
  • 针对具有多个 function app 的计划的高密度应用分配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 and memory used across needed and pre-warmed instances. 每个计划至少必须有一个实例处于热状态。At least one instance must be warm at all times per plan. 这意味着,每个活动计划的每月最低开销,而不考虑执行次数。This means that there is a minimum monthly cost per active plan, regardless of the number of executions. 请记住,高级计划中的所有 function app 共享准备好和活动实例。Keep in mind that all function apps in a Premium plan share pre-warmed and active instances.

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

  • 函数应用持续或几乎持续运行。Your function apps run continuously, or nearly continuously.
  • 您的小型执行数量很大,并且在消耗计划中的执行费用较高,但费用较低。You have a high number of small executions and have a high execution bill but low GB second bill in the consumption plan.
  • 你需要比消耗计划提供的更多的 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.

对高级计划运行 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

函数应用的超时持续时间由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
消耗Consumption 3.x3.x 55 1010
应用服务App Service 1.x1.x 无限制Unlimited 无限制Unlimited
应用服务App Service 2.x2.x 3030 无限制Unlimited
应用服务App Service 3.x3.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 relies 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.

用于存储应用程序数据的函数应用使用的相同存储帐户也可由触发器和绑定使用。The same storage account used by your function app can also be used by your triggers and bindings to store your application data. 但是,对于存储密集型操作,应使用单独的存储帐户。However, for storage-intensive operations, you should use a separate storage account.

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

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

在消耗和高级计划中,Azure 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. 消耗计划中托管的每个函数实例限制为 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.

运行时缩放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. 当函数应用内没有运行任何函数时,实例的数量最终将放大为零。The number of instances is eventually scaled in 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

下表指示在各种宿主计划中运行时应用于 function app 的限制: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
最大实例数Max instances 200200 100100 10-2010-20
默认超时持续时间(分钟)Default time out duration (min) 55 3030 302302
最大超时持续时间(分钟)Max time out duration (min) 1010 6060 不受限制3unbounded3
最大出站连接数(每个实例)Max outbound connections (per instance) 600 个处于活动状态(总共 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
每个实例的 ACUACU 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 apps per plan 100100 100100 不受限制5unbounded5
应用服务计划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 包含无限制的 SNI SSL 连接unbounded SNI SSL connection included 包含无限制的 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.