透過可預測方式在 Azure 中佈建和部署微服務Provision and deploy microservices predictably in Azure

本教學課程示範如何在 Azure App Service 中將包含微服務的應用程式佈建和部署為單一單位,並且使用 JSON 資源群組範本和 PowerShell 指令碼的可預測方式。This tutorial shows how to provision and deploy an application composed of microservices in Azure App Service as a single unit and in a predictable manner using JSON resource group templates and PowerShell scripting.

佈建和部署包含高低耦合微服務的高級別應用程式時,重複性和可預測性是成功的重要關鍵。When provisioning and deploying high-scale applications that are composed of highly decoupled microservices, repeatability and predictability are crucial to success. Azure App Service 可讓您建立微服務,其中包括 Web 應用程式、行動後端和 API 應用程式。Azure App Service enables you to create microservices that include web apps, mobile back ends, and API apps. Azure Resource Manager 可讓您將所有微服務當成一個單位來進行管理,以及管理資源相依性 (例如資料庫和原始檔控制設定)。Azure Resource Manager enables you to manage all the microservices as a unit, together with resource dependencies such as database and source control settings. 現在,您也可以使用 JSON 範本和簡單 PowerShell 指令碼來部署這類應用程式。Now, you can also deploy such an application using JSON templates and simple PowerShell scripting.

將執行的作業What you will do

在教學課程中,您將部署的應用程式包括:In the tutorial, you will deploy an application that includes:

  • 兩個 App Service 應用程式 (即兩個微服務)Two App Service apps (i.e. two microservices)
  • 後端 SQL DatabaseA backend SQL Database
  • 應用程式設定、連接字串和原始檔控制App settings, connection strings, and source control
  • Application insights、警示、自動調整設定Application insights, alerts, autoscaling settings

將使用的工具Tools you will use

在本教學課程中,您將使用下列工具。In this tutorial, you will use the following tools. 這不是工具的完整討論,因此將著重在端對端案例,並且只對每個工具進行簡短介紹,以及您可以在哪裡找到更多資訊。Since it’s not comprehensive discussion on tools, I’m going to stick to the end-to-end scenario and just give you a brief intro to each, and where you can find more information on it.

Azure 資源管理員範本 (JSON)Azure Resource Manager templates (JSON)

例如,每次在 Azure App Service 中建立應用程式時,Azure Resource Manager 都會使用 JSON 範本來建立具有元件資源的整個資源群組。Every time you create an app in Azure App Service, for example, Azure Resource Manager uses a JSON template to create the entire resource group with the component resources. 來自 Azure Marketplace 的複雜範本,可包括資料庫、儲存體帳戶、App Service 方案、應用程式本身、警示規則、應用程式設定、自動調整設定等等,而且您可以透過 PowerShell 使用所有這類範本。A complex template from the Azure Marketplace can include the database, storage accounts, the App Service plan, the app itself, alert rules, app settings, autoscale settings, and more, and all these templates are available to you through PowerShell. 如需 Azure 資源管理員範本的詳細資訊,請參閱 編寫 Azure 資源管理員範本For more information on the Azure Resource Manager templates, see Authoring Azure Resource Manager Templates

Azure SDK 2.6 for Visual StudioAzure SDK 2.6 for Visual Studio

最新 SDK 已增強 JSON 編輯器中的資源管理員範本支援。The newest SDK contains improvements to the Resource Manager template support in the JSON editor. 您可以使用此項快速從頭開始建立資源群組範本,或開啟現有 JSON 範本 (例如已下載的資源庫範本) 進行修改、填入參數檔案,甚至直接從 Azure 資源群組方案部署資源群組。You can use this to quickly create a resource group template from scratch or open an existing JSON template (such as a downloaded gallery template) for modification, populate the parameters file, and even deploy the resource group directly from an Azure Resource Group solution.

如需詳細資訊,請參閱 Azure SDK 2.6 for Visual StudioFor more information, see Azure SDK 2.6 for Visual Studio.

Azure PowerShell 0.8.0 或更新版本Azure PowerShell 0.8.0 or later

自 0.8.0 版開始,Azure PowerShell 安裝除了 Azure 模組之外還包括 Azure 資源管理員模組。Beginning in version 0.8.0, the Azure PowerShell installation includes the Azure Resource Manager module in addition to the Azure module. 這個新模組可讓您編寫資源群組部署的指令碼。This new module enables you to script the deployment of resource groups.

如需詳細資訊,請參閱搭配 使用 Azure PowerShell Azure Resource ManagerFor more information, see Using Azure PowerShell with Azure Resource Manager

Azure 資源總管Azure Resource Explorer

預覽工具可讓您瀏覽您的訂用帳戶與個別資源中的所有資源群組的 JSON 定義。This preview tool enables you to explore the JSON definitions of all the resource groups in your subscription and the individual resources. 在此工具中,您可以編輯資源的 JSON 定義、刪除整個階層的資源,以及建立新的資源。In the tool, you can edit the JSON definitions of a resource, delete an entire hierarchy of resources, and create new resources. 此工具中提供的資訊對於範本撰寫非常有説明,因為它會顯示您需要針對特定資源類型、正確值等設定哪些屬性。您甚至可以在 Azure 入口網站中建立資源群組,然後在 explorer 工具中檢查其 JSON 定義,以協助您範本化資源群組。The information readily available in this tool is very helpful for template authoring because it shows you what properties you need to set for a particular type of resource, the correct values, etc. You can even create your resource group in the Azure Portal, then inspect its JSON definitions in the explorer tool to help you templatize the resource group.

部署至 Azure 按鈕Deploy to Azure button

如果您使用 GitHub 進行原始檔控制,則可以將 部署至 Azure 按鈕 放入 README.MD,而此檔案會啟用 Azure 的轉換金鑰部署 UI。If you use GitHub for source control, you can put a Deploy to Azure button into your README.MD, which enables a turn-key deployment UI to Azure. 雖然您可以針對任何簡單應用程式執行這項作業,但是您可以將 azuredeploy.json 檔案放在存放庫根目錄中,以擴充此項來啟用部署整個資源群組。While you can do this for any simple app, you can extend this to enable deploying an entire resource group by putting an azuredeploy.json file in the repository root. 此 JSON 檔案包含資源群組範本,將供 [部署至 Azure] 按鈕用來建立資源群組。This JSON file, which contains the resource group template, will be used by the Deploy to Azure button to create the resource group. 如需範例,請參閱將在本教學課程中使用的 ToDoApp 範例。For an example, see the ToDoApp sample, which you will use in this tutorial.

取得範例資源群組範本Get the sample resource group template

因此,現在讓我們著手進行。So now let’s get right to it.

  1. 巡覽至 ToDoApp App Service 範例。Navigate to the ToDoApp App Service sample.

  2. 在 readme.md 中,按一下 [ 部署至 Azure]。In readme.md, click Deploy to Azure.

  3. 您會進入 deploy-to-azure 網站,並要求您輸入部署參數。You’re taken to the deploy-to-azure site and asked to input deployment parameters. 請注意,大部分的欄位都會填入儲存機制名稱以及一些隨機字串。Notice that most of the fields are populated with the repository name and some random strings for you. 您可以視需要變更所有欄位,但唯一必須輸入的項目是 SQL Server 管理登入和密碼,然後按 [ 下一步]。You can change all the fields if you want, but the only things you have to enter are the SQL Server administrative login and the password, then click Next.

    顯示部署至 azure 網站上的輸入部署參數。

  4. 接著,按一下 [ 部署 ] 啟動部署程序。Next, click Deploy to start the deployment process. 程序執行並完成之後,請按一下 http://todoappXXXX.azurewebsites.net 連結來瀏覽已部署的應用程式。Once the process runs to completion, click the http://todoappXXXX.azurewebsites.net link to browse the deployed application.

    顯示應用程式的部署流程。

    在您第一次瀏覽至 UI 時,UI 會比較慢,因為應用程式剛剛啟動,但確信它是功能完整的應用程式。The UI would be a little slow when you first browse to it because the apps are just starting up, but convince yourself that it’s a fully-functional application.

  5. 回到 [部署] 頁面,並按一下 [管理] 連結,以在 Azure 入口網站中查看新的應用程式。Back in the Deploy page, click the Manage link to see the new application in the Azure Portal.

  6. 在 [ Essentials ] 下拉式清單中,按一下 [資源群組] 連結。In the Essentials dropdown, click the resource group link. 另請注意應用程式已連線至 [外部專案] 下的 GitHub 存放庫。Note also that the app is already connected to the GitHub repository under External Project.

    在 [基本功能] 下拉式區段中顯示 [資源群組] 連結。

  7. 在資源群組刀鋒視窗中,請注意,資源群組中已經有兩個應用程式和一個 SQL Database。In the resource group blade, note that there are already two apps and one SQL Database in the resource group.

    顯示資源群組中可用的資源。

剛剛您在短短幾分鐘內看到的所有項目都是完整部署的雙微服務應用程式,以及所有元件、相依性、設定、資料庫和連續發行 (由 Azure 資源管理員中的自動化協調流程所設定)。Everything that you just saw in a few short minutes is a fully deployed two-microservice application, with all the components, dependencies, settings, database, and continuous publishing, set up by an automated orchestration in Azure Resource Manager. 這項作業是透過兩件事完成:All this was done by two things:

  • [部署至 Azure] 按鈕The Deploy to Azure button
  • 儲存機制根目錄中的 azuredeploy.jsonazuredeploy.json in the repo root

您可以部署這個相同的應用程式數十次、數百次或數千次,而且每次的組態都完全相同。You can deploy this same application tens, hundreds, or thousands of times and have the exact same configuration every time. 這種方法的重複性和可預測性可讓您輕鬆並自信地部署高延展性應用程式。The repeatability and the predictability of this approach enables you to deploy high-scale applications with ease and confidence.

檢查 (或編輯) AZUREDEPLOY.JSONExamine (or edit) AZUREDEPLOY.JSON

現在,讓我們看看已如何設定 GitHub 儲存機制。Now let’s look at how the GitHub repository was set up. 您將使用 Azure .NET SDK 中的 JSON 編輯器,因此,如果尚未安裝 Azure .NET SDK 2.6,請立即安裝。You will be using the JSON editor in the Azure .NET SDK, so if you haven’t already installed Azure .NET SDK 2.6, do it now.

  1. 使用慣用的 git 工具複製 ToDoApp 儲存機制。Clone the ToDoApp repository using your favorite git tool. 在下面的螢幕擷取畫面中,我將在 Visual Studio 2013 的 Team Explorer 中中進行這項作業。In the screenshot below, I’m doing this in the Team Explorer in Visual Studio 2013.

    說明如何使用 git 工具來複製 ToDoApp 存放庫。

  2. 在 Visual Studio 中,從儲存機制根目錄中開啟 azuredeploy.json。From the repository root, open azuredeploy.json in Visual Studio. 如果您看不到 [JSON 大綱] 窗格,則需要安裝 Azure .NET SDK。If you don’t see the JSON Outline pane, you need to install Azure .NET SDK.

    顯示 Visual Studio 中的 [JSON 大綱] 窗格。

我不打算詳述 JSON 格式,但是 其他資源 小節具有了解資源群組範本語言的連結。I’m not going to describe every detail of the JSON format, but the More Resources section has links for learning the resource group template language. 在這裡,我只會說明可協助您開始建立專屬自訂範本以進行應用程式部署的有趣功能。Here, I’m just going to show you the interesting features that can help you get started in making your own custom template for app deployment.

參數Parameters

查看 parameters 區段,了解大部分這些參數都是 [ 部署至 Azure ] 按鈕提示您輸入的參數。Take a look at the parameters section to see that most of these parameters are what the Deploy to Azure button prompts you to input. [ 部署至 Azure ] 按鈕後面的網站會使用 azuredeploy.json 中所定義的參數來填入輸入 UI。The site behind the Deploy to Azure button populates the input UI using the parameters defined in azuredeploy.json. 這些參數用於整個資源定義 (例如資源名稱、屬性值等)。These parameters are used throughout the resource definitions, such as resource names, property values, etc.

資源Resources

在 resources 節點中,您可以看到已定義 4 個最上層資源,包括 SQL Server 執行個體、App Service 方案和兩個應用程式。In the resources node, you can see that 4 top-level resources are defined, including a SQL Server instance, an App Service plan, and two apps.

App Service 方案App Service plan

讓我們從 JSON 中的簡單根層級資源開始。Let’s start with a simple root-level resource in the JSON. 在 [JSON 大綱] 中,按一下名為 [hostingPlanName] 的 App Service 方案反白顯示對應的 JSON 程式碼。In the JSON Outline, click the App Service plan named [hostingPlanName] to highlight the corresponding JSON code.

顯示 JSON 程式碼的 [hostingPlanName] 區段。

請注意, type 項目指定 App Service 方案 (很久以前稱為伺服器陣列) 的字串,並會使用 JSON 檔案中所定義的參數來填寫其他項目和屬性,而且此資源沒有任何巢狀資源。Note that the type element specifies the string for an App Service plan (it was called a server farm a long, long time ago), and other elements and properties are filled in using the parameters defined in the JSON file, and this resource doesn’t have any nested resources.

注意

也請注意,apiVersion 的值告訴 Azure 搭配使用哪一個版本的 REST API 與 JSON 資源定義,而且可能會影響資源在 {} 內的格式。Note also that the value of apiVersion tells Azure which version of the REST API to use the JSON resource definition with, and it can affect how the resource should be formatted inside the {}.

SQL ServerSQL Server

接下來,按一下 [JSON 大綱] 中名為 SQLServer 的 SQL Server 資源。Next, click on the SQL Server resource named SQLServer in the JSON Outline.

顯示 JSON 大綱中名稱為 SQLServer 的 SQL Server 資源。

請注意反白顯示 JSON 程式碼的下列資訊:Note the following about the highlighted JSON code:

  • 參數的使用確保透過讓所建立的資源彼此一致的方式來命名和設定所建立的資源。The use of parameters ensures that the created resources are named and configured in a way that makes them consistent with one another.

  • SQLServer 資源有兩個巢狀資源,其各有不同的 type 值。The SQLServer resource has two nested resources, each has a different value for type.

  • “resources”: […] 內巢狀資源 (其中已定義資料庫和防火牆規則) 的 dependsOn 項目指定根層級 SQLServer 資源的資源識別碼。The nested resources inside “resources”: […], where the database and the firewall rules are defined, have a dependsOn element that specifies the resource ID of the root-level SQLServer resource. 這會告訴 Azure 資源管理員:「建立此資源之前,那個其他資源必須已經存在;如果那個其他資源定義在範本中,則請先建立它」。This tells Azure Resource Manager, “before you create this resource, that other resource must already exist; and if that other resource is defined in the template, then create that one first”.

    注意

    如需如何使用 resourceId() 函式的詳細資訊,請參閱 Azure Resource Manager 範本函式For detailed information on how to use the resourceId() function, see Azure Resource Manager Template Functions.

  • dependsOn 項目的作用是 Azure 資源管理員可以知道可平行建立哪些資源,以及必須依序建立哪些資源。The effect of the dependsOn element is that Azure Resource Manager can know which resources can be created in parallel and which resources must be created sequentially.

App Service 應用程式App Service app

現在,讓我們繼續討論更為複雜的實際應用程式本身。Now, let’s move on to the actual apps themselves, which are more complicated. 按一下 [JSON 大綱] 中的 [variables(‘apiSiteName’)] 應用程式,以醒目提示其 JSON 程式碼。Click the [variables(‘apiSiteName’)] app in the JSON Outline to highlight its JSON code. 您會發現事情變得越來越有趣。You’ll notice that things are getting much more interesting. 因此,我將逐一討論所有功能:For this purpose, I’ll talk about the features one by one:

根資源Root resource

應用程式與兩個不同的資源相依。The app depends on two different resources. 這表示只有在建立 App Service 方案和 SQL Server 執行個體之後,Azure Resource Manager 才會建立應用程式。This means that Azure Resource Manager will create the app only after both the App Service plan and the SQL Server instance are created.

顯示 App Service 方案和 SQL Server 實例上的應用程式相依性。

應用程式設定App settings

應用程式設定也會定義為巢狀資源。The app settings are also defined as a nested resource.

顯示在 JSON 程式碼中定義為嵌套資源的應用程式設定。

config/appsettingsproperties 項目中,您會有兩個格式為 "<name>" : "<value>" 的應用程式設定。In the properties element for config/appsettings, you have two app settings in the format "<name>" : "<value>".

  • PROJECTKUDU 設定 ,告訴 Azure 部署在多專案 Visual Studio 方案中使用哪個專案。PROJECT is a KUDU setting that tells Azure deployment which project to use in a multi-project Visual Studio solution. 稍後會示範如何設定原始檔控制,但是,因為 ToDoApp 程式碼是在多專案 Visual Studio 方案中,所以我們需要這項設定。I will show you later how source control is configured, but since the ToDoApp code is in a multi-project Visual Studio solution, we need this setting.
  • clientUrl 只是應用程式程式碼所使用的應用程式設定。clientUrl is simply an app setting that the application code uses.
連接字串Connection strings

連接字串也會定義為巢狀資源。The connection strings are also defined as a nested resource.

顯示如何在 JSON 程式碼中將連接字串定義為嵌套資源。

<span data-ttu-id="012c3-215">在 config/connectionstringsproperties 項目中,每個連接字串也會定義為「名稱:值」配對,並且具有特定格式:&quot;<name>&quot; : {&quot;value&quot;: &quot;…&quot;, &quot;type&quot;: &quot;…&quot;}In the properties element for config/connectionstrings, each connection string is also defined as a name:value pair, with the specific format of &quot;<name>&quot; : {&quot;value&quot;: &quot;…&quot;, &quot;type&quot;: &quot;…&quot;}. 針對 type 項目,可能的值為 MySqlSQLServerSQLAzureCustomFor the type element, possible values are MySql, SQLServer, SQLAzure, and Custom.

提示

如需連接字串型別的限定清單,請在 Azure PowerShell 中執行下列命令:[Enum]::GetNames("Microsoft.WindowsAzure.Commands.Utilities.Websites.Services.WebEntities.DatabaseType")For a definitive list of the connection string types, run the following command in Azure PowerShell: [Enum]::GetNames("Microsoft.WindowsAzure.Commands.Utilities.Websites.Services.WebEntities.DatabaseType")

原始檔控制Source control

原始檔控制也會定義為巢狀資源。The source control settings are also defined as a nested resource. Azure 資源管理員使用這項資源來設定持續發佈 (請參閱後面 IsManualIntegration 上的警告),並且在處理 JSON 檔案期間開始自動部署應用程式程式碼。Azure Resource Manager uses this resource to configure continuous publishing (see caveat on IsManualIntegration later) and also to kick off the deployment of application code automatically during the processing of the JSON file.

顯示如何在 JSON 程式碼中將原始檔控制設定定義為嵌套資源。

RepoUrlbranch 應該相當具直覺式,而且應該指向 Git 儲存機制以及從發佈之分支的名稱。RepoUrl and branch should be pretty intuitive and should point to the Git repository and the name of the branch to publish from. 同樣地,這些是透過輸入參數所定義。Again, these are defined by input parameters.

請注意,在 dependsOn 項目中,除了應用程式資源本身之外,sourcecontrols/web 也與 config/appsettingsconfig/connectionstrings 相依。Note in the dependsOn element that, in addition to the app resource itself, sourcecontrols/web also depends on config/appsettings and config/connectionstrings. 原因是設定 sourcecontrols/web 之後,Azure 部署程序會自動嘗試部署、建置和啟動應用程式碼。This is because once sourcecontrols/web is configured, the Azure deployment process will automatically attempt to deploy, build, and start the application code. 因此,插入此相依性有助於確定在執行應用程式碼之前,應用程式可以存取所需的應用程式設定和連接字串。Therefore, inserting this dependency helps you make sure that the application has access to the required app settings and connection strings before the application code is run.

注意

也請注意,IsManualIntegration 設為 trueNote also that IsManualIntegration is set to true. 這個屬性是本教學課程中的必要屬性,因為您實際上未擁有 GitHub 存放庫,因此無法實際將從 ToDoApp (也就是將自動存放庫更新推送至 Azure) 設定持續發佈的權限授與 Azure。This property is necessary in this tutorial because you do not actually own the GitHub repository, and thus cannot actually grant permission to Azure to configure continuous publishing from ToDoApp (i.e. push automatic repository updates to Azure). 只有在之前已在 Azure 入口網站中設定過擁有者的 GitHub 認證時,才能使用所指定儲存機制的預設值 falseYou can use the default value false for the specified repository only if you have configured the owner’s GitHub credentials in the Azure portal before. 換句話說,如果您之前已使用使用者認證來設定 Azure 入口網站中任何應用程式的 GitHub 或 BitBucket 原始檔控制,則 Azure 會記住認證,並在未來從 GitHub 或 BitBucket 部署任何應用程式時使用它們。In other words, if you have set up source control to GitHub or BitBucket for any app in the Azure Portal previously, using your user credentials, then Azure will remember the credentials and use them whenever you deploy any app from GitHub or BitBucket in the future. 不過,如果您還沒有這麼做,則 Azure Resource Manager 嘗試設定應用程式的原始檔控制設定時,JSON 範本的部署會失敗。因為它無法使用存放庫擁有者的認證登入 GitHub 或 BitBucket。However, if you haven’t done this already, deployment of the JSON template will fail when Azure Resource Manager tries to configure the app’s source control settings because it cannot log into GitHub or BitBucket with the repository owner’s credentials.

比較具有已部署資源群組的 JSON 範本Compare the JSON template with deployed resource group

您可以在這裡瀏覽 Azure 入口網站中的所有應用程式刀鋒視窗,但還有另一個工具十分有用。Here, you can go through all the app’s blades in the Azure Portal, but there’s another tool that’s just as useful, if not more. 移至 Azure 資源總管 預覽工具,其可提供訂用帳戶中所有資源群組的 JSON 呈現,因為它們實際存在於 Azure 後端。Go to the Azure Resource Explorer preview tool, which gives you a JSON representation of all the resource groups in your subscriptions, as they actually exist in the Azure backend. 您也可以看到 Azure 中資源群組的 JSON 階層如何與用來建立它的範本檔案中的階層對應。You can also see how the resource group’s JSON hierarchy in Azure corresponds with the hierarchy in the template file that’s used to create it.

例如,移至 Azure 資源總管 工具並展開總管中的節點時,可以看到資源群組以及收集在其個別資源型別下方的根層級資源。For example, when I go to the Azure Resource Explorer tool and expand the nodes in the explorer, I can see the resource group and the root-level resources that are collected under their respective resource types.

在擴充的 Azure 資源瀏覽器工具中,查看資源群組和根層級的資源。

如果您向下切入至應用程式,則應該可以看到與下面螢幕擷取畫面類似的應用程式設定詳細資料:If you drill down to an app, you should be able to see app configuration details similar to the below screenshot:

向下切入以查看應用程式中的設定詳細資料。

同樣地,巢狀資源的階層應該與 JSON 範本檔案中的階層極為類似,而且您應該會看到 JSON 窗格中正確反映的應用程式設定、連接字串等。Again, the nested resources should have a hierarchy very similar to those in your JSON template file, and you should see the app settings, connection strings, etc., properly reflected in the JSON pane. 沒有這裡的設定可能表示 JSON 檔案發生問題,並可協助您疑難排解 JSON 範本檔案。The absence of settings here may indicate an issue with your JSON file and can help you troubleshoot your JSON template file.

自行部署資源群組範本Deploy the resource group template yourself

[ 部署至 Azure ] 按鈕不錯,但是,只有在已將 azuredeploy.json 推送至 GitHub 時,它才可讓您在 azuredeploy.json 中部署資源群組範本。The Deploy to Azure button is great, but it allows you to deploy the resource group template in azuredeploy.json only if you have already pushed azuredeploy.json to GitHub. Azure .NET SDK 也提供工具,讓您直接從本機電腦部署任何 JSON 範本檔案。The Azure .NET SDK also provides the tools for you to deploy any JSON template file directly from your local machine. 若要這樣做,請遵循下面的步驟:To do this, follow the steps below:

  1. 在 Visual Studio 中,按一下 [檔案] > [新增] > [專案] 。In Visual Studio, click File > New > Project.

  2. 按一下 [ Visual c # > Cloud > Azure 資源群組],然後按一下 [確定]Click Visual C# > Cloud > Azure Resource Group, then click OK.

    在 Azure .NET SDK 中,將新的專案建立為 Azure 資源群組。

  3. 在 [選取 Azure 範本] 中,選取 [空白範本],然後按一下 [確定]。In Select Azure Template, select Blank Template and click OK.

  4. 將 azuredeploy.json 拖曳至新專案的 [ 樣本 ] 資料夾。Drag azuredeploy.json into the Template folder of your new project.

    顯示將 azuredeploy.js檔案拖曳至專案的範本資料夾的結果。

  5. 從 [方案總管],開啟複製的 azuredeploy.json。From Solution Explorer, open the copied azuredeploy.json.

  6. 按一下 [ 加入資源],將一些標準 Application Insight 資源加入 JSON 檔案,但此作業僅供示範之用。Just for the sake of the demonstration, let’s add some standard Application Insight resources to our JSON file, by clicking Add Resource. 如果您只想部署 JSON 檔案,請跳至部署步驟。If you’re just interested in deploying the JSON file, skip to the deploy steps.

    顯示 [新增資源] 按鈕,您可以用來將標準的應用程式深入解析資源新增至 JSON 檔案。

  7. 選取 [Web Apps 的 Application Insights],並確定已選取現有 App Service 方案和應用程式,然後按一下 [新增]。Select Application Insights for Web Apps, then make sure an existing App Service plan and app is selected, and then click Add.

    顯示 Web Apps、名稱、App Service 方案和 Web 應用程式的 Application Insights 選擇。

    您現在可以看到數個新資源 (根據資源和其作用而定) 與 App Service 方案或應用程式相依。You’ll now be able to see several new resources that, depending on the resource and what it does, have dependencies on either the App Service plan or the app. 這些資源不是透過其現有定義所啟用,而您將變更這項行為。These resources are not enabled by their existing definition and you’re going to change that.

    查看相依于 App Service 方案或應用程式的新資源。

  8. 在 [JSON 大綱] 中,按一下 appInsights AutoScale 反白顯示其 JSON 程式碼。In the JSON Outline, click appInsights AutoScale to highlight its JSON code. 這是您 App Service 方案的調整設定。This is the scaling setting for your App Service plan.

  9. 在反白顯示的 JSON 程式碼中,找到 locationenabled 屬性,並如下所示設定它們。In the highlighted JSON code, locate the location and enabled properties and set them as shown below.

    顯示 appInsights 自動調整 JSON 程式碼中的位置和已啟用屬性,以及您應該將其設定為的值。

  10. 在 [JSON 大綱] 中,按一下 CPUHigh appInsights 反白顯示其 JSON 程式碼。In the JSON Outline, click CPUHigh appInsights to highlight its JSON code. 這是警示。This is an alert.

  11. 找到 locationisEnabled 屬性,並如下所示設定它們。Locate the location and isEnabled properties and set them as shown below. 請對其他三個警示 (紫色燈泡) 執行相同的動作。Do the same for the other three alerts (purple bulbs).

    顯示 CPUHigh appInsights JSON 程式碼中的位置和 isEnabled 屬性,以及您應該將其設定為的值。

  12. 您現在可以開始進行部署。You’re now ready to deploy. 以滑鼠右鍵按一下專案,然後選取 [部署 > 新的部署]。Right-click the project and select Deploy > New Deployment.

    說明如何部署新專案。

  13. 登入 Azure 帳戶 (如果您尚未這樣做)。Log into your Azure account if you haven’t already done so.

  14. 在您的訂用帳戶中選取現有的資源群組,或選取 azuredeploy.json,然後按一下 [編輯參數] 建立新的資源群組。Select an existing resource group in your subscription or create a new one, select azuredeploy.json, and then click Edit Parameters.

    顯示如何編輯 azuredeploy.json 檔案的參數。

    您現在可以透過好用的資料表編輯範本檔案中定義的所有參數。You’ll now be able to edit all the parameters defined in the template file in a nice table. 定義預設值的參數已經有其預設值,而定義允許值清單的參數則會顯示為下拉式清單。Parameters that define defaults will already have their default values, and parameters that define a list of allowed values will be shown as dropdowns.

    顯示將允許值清單定義為下拉式清單的參數。

  15. 填入所有空的參數,並在 repoUrl 中使用 GitHub repo address for ToDoAppFill in all the empty parameters, and use the GitHub repo address for ToDoApp in repoUrl. 然後按一下 [ 儲存]。Then, click Save.

    顯示檔案 azuredeploy.js的新填入參數。

    注意

    自動調整是 標準 層級或更高層級所提供的功能,而計劃層級警示是 基本 層級或更高層級所提供的功能,您需要將 sku 參數設為 標準進階,才能看到所有新的 App Insights 資源都已啟動。Autoscaling is a feature offered in Standard tier or higher, and plan-level alerts are features offered in Basic tier or higher, you’ll need to set the sku parameter to Standard or Premium in order to see all your new App Insights resources light up.

  16. 按一下 [ 部署]。Click Deploy. 如果您選取 [儲存密碼],則會將密碼 以純文字形式 儲存至參數檔。If you selected Save passwords, the password will be saved in the parameter file in plain text. 否則,系統會在部署過程要求您輸入資料庫密碼。Otherwise, you’ll be asked to input the database password during the deployment process.

這樣就大功告成了!That’s it! 現在,您只需要移至 Azure 入口網站Azure 資源總管工具,即可查看已新增 JSON 已部署應用程式中的新警示和自動調整設定。Now you just need to go to the Azure Portal and the Azure Resource Explorer tool to see the new alerts and autoscale settings added to your JSON deployed application.

您在本節中的步驟主要是完成下列作業:Your steps in this section mainly accomplished the following:

  1. 準備範本檔案Prepared the template file
  2. 建立要與範本檔案一起使用的參數檔案Created a parameter file to go with the template file
  3. 部署具有參數檔案的範本檔案Deployed the template file with the parameter file

最後一個步驟是透過 PowerShell Cmdlet 輕鬆完成。The last step is easily done by a PowerShell cmdlet. 若要查看 Visual Studio 在部署您的應用程式時所執行的作業,請開啟 Scripts\Deploy-AzureResourceGroup.ps1。To see what Visual Studio did when it deployed your application, open Scripts\Deploy-AzureResourceGroup.ps1. 在該處有很多程式碼,但我只想要討論部署具有參數檔案之範本檔案所需的所有相關程式碼。There’s a lot of code there, but I’m just going to highlight all the pertinent code you need to deploy the template file with the parameter file.

顯示腳本中的相關程式碼,您需要使用這些程式碼來部署具有參數檔案的範本檔案。

最後一個 Cmdlet ( New-AzureResourceGroup) 是實際執行動作的 Cmdlet。The last cmdlet, New-AzureResourceGroup, is the one that actually performs the action. 所有這些動作都是在告訴您,在工具的協助下,透過可預測方式部署您的雲端應用程式相當簡單。All this should demonstrate to you that, with the help of tooling, it is relatively straightforward to deploy your cloud application predictably. 每次對具有相同參數檔案的相同範本執行 Cmdlet 時,結果都會相同。Every time you run the cmdlet on the same template with the same parameter file, you’re going to get the same result.

總結Summary

在 DevOps 中,重複性和可預測性是任何成功部署包含微服務之高級別應用程式的關鍵。In DevOps, repeatability and predictability are keys to any successful deployment of a high-scale application composed of microservices. 在本教學課程中,您已使用 Azure 資源管理員範本將雙微服務應用程式部署至 Azure 以做為單一資源群組。In this tutorial, you have deployed a two-microservice application to Azure as a single resource group using the Azure Resource Manager template. 希望這可讓您了解如何開始將 Azure 中的應用程式轉換成範本,而且可以透過可預測方式進行佈建和部署。Hopefully, it has given you the knowledge you need in order to start converting your application in Azure into a template and can provision and deploy it predictably.

其他資源More resources

下一步Next steps

若要深入了解本文中所部署資源類型的 JSON 語法和屬性,請參閱:To learn about the JSON syntax and properties for resource types deployed in this article, see: