使用 Azure Resource Manager 模板部署 Azure Cosmos DB 和 Azure 应用服务 Web 应用Deploy Azure Cosmos DB and Azure App Service Web Apps using an Azure Resource Manager Template

本教程说明如何使用 Azure Resource Manager 模板来部署和集成 Azure Cosmos DBAzure 应用服务、Web 应用以及示例 Web 应用程序。This tutorial shows you how to use an Azure Resource Manager template to deploy and integrate Azure Cosmos DB, an Azure App Service web app, and a sample web application.

使用 Azure Resource Manager 模板,可以轻松自动化 Azure 资源的部署和配置。Using Azure Resource Manager templates, you can easily automate the deployment and configuration of your Azure resources. 本教程演示如何部署 Web 应用程序,以及自动配置 Azure Cosmos DB 帐户的连接信息。This tutorial shows how to deploy a web application and automatically configure Azure Cosmos DB account connection information.

完成本教程后,你能够回答以下问题:After completing this tutorial, you will be able to answer the following questions:

  • 如何使用 Azure Resource Manager 模板来部署和集成 Azure Cosmos DB 帐户与 Azure 应用服务中的 Web 应用?How can I use an Azure Resource Manager template to deploy and integrate an Azure Cosmos DB account and a web app in Azure App Service?
  • 如何使用 Azure Resource Manager 模板来部署和集成 Azure Cosmos DB 帐户、应用服务 Web 应用中的 Web 应用以及 Webdeploy 应用程序?How can I use an Azure Resource Manager template to deploy and integrate an Azure Cosmos DB account, a web app in App Service Web Apps, and a Webdeploy application?

必备条件Prerequisites

提示

虽然本教程不会假设先前有使用 Azure 资源管理器模板或 JSON 的经验,但是,如果想修改引用的模板或部署选项,则需要有其中每个领域的知识。While this tutorial does not assume prior experience with Azure Resource Manager templates or JSON, should you wish to modify the referenced templates or deployment options, then knowledge of each of these areas is required.

在按照本教程中的说明操作之前,请确保已有 Azure 订阅。Before following the instructions in this tutorial, ensure that you have the an Azure subscription. Azure 是基于订阅的平台。Azure is a subscription-based platform. 有关获取订阅的详细信息,请参阅购买选项会员套餐试用For more information about obtaining a subscription, see Purchase Options, Member Offers, or Trial.

步骤 1:下载模板文件Step 1: Download the template files

让我们从下载本教程所需的模板文件开始。Let's start by downloading the template files that this tutorial requires.

  1. 创建 Azure Cosmos DB 帐户、Web 应用和部署演示应用程序示例模板下载到本地文件夹(例如 C:\Azure Cosmos DBTemplates)。Download the Create an Azure Cosmos DB account, Web Apps, and deploy a demo application sample template to a local folder (for example, C:\Azure Cosmos DBTemplates). 此模板会部署 Azure Cosmos DB 帐户、应用服务 Web 应用和 Web 应用程序。This template deploys an Azure Cosmos DB account, an App Service web app, and a web application. 它还会自动配置 Web 应用程序,以连接到 Azure Cosmos DB 帐户。It also automatically configures the web application to connect to the Azure Cosmos DB account.
  2. 创建 Azure Cosmos DB 帐户和 Web 应用示例模板下载到本地文件夹(例如 C:\Azure Cosmos DBTemplates)。Download the Create an Azure Cosmos DB account and Web Apps sample template to a local folder (for example, C:\Azure Cosmos DBTemplates). 此模板将部署 Azure Cosmos DB 帐户、应用服务 Web 应用,并修改站点的应用程序设置以便轻松地显示 Azure Cosmos DB 连接信息,但不包含 Web 应用程序。This template deploys an Azure Cosmos DB account, an App Service web app, and modifies the site's application settings to easily surface Azure Cosmos DB connection information, but does not include a web application.

步骤 2:部署 Azure Cosmos DB 帐户、应用服务 Web 应用和演示应用程序示例Step 2: Deploy the Azure Cosmos DB account, App Service web app, and demo application sample

现在让我们来部署第一个模板。Now let's deploy your first template.

提示

该模板不会验证在以下模板中输入的 Web 应用名称和 Azure Cosmos DB 帐户名称是否:a) 有效和 b) 可用。The template does not validate that the web app name and Azure Cosmos DB account name entered in the following template are a) valid and b) available. 强烈建议在提交部署之前,先确认你打算提供的名称的可用性。It is highly recommended that you verify the availability of the names you plan to supply prior to submitting the deployment.

  1. 登录到 Azure 门户,单击“新建”并搜索“模板部署”。Login to the Azure Portal, click New and search for "Template deployment". 模板部署 UI 的屏幕截图Screenshot of the template deployment UI

  2. 选择模板部署项目,然后单击“创建” 模板部署 UI 的屏幕截图Select the Template deployment item and click Create Screenshot of the template deployment UI

  3. 单击“编辑模板” ,粘贴 DocDBWebsiteTodo.json 模板文件的内容,并单击“保存” 。Click Edit template, paste the contents of the DocDBWebsiteTodo.json template file, and click Save.

    将 Location 的 allowedValues 属性更改为 ["China East","China East 2","China North","China North 2"]。Change the allowedValues properties of Location as ["China East","China East 2","China North","China North 2"].

    模板部署 UI 的屏幕截图

  4. 单击“编辑参数” ,为每个必需参数提供值,并单击“确定” 。Click Edit parameters, provide values for each of the mandatory parameters, and click OK. 参数如下:The parameters are as follows:

    1. SITENAME:指定应用服务 Web 应用名称,并用来构造用于访问 Web 应用的 URL(例如,如果指定“mydemodocdbwebapp”,则用于访问 Web 应用的 URL 为 mydemodocdbwebapp.chinacloudsites.cn)。SITENAME: Specifies the App Service web app name and is used to construct the URL that you use to access the web app (for example, if you specify "mydemodocdbwebapp", then the URL by which you access the web app is mydemodocdbwebapp.chinacloudsites.cn).

    2. HOSTINGPLANNAME:指定要创建的应用服务托管计划的名称。HOSTINGPLANNAME: Specifies the name of App Service hosting plan to create.

    3. LOCATION:指定要在其中创建 Azure Cosmos DB 和 Web 应用资源的 Azure 位置。LOCATION: Specifies the Azure location in which to create the Azure Cosmos DB and web app resources.

    4. DATABASEACCOUNTNAME:指定要创建的 Azure Cosmos DB 帐户的名称。DATABASEACCOUNTNAME: Specifies the name of the Azure Cosmos DB account to create.

      模板部署 UI 的屏幕截图

  5. 选择现有的资源组或提供名称以创建新的资源组,并选择资源组的位置。Choose an existing Resource group or provide a name to make a new resource group, and choose a location for the resource group.

    模板部署 UI 的屏幕截图

  6. 依次单击“查看法律条款” 、“购买” 和“创建” 以开始部署。Click Review legal terms, Purchase, and then click Create to begin the deployment. 选择“固定到仪表板”,让生成的部署轻松显示在 Azure 门户的主页上。Select Pin to dashboard so the resulting deployment is easily visible on your Azure portal home page. 模板部署 UI 的屏幕截图Screenshot of the template deployment UI

  7. 部署完成后,会打开“资源组”窗格。When the deployment finishes, the Resource group pane opens. “资源组”窗格的屏幕截图Screenshot of the resource group pane

  8. 若要使用应用程序,请导航到 Web 应用 URL(上述示例中的 URL 是 http://mydemodocdbwebapp.chinacloudsites.cn)。To use the application, navigate to the web app URL (in the example above, the URL would be http://mydemodocdbwebapp.chinacloudsites.cn). 会看到下列 Web 应用程序:You'll see the following web application:

    示例待办事项应用程序

  9. 继续在 Web 应用中创建几个任务,并返回到 Azure 门户中的“资源组”窗格。Go ahead and create a couple of tasks in the web app and then return to the Resource group pane in the Azure portal. 单击“资源”列表中的“Azure Cosmos DB 帐户”资源,并单击“数据资源管理器” 。Click the Azure Cosmos DB account resource in the Resources list and then click Data Explorer.

  10. 运行默认查询“SELECT * FROM c”,并检查结果。Run the default query, "SELECT * FROM c" and inspect the results. 请注意,查询已检索在上面的步骤 7 中创建的待办事项的 JSON 表示形式。Notice that the query has retrieved the JSON representation of the todo items you created in step 7 above. 任意尝试查询;例如,尝试运行 SELECT * FROM c WHERE c.isComplete = true,以返回所有标记为完成的待办事项。Feel free to experiment with queries; for example, try running SELECT * FROM c WHERE c.isComplete = true to return all todo items that have been marked as complete.

  11. 随意浏览 Azure Cosmos DB 门户体验,或修改示例待办事项应用程序。Feel free to explore the Azure Cosmos DB portal experience or modify the sample Todo application. 准备好时,让我们来部署另一个模板。When you're ready, let's deploy another template.

步骤 3:部署文档帐户和 Web 应用示例Step 3: Deploy the Document account and web app sample

现在让我们来部署第二个模板。Now let's deploy your second template. 此模板可用于演示如何将帐户终结点和主密钥等 Azure Cosmos DB 连接信息插入 Web 应用,作为应用程序设置或自定义连接字符串。This template is useful to show how you can inject Azure Cosmos DB connection information such as account endpoint and master key into a web app as application settings or as a custom connection string. 例如,你或许有想要使用 Azure Cosmos DB 帐户部署的 Web 应用程序,以及在部署期间自动填充的连接信息。For example, perhaps you have your own web application that you would like to deploy with an Azure Cosmos DB account and have the connection information automatically populated during deployment.

提示

该模板不会验证下面输入的 Web 应用名称和 Azure Cosmos DB 帐户名称是否:a) 有效以及 b) 可用。The template does not validate that the web app name and Azure Cosmos DB account name entered below are a) valid and b) available. 强烈建议在提交部署之前,先确认你打算提供的名称的可用性。It is highly recommended that you verify the availability of the names you plan to supply prior to submitting the deployment.

  1. Azure 门户中,单击“新建”并搜索“模板部署”。In the Azure Portal, click New and search for "Template deployment". 模板部署 UI 的屏幕截图Screenshot of the template deployment UI

  2. 选择模板部署项目,然后单击“创建” 模板部署 UI 的屏幕截图Select the Template deployment item and click Create Screenshot of the template deployment UI

  3. 单击“编辑模板” ,粘贴 DocDBWebSite.json 模板文件的内容,并单击“保存” 。Click Edit template, paste the contents of the DocDBWebSite.json template file, and click Save.

    将 Location 的 allowedValues 属性更改为 ["China East","China East 2","China North","China North 2"]。Change the allowedValues properties of Location as ["China East","China East 2","China North","China North 2"].

    模板部署 UI 的屏幕截图

  4. 单击“编辑参数” ,为每个必需参数提供值,并单击“确定” 。Click Edit parameters, provide values for each of the mandatory parameters, and click OK. 参数如下:The parameters are as follows:

    1. SITENAME:指定应用服务 Web 应用名称,该名称用来构造用于访问 Web 应用的 URL(例如,如果指定“mydemodocdbwebapp”,则用于访问 Web 应用的 URL 是 mydemodocdbwebapp.chinacloudsites.cn)。SITENAME: Specifies the App Service web app name and is used to construct the URL that you will use to access the web app (for example, if you specify "mydemodocdbwebapp", then the URL by which you access the web app is mydemodocdbwebapp.chinacloudsites.cn).

    2. HOSTINGPLANNAME:指定要创建的应用服务托管计划的名称。HOSTINGPLANNAME: Specifies the name of App Service hosting plan to create.

    3. LOCATION:指定要在其中创建 Azure Cosmos DB 和 Web 应用资源的 Azure 位置。LOCATION: Specifies the Azure location in which to create the Azure Cosmos DB and web app resources.

    4. DATABASEACCOUNTNAME:指定要创建的 Azure Cosmos DB 帐户的名称。DATABASEACCOUNTNAME: Specifies the name of the Azure Cosmos DB account to create.

      模板部署 UI 的屏幕截图

  5. 选择现有的资源组或提供名称以创建新的资源组,并选择资源组的位置。Choose an existing Resource group or provide a name to make a new resource group, and choose a location for the resource group.

    模板部署 UI 的屏幕截图

  6. 依次单击“查看法律条款” 、“购买” 和“创建” 以开始部署。Click Review legal terms, Purchase, and then click Create to begin the deployment. 选择“固定到仪表板”,让生成的部署轻松显示在 Azure 门户的主页上。Select Pin to dashboard so the resulting deployment is easily visible on your Azure portal home page. 模板部署 UI 的屏幕截图Screenshot of the template deployment UI

  7. 部署完成后,会打开“资源组”窗格。When the deployment finishes, the Resource group pane opens. “资源组”窗格的屏幕截图Screenshot of the resource group pane

  8. 单击“资源”列表中的“Web 应用”资源,然后单击“应用程序设置” 资源组的屏幕截图Click the Web App resource in the Resources list and then click Application settings Screenshot of the resource group

  9. 注意出现的 Azure Cosmos DB 终结点和每个 Azure Cosmos DB 主密钥的应用程序设置。Note how there are application settings present for the Azure Cosmos DB endpoint and each of the Azure Cosmos DB master keys.

    应用程序设置的屏幕截图

  10. 继续随意浏览 Azure 门户,或按照其中一个 Azure Cosmos DB 示例来创建自己的 Azure Cosmos DB 应用程序。Feel free to continue exploring the Azure Portal, or follow one of our Azure Cosmos DB samples to create your own Azure Cosmos DB application.

后续步骤Next steps

祝贺!Congratulations! 已使用 Azure Resource Manager 模板部署了 Azure Cosmos DB、应用服务 Web 应用以及示例 Web 应用程序。You've deployed Azure Cosmos DB, App Service web app and a sample web application using Azure Resource Manager templates.

  • 若要了解有关 Azure Cosmos DB 的详细信息,请单击此处To learn more about Azure Cosmos DB, click here.
  • 若要了解有关 Azure 应用服务 Web 应用的详细信息,请单击此处To learn more about Azure App Service Web apps, click here.
  • 若要了解有关 Azure Resource Manager 模板的详细信息,请单击此处To learn more about Azure Resource Manager templates, click here.

发生的更改What's changed