在 Azure API 管理中将 Azure Function App 作为 API 导入Import an Azure Function App as an API in Azure API Management

Azure API 管理支持将 Azure Function App 作为新 API 导入或将其追加到现有 API。Azure API Management supports importing Azure Function Apps as new APIs or appending them to existing APIs. 此过程会在 Azure Function App 中自动生成一个主机密钥,然后将其分配给 Azure API 管理中的命名值。The process automatically generates a host key in the Azure Function App, which is then assigned to a named value in Azure API Management.

本文详细介绍如何在 Azure API 管理中将 Azure Function App 作为 API 导入。This article walks through importing an Azure Function App as an API in Azure API Management. 此外还介绍测试过程。It also describes the testing process.

可以了解如何执行以下操作:You will learn how to:

  • 将 Azure Function App 作为 API 导入Import an Azure Function App as an API
  • 将 Azure Function App 追加到 APIAppend an Azure Function App to an API
  • 查看新的 Azure Function App 主机密钥和 Azure API 管理命名值View the new Azure Function App host key and Azure API Management named value
  • 在 Azure 门户中测试 APITest the API in the Azure portal
  • 在开发人员门户中测试 APITest the API in the developer portal

先决条件Prerequisites

转到你的 API 管理实例Go to your API Management instance

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 选择“所有服务”。Select All services.

  3. 在搜索框中输入 apiIn the search box, enter api.

  4. 在搜索结果中,选择“API 管理服务”。In the search results, select API Management services.

  5. 选择自己的 API 管理服务实例。Select your API Management service instance.

提示

若要将 API 管理添加到 Azure 门户中的收藏夹,请选择星形。To add API Management to your favorites in the Azure portal, select the star.

API 管理图标The API Management icon ((API 管理图标)现在显示在门户的左侧菜单中。) now appears in the left menu in the portal.

将 Azure Function App 作为新 API 导入Import an Azure Function App as a new API

执行以下步骤,通过 Azure Function App 创建新的 API。Follow the steps below to create a new API from an Azure Function App.

  1. 在 Azure 门户中导航到 API 管理服务,然后从菜单中选择“API”。Navigate to your API Management service in the Azure portal and select APIs from the menu.

  2. 在“添加新 API”列表中,选择“Function App”。 In the Add a new API list, select Function App.

    从 Function App 添加

  3. 单击“浏览”,选择要导入的 Functions。Click Browse to select Functions for import.

    从 Function App 添加

  4. 单击“Function App”部分,从可用 Function App 的列表中进行选择。Click on the Function App section to choose from the list of available Function Apps.

    从 Function App 添加

  5. 找到要从其导入 Functions 的 Function App,然后单击它并按“选择”。Find the Function App you want to import Functions from, click on it and press Select.

    从 Function App 添加

  6. 选择要导入的 Functions,然后单击“选择”。Select the Functions you would like to import and click Select.

    从 Function App 添加

    备注

    可以只导入基于 HTTP 触发器的 Functions,并将授权级别设置设为“匿名”或“函数”。 You can import only Functions that are based off HTTP trigger and have the authorization level setting set to Anonymous or Function.

  7. 切换到“完整”视图并将“产品”分配到新 API 。Switch to the Full view and assign Product to your new API. 如果需要,请在创建过程中指定其他字段,也可以稍后转到“设置”选项卡来配置这些字段。在导入和发布第一个 API 教程中对这些设置进行了说明。If needed, specify other fields during creation or configure them later by going to the Settings tab. The settings are explained in the Import and publish your first API tutorial.

  8. 单击创建Click Create.

将 Azure Function App 追加到现有 APIAppend Azure Function App to an existing API

执行以下步骤,将 Azure Function App 追加到现有 API。Follow the steps below to append Azure Function App to an existing API.

  1. Azure API 管理服务实例中,从左侧的菜单选择“API”。In your Azure API Management service instance, select APIs from the menu on the left.

  2. 选择要将 Azure Function App 导入到其中的 API。Choose an API you want to import an Azure Function App to. 单击“...”,并从上下文菜单中选择“导入”。 Click ... and select Import from the context menu.

    从 Function App 追加

  3. 单击“Function App”磁贴。Click on the Function App tile.

    从 Function App 追加

  4. 在弹出窗口中,单击“浏览”。In the pop-up window, click Browse.

    从 Function App 追加

  5. 单击“Function App”部分,从可用 Function App 的列表中进行选择。Click on the Function App section to choose from the list of available Function Apps.

    从 Function App 添加

  6. 找到要从其导入 Functions 的 Function App,然后单击它并按“选择”。Find the Function App you want to import Functions from, click on it and press Select.

    从 Function App 添加

  7. 选择要导入的 Functions,然后单击“选择”。Select the Functions you would like to import and click Select.

    从 Function App 添加

  8. 单击“导入”。Click Import.

    从 Function App 追加

授权Authorization

导入 Azure Function App 会自动生成:Import of an Azure Function App automatically generates:

  • 主机密钥,该密钥位于 Function App 中,其名称为 apim-{你的 Azure API 管理服务实例名称};Host key inside the Function App with the name apim-{your Azure API Management service instance name},
  • 命名值,该值位于 Azure API 管理实例中,其名称为 {你的 Azure Function App 实例名称}-key,其中包含创建的主机密钥。Named value inside the Azure API Management instance with the name {your Azure Function App instance name}-key, which contains the created host key.

对于 2019 年 4 月 4 日以后创建的 API,主机密钥将通过 HTTP 请求从 API 管理传递到标头中的 Function App。For APIs created after April 4th 2019, the host key is passed in HTTP requests from API Management to the Function App in a header. 较旧的 API 将主机密钥作为查询参数传递。Older APIs pass the host key as a query parameter. 可以通过对与 Function App 关联的后端实体进行 PATCH Backend REST API 调用来更改此行为。This behavior may be changed through the PATCH Backend REST API call on the Backend entity associated with the Function App.

警告

删除或更改 Azure Function App 主机密钥或 Azure API 管理命名值的值会导致服务之间的通信中断。Removing or changing value of either the Azure Function App host key or Azure API Management named value will break the communication between the services. 这些值不自动同步。The values do not sync automatically.

如需轮换主机密钥,请确保 Azure API 管理中的命名值也得到修改。If you need to rotate the host key, make sure the named value in Azure API Management is also modified.

访问 Azure Function App 主机密钥Access Azure Function App host key

  1. 导航到 Azure Function App 实例。Navigate to your Azure Function App instance.

  2. 从概览中选择“Function App 设置”。Select Function App settings from the overview.

    从 Function App 添加

  3. 密钥位于“主机密钥”部分。The key is located in the Host Keys section.

    从 Function App 添加

访问 Azure API 管理中的命名值Access the named value in Azure API Management

导航到 Azure API 管理实例,然后在左侧菜单中选择“命名值”。Navigate to your Azure API Management instance and select Named values from the menu on the left. Azure Function App 密钥存储在该处。The Azure Function App key is stored there.

从 Function App 添加

在 Azure 门户中测试新的 APITest the new API in the Azure portal

可直接从 Azure 门户调用操作。You can call operations directly from the Azure portal. 使用 Azure 门户可以方便地查看和测试 API 的操作。Using the Azure portal is a convenient way to view and test the operations of an API.

  1. 选择在上一部分创建的 API。Select the API that you created in the preceding section.

  2. 选择“测试”选项卡。Select the Test tab.

  3. 选择操作。Select an operation.

    该页将显示查询参数的字段和标头的字段。The page displays fields for query parameters and fields for the headers. 其中一个标头是 Ocp-Apim-Subscription-Key,适用于和此 API 关联的产品订阅密钥。One of the headers is Ocp-Apim-Subscription-Key, for the subscription key of the product that is associated with this API. 如果创建了 API 管理实例,那么你已是管理员,因此会自动填充该密钥。If you created the API Management instance, you are an administrator already, so the key is filled in automatically.

  4. 选择“发送”。Select Send.

    后端以“200 正常”和某些数据做出响应。The back end responds with 200 OK and some data.

后续步骤Next steps