共用方式為


使用 Maven 部署 Spring Boot 應用程式

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但是您暫時還是會在某些位置看到舊的名稱。我們正在致力更新螢幕擷取畫面、影片和圖表等資產。

本文適用於: ✔️ Java ❌ C#

本文適用於: ✔️基本/標準✔️企業

本文說明如何使用 Azure Spring Apps Maven 外掛程式,將應用程式設定及部署至 Azure Spring Apps。

必要條件

產生 Spring 專案

若要建立 Spring 專案以供本文使用,請使用下列步驟:

  1. 流覽至 Spring Initializr 以產生具有 Azure Spring Apps 建議相依性的範例專案。 此連結會使用下列 URL 為您提供預設設定。

    https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.7&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=hellospring&name=hellospring&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.hellospring&dependencies=web,cloud-eureka,actuator,cloud-config-client
    

    下圖顯示此範例項目的建議 Spring Initializr 設定。

    顯示建議設定之 Spring Initializr 頁面的螢幕快照。

    此範例使用Java第8版。 如果您想要使用 Java 第 11 版,請變更 [項目元數據] 底下的 選項。

  2. 選取 [ 設定所有相依性時產生 ]。

  3. 下載並解壓縮套件,然後建立 Web 應用程式的 Web 控制器。 使用下列內容新增 src/main/java/com/example/hellospring/HelloController.java檔案:

    package com.example.hellospring;
    
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RestController
    public class HelloController {
    
        @RequestMapping("/")
        public String index() {
            return "Greetings from Azure Spring Apps!";
        }
    
    }
    

在本機建置 Spring 應用程式

若要使用 Maven 建置專案,請執行下列命令:

cd hellospring
mvn clean package -DskipTests -Denv=cloud

編譯專案需要幾分鐘的時間。 完成之後,您應該在其各自的資料夾中,針對每個服務都有個別的 JAR 檔案。

布建 Azure Spring Apps 的實例

下列程式會使用 Azure 入口網站 建立 Azure Spring Apps 的實例。

  1. 在新索引標籤,開啟 Azure 入口網站

  2. 從頂端的搜尋方塊,搜尋 Azure Spring Apps

  3. 從結果中選取 [Azure Spring Apps ]。

  4. 在 [Azure Spring Apps] 頁面上,選取 [ 建立]。

    顯示 Azure Spring Apps 資源的 Azure 入口網站 螢幕快照,其中已醒目提示 [建立] 按鈕。

  5. 填寫 [Azure Spring Apps 建立 ] 頁面上的窗體。 請參考下列指引:

    • 用帳戶:選取您想要為此資源計費的訂用帳戶。
    • 資源群組:為新資源建立新的資源群組是最佳做法。 您稍後的步驟會使用此資源群組作為資源組名>。<
    • 服務詳細資料/名稱:指定 <服務實例名稱>。 名稱長度必須介於 4 到 32 個字元之間,而且只能包含小寫字母、數位和連字元。 服務名稱的第一個字元必須是字母,最後一個字元必須是字母或數位。
    • 位置:選取服務實例的區域。

    顯示 Azure Spring Apps 建立頁面之 Azure 入口網站 的螢幕快照。

  6. 選取 [檢閱和建立]

產生組態並部署至 Azure Spring Apps

若要產生組態並部署應用程式,請遵循下列步驟:

  1. 從包含 POM 檔案的 hellospring 根資料夾執行下列命令。 如果您已經使用 Azure CLI 登入,命令會自動挑選認證。 否則,命令會提示您輸入登入指示。 如需詳細資訊,請參閱 GitHub 上 azure-maven-plugins 存放庫中的驗證

    mvn com.microsoft.azure:azure-spring-apps-maven-plugin:1.10.0:config
    

    系統會要求您選取:

    • 訂用帳戶識別碼 - 您用來建立 Azure Spring Apps 實例的訂用帳戶。
    • 服務實例 - Azure Spring Apps 實例的名稱。
    • 應用程式名稱 - 您選擇的應用程式名稱,或使用預設值 artifactId
    • 公用端點 - true 表示將應用程式公開為公用存取,否則 為 false
  2. 確認 appName POM 檔案中的 專案具有正確的值。 POM 檔案的相關部分看起來應該類似下列範例。

    <build>
        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-spring-apps-maven-plugin</artifactId>
                <version>1.10.0</version>
                <configuration>
                    <subscriptionId>xxxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx</subscriptionId>
                    <clusterName>v-spr-cld</clusterName>
                    <appName>hellospring</appName>
    

    POM 檔案現在包含外掛程式相依性和組態。

  3. 使用下列命令部署應用程式。

    mvn azure-spring-apps:deploy
    

確認服務

部署完成後,您可以在 存取應用程式 https://<service instance name>-hellospring.azuremicroservices.io/

Hello Spring 應用程式的螢幕快照,如瀏覽器中所示。

清除資源

如果您打算繼續使用範例應用程式,您可能想要保留資源。 若不再需要,請刪除包含 Azure Spring Apps 實例的資源群組。 若要使用 Azure CLI 刪除資源群組,請使用下列命令:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az group delete --name $resourceGroupName &&
echo "Press [ENTER] to continue ..."

下一步