管理函数应用Manage your function app

在 Azure Functions 中,Function App 提供各个函数的执行上下文。In Azure Functions, a function app provides the execution context for your individual functions. Function App 行为适用于由给定 Function App 托管的所有函数。Function app behaviors apply to all functions hosted by a given function app. 函数应用中的所有函数必须使用同一语言All functions in a function app must be of the same language.

函数应用中的各个函数一起部署并一起缩放。Individual functions in a function app are deployed together and are scaled together. 同一函数应用中的所有函数在函数应用缩放时共享每个实例的资源。All functions in the same function app share resources, per instance, as the function app scales.

将为每个函数应用单独定义连接字符串、环境变量以及其他应用程序设置。Connection strings, environment variables, and other application settings are defined separately for each function app. 必须在函数应用之间共享的任何数据都应该以外部方式存储在持久存储中。Any data that must be shared between function apps should be stored externally in a persisted store.

在 Azure 门户中开始Get started in the Azure portal

  1. 要开始,请转到 Azure 门户,并使用 Azure 帐户登录。To begin, go to the Azure portal and sign in to your Azure account. 在门户顶端的搜索栏中,输入函数应用的名称,并从列表中将其选中。In the search bar at the top of the portal, enter the name of your function app and select it from the list.

  2. 在左窗格的“配置”下,选择“配置” 。Under Settings in the left pane, select Configuration.

    Azure 门户中的函数应用概述

可以从概述页导航到管理函数应用所需的所有内容,特别是 应用程序设置平台功能You can navigate to everything you need to manage your function app from the overview page, in particular the Application settings and Platform features.

使用应用程序设置Work with application settings

可以通过 Azure 门户以及 Azure CLIAzure PowerShell 管理应用程序设置。Application settings can be managed from the Azure portal and by using the Azure CLI and Azure PowerShell. 还可以通过 Visual Studio CodeVisual Studio 管理应用程序设置。You can also manage application settings from Visual Studio Code and from Visual Studio.

这些设置是加密存储的。These settings are stored encrypted. 若要了解详细信息,请参阅应用程序设置安全To learn more, see Application settings security.

若要查找应用程序设置,请参阅在 Azure 门户中开始操作To find the application settings, see Get started in the Azure portal.

“应用程序设置”选项卡维护函数应用使用的设置。The Application settings tab maintains settings that are used by your function app. 你必须选择“显示值”才能在门户中查看值。You must select Show values to see the values in the portal. 若要在门户中添加设置,请选择“新建应用程序设置”并添加新的键值对。To add a setting in the portal, select New application setting and add the new key-value pair.

Azure 门户中的函数应用设置。

使用应用程序设置Use application settings

还可以在代码中将函数应用设置值读取为环境变量。The function app settings values can also be read in your code as environment variables. 有关详细信息,请参阅以下特定于语言的参考主题的“环境变量”部分:For more information, see the Environment variables section of these language-specific reference topics:

在本地开发函数应用时,必须将这些值的本地副本保留在 local.settings.json 项目文件中。When you develop a function app locally, you must maintain local copies of these values in the local.settings.json project file. 若要了解详细信息,请参阅本地设置文件To learn more, see Local settings file.

托管计划类型Hosting plan type

创建函数应用时,你还需要创建托管计划,应用将在其中运行。When you create a function app, you also create a hosting plan in which the app runs. 一个计划可以有一个或多个函数应用。A plan can have one or more function apps. 函数的功能、缩放和定价取决于计划的类型。The functionality, scaling, and pricing of your functions depend on the type of plan. 若要了解详细信息,请参阅 Azure Functions 托管选项To learn more, see Azure Functions hosting options.

可以从 Azure 门户中或通过使用 Azure CLI 或 Azure PowerShell API 确定函数应用所使用的计划类型。You can determine the type of plan being used by your function app from the Azure portal, or by using the Azure CLI or Azure PowerShell APIs.

以下值指示计划类型:The following values indicate the plan type:

计划类型Plan type 门户Portal Azure CLI/PowerShellAzure CLI/PowerShell
消耗Consumption 消耗Consumption Dynamic
高级Premium ElasticPremiumElasticPremium ElasticPremium
专用(应用服务)Dedicated (App Service) 各种Various 各种Various

要确定你的函数应用所使用的计划的类型,请在 Azure 门户中查看该函数应用的“概述”选项卡中的“应用服务计划” 。To determine the type of plan used by your function app, see App Service plan in the Overview tab for the function app in the Azure portal. 若要查看定价层,请选择“应用服务计划”的名称,然后从左侧窗格中选择“属性” 。To see the pricing tier, select the name of the App Service Plan, and then select Properties from the left pane.

在门户中查看缩放计划

计划迁移Plan migration

你可以使用 Azure CLI 命令在 Windows 上的消耗计划和高级计划之间迁移函数应用。You can use Azure CLI commands to migrate a function app between a Consumption plan and a Premium plan on Windows. 具体的命令取决于迁移方向。The specific commands depend on the direction of the migration. 当前不支持直接迁移到专用(应用服务)计划。Direct migration to a Dedicated (App Service) plan isn't currently supported.

Linux 不支持此迁移。This migration isn't supported on Linux.

从消耗计划到高级计划Consumption to Premium

可以使用以下过程从 Windows 上的消耗计划迁移到高级计划:Use the following procedure to migrate from a Consumption plan to a Premium plan on Windows:

  1. 运行以下命令,在与现有函数应用相同的区域和资源组中创建一个新的应用服务计划(弹性高级)。Run the following command to create a new App Service plan (Elastic Premium) in the same region and resource group as your existing function app.

    az functionapp plan create --name <NEW_PREMIUM_PLAN_NAME> --resource-group <MY_RESOURCE_GROUP> --location <REGION> --sku EP1
    
  2. 运行以下命令,将现有函数应用迁移到新的高级计划Run the following command to migrate the existing function app to the new Premium plan

    az functionapp update --name <MY_APP_NAME> --resource-group <MY_RESOURCE_GROUP> --plan <NEW_PREMIUM_PLAN>
    
  3. 如果不再需要以前的消耗函数应用计划,请在确认已成功迁移到新的计划后,删除原始的函数应用计划。If you no longer need your previous Consumption function app plan, delete your original function app plan after confirming you have successfully migrated to the new one. 运行以下命令来获取资源组中所有消耗计划的列表。Run the following command to get a list of all Consumption plans in your resource group.

    az functionapp plan list --resource-group <MY_RESOURCE_GROUP> --query "[?sku.family=='Y'].{PlanName:name,Sites:numberOfSites}" -o table
    

    你可以放心地删除没有站点的计划,这是你从其进行迁移的计划。You can safely delete the plan with zero sites, which is the one you migrated from.

  4. 运行以下命令来删除从其进行迁移的消耗计划。Run the following command to delete the Consumption plan you migrated from.

    az functionapp plan delete --name <CONSUMPTION_PLAN_NAME> --resource-group <MY_RESOURCE_GROUP>
    

从高级计划到消耗计划Premium to Consumption

可以使用以下过程从 Windows 上的高级计划迁移到消耗计划:Use the following procedure to migrate from a Premium plan to a Consumption plan on Windows:

  1. 运行以下命令,在与现有函数应用相同的区域和资源组中创建一个新的函数应用(消耗)。Run the following command to create a new function app (Consumption) in the same region and resource group as your existing function app. 此命令还会创建一个将在其中运行函数应用的新消耗计划。This command also creates a new Consumption plan in which the function app runs.

    az functionapp create --resource-group <MY_RESOURCE_GROUP> --name <NEW_CONSUMPTION_APP_NAME> --consumption-plan-location <REGION> --runtime dotnet --functions-version 3 --storage-account <STORAGE_NAME>
    
  2. 运行以下命令,将现有函数应用迁移到新的消耗计划。Run the following command to migrate the existing function app to the new Consumption plan.

    az functionapp update --name <MY_APP_NAME> --resource-group <MY_RESOURCE_GROUP> --plan <NEW_CONSUMPTION_PLAN>
    
  3. 删除你在步骤 1 中创建的函数应用,因为你只需要为运行现有函数应用而创建的计划。Delete the function app you created in step 1, since you only need the plan that was created to run the existing function app.

    az functionapp delete --name <NEW_CONSUMPTION_APP_NAME> --resource-group <MY_RESOURCE_GROUP>
    
  4. 如果不再需要以前的高级函数应用计划,请在确认已成功迁移到新计划后,删除原始函数应用计划。If you no longer need your previous Premium function app plan, delete your original function app plan after confirming you have successfully migrated to the new one. 请注意,如果未删除该计划,则仍按高级计划收费。Please note that if the plan is not deleted, you will still be charged for the Premium plan. 运行以下命令来获取资源组中所有高级计划的列表。Run the following command to get a list of all Premium plans in your resource group.

    az functionapp plan list --resource-group <MY_RESOURCE_GROUP> --query "[?sku.family=='EP'].{PlanName:name,Sites:numberOfSites}" -o table
    
  5. 运行以下命令来删除从其进行迁移的高级计划。Run the following command to delete the Premium plan you migrated from.

    az functionapp plan delete --name <PREMIUM_PLAN> --resource-group <MY_RESOURCE_GROUP>
    

平台功能Platform features

函数应用在 Azure 应用服务平台中运行,并由该平台维护。Function apps run in, and are maintained by, the Azure App Service platform. 在这种情况下,Function App 有权访问 Azure 核心 Web 托管平台的大多数功能。As such, your function apps have access to most of the features of Azure's core web hosting platform. 可在左侧窗格中访问可用于函数应用的应用服务平台中的许多功能。The left pane is where you access the many features of the App Service platform that you can use in your function apps.

备注

Function App 运行于消耗托管计划中时,并非所有应用服务功能均可用。Not all App Service features are available when a function app runs on the Consumption hosting plan.

本文的其余部分侧重于 Azure 门户中以下可用于 Functions 的应用服务功能:The rest of this article focuses on the following App Service features in the Azure portal that are useful for Functions:

若要深入了解如何使用应用服务设置,请参阅配置 Azure 应用服务设置For more information about how to work with App Service settings, see Configure Azure App Service Settings.

应用服务编辑器App Service editor

应用服务编辑器

应用服务编辑器是一种高级的门户内编辑器,可用于修改诸如 JSON 配置文件和代码文件等内容。The App Service editor is an advanced in-portal editor that you can use to modify JSON configuration files and code files alike. 选择此选项会启动单独的浏览器选项卡和基本编辑器。Choosing this option launches a separate browser tab with a basic editor. 借此,可与 Git 存储库集成、运行和调试代码,并可修改 Function App 设置。This enables you to integrate with the Git repository, run and debug code, and modify function app settings. 同内置的函数编辑器相比,此编辑器为 Functions 提供了增强的开发环境。This editor provides an enhanced development environment for your functions compared with the built-in function editor.

建议你考虑在本地计算机上开发函数。We recommend that you consider developing your functions on your local computer. 在本地开发项目并将其发布到 Azure 时,项目文件在门户中处于只读状态。When you develop locally and publish to Azure, your project files are read-only in the portal. 若要了解详细信息,请参阅在本地对 Azure Functions 进行编码和测试To learn more, see Code and test Azure Functions locally.

控制台Console

Function App 控制台

要从命令行与 Function App 交互时,门户内控制台就是非常合适的开发人员工具。The in-portal console is an ideal developer tool when you prefer to interact with your function app from the command line. 常见命令包括创建和导航目录与文件,以及执行批处理文件和脚本。Common commands include directory and file creation and navigation, as well as executing batch files and scripts.

在本地进行开发时,建议使用 Azure Functions Core ToolsAzure CLIWhen developing locally, we recommend using the Azure Functions Core Tools and the Azure CLI.

高级工具 (Kudu)Advanced tools (Kudu)

配置 Kudu

应用服务的高级工具(也称为 Kudu)提供对 Function App 高级管理功能的访问。The advanced tools for App Service (also known as Kudu) provide access to advanced administrative features of your function app. 从 Kudu 中,可以管理系统信息、应用设置、环境变量、站点扩展、HTTP 头和服务器变量。From Kudu, you manage system information, app settings, environment variables, site extensions, HTTP headers, and server variables. 也可以通过浏览到 Function App 的 SCM 终结点(如 https://<myfunctionapp>.scm.chinacloudsites.cn/),启动 KuduYou can also launch Kudu by browsing to the SCM endpoint for your function app, like https://<myfunctionapp>.scm.chinacloudsites.cn/

部署中心Deployment Center

使用源代码管理解决方案来开发和维护函数代码时,可以使用部署中心通过源代码管理进行生成和部署。When you use a source control solution to develop and maintain your functions code, Deployment Center lets you build and deploy from source control. 进行更新时,会生成项目并将其部署到 Azure。Your project is built and deployed to Azure when you make updates. 有关详细信息,请参阅 Azure Functions 中的部署技术For more information, see Deployment technologies in Azure Functions.

跨域资源共享Cross-origin resource sharing

为了防止在客户端执行恶意代码,新式的浏览器会阻止 Web 应用程序向正在单独的域中运行的资源发送请求。To prevent malicious code execution on the client, modern browsers block requests from web applications to resources running in a separate domain. 跨域资源共享 (CORS) 允许 Access-Control-Allow-Origin 标头声明允许哪些域调用函数应用上的终结点。Cross-origin resource sharing (CORS) lets an Access-Control-Allow-Origin header declare which origins are allowed to call endpoints on your function app.

门户Portal

配置函数应用的“允许的域”列表时,Access-Control-Allow-Origin 标头会自动添加到函数应用中 HTTP 终结点发出的所有响应。When you configure the Allowed origins list for your function app, the Access-Control-Allow-Origin header is automatically added to all responses from HTTP endpoints in your function app.

配置函数应用的 CORS 列表

使用星号 (*) 时,会忽略所有其他的域。When the wildcard (*) is used, all other domains are ignored.

使用 az functionapp cors add 命令将域添加到“允许的域”列表。Use the az functionapp cors add command to add a domain to the allowed origins list. 以下示例添加 contoso.com 域:The following example adds the contoso.com domain:

az functionapp cors add --name <FUNCTION_APP_NAME> \
--resource-group <RESOURCE_GROUP_NAME> \
--allowed-origins https://contoso.com

使用 az functionapp cors show 命令列出目前允许的域。Use the az functionapp cors show command to list the current allowed origins.

身份验证Authentication

配置 Function App 的身份验证

函数使用 HTTP 触发器时,可以要求首先对调用进行身份验证。When functions use an HTTP trigger, you can require calls to first be authenticated. 应用服务支持 Azure Active Directory 身份验证和使用社交提供程序登录。App Service supports Azure Active Directory authentication and sign-in with social providers. 有关配置特定身份验证提供程序的详细信息,请参阅 Azure 应用服务身份验证概述For details on configuring specific authentication providers, see Azure App Service authentication overview.

后续步骤Next steps