練習 - 部署 Spring Boot 應用程式

已完成

在本單元中,您將設定透過 Maven 部署 Spring Boot 應用程式。 接著,建立及部署至 Azure App Service 執行個體。

設定 Maven 外掛程式

Azure App Service 的部署程序會自動使用來自 Azure CLI 的 Azure 認證。

執行下列 Maven 命令以設定部署。 此命令會協助設定 App Service 作業系統、Azure 訂閱和 Java 版本。

mvn com.microsoft.azure:azure-webapp-maven-plugin:1.12.0:config
  1. 當系統提示使用 [訂閱] 選項時,請在行首輸入適當的號碼以選取訂閱。

  2. 當系統提示使用 [Web 應用程式] 選項時,請按 Enter 接受預設選項 <create>

  3. 當系統提示使用 [OS] 選項時,請按 Enter 選取 linux

  4. 按 Enter 選擇預設的 Java 版本 1.8。

  5. 在最後一個提示中,按 Enter 確認選擇。

    Please confirm webapp properties
    AppName : demo-1604579125693
    ResourceGroup : demo-1604579125693-rg
    Region : eastus
    PricingTier : PremiumV2_P1v2
    OS : Linux
    Java : Java 8
    Web server stack: Java SE
    Deploy to slot : false
    Confirm (Y/N) [Y]: Y
    [INFO] Saving configuration to pom.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  28.781 s
    [INFO] Finished at: 2020-11-05T14:30:00+02:00
    [INFO] ------------------------------------------------------------------------
    

重要

區域的預設值是 westeurope。 為達到最佳效能,請將值變更為 eastus,因為資料庫位於此區域。

在專案的 pom.xml 檔案中,變更下列欄位。

    <configuration>
        ...
        <region>eastus</region>
        ...
    </configuration>

部署應用程式

Maven 部署外掛程式會建立 Azure App Service 伺服器。 將 JAR 檔案複製到應用程式伺服器的 /local/site/wwwroot 目錄。 最後,此外掛程式會使用 Spring Boot JAR 檔案的內嵌 HTTP 伺服器啟動應用程式。

接下來,使用下列命令將 Spring Boot 應用程式部署到 Azure:

mvn package com.microsoft.azure:azure-webapp-maven-plugin:1.12.0:deploy

這個螢幕擷取畫面會顯示部署到 Azure App Service 的應用程式:

顯示已部署應用程式的螢幕擷取畫面。

測試 Azure App Service 應用程式

當部署完成後,應用程式即會在 http://<appName>.azurewebsites.net/ 就緒。

您可使用 cURL 以測試應用程式。

在重新部署應用程式時,即已清除資料庫。 現在必須在資料庫中建立新的待辦事項:

curl --header "Content-Type: application/json" \
    --request POST \
    --data '{"description":"configuration","details":"congratulations, you have set up your Spring Boot application correctly!","done": "true"}' \
    http://<appName>.azurewebsites.net

此命令應該會傳回已建立的項目:

{"id":1,"description":"configuration","details":"congratulations, you have set up your Spring Boot application correctly!","done":true}

使用新的 cURL 要求擷取資料:

curl http://<appName>.azurewebsites.net

此命令會傳回待辦事項清單,包括所建立的項目:

[{"id":1,"description":"configuration","details":"congratulations, you have set up your Spring Boot application correctly!","done":true}]

注意

您也可以在網頁瀏覽器中開啟 URL 以傳回待辦事項清單。