將 Spring Boot JAR 檔案 Web 應用程式部署至 Linux 上的 Azure App ServiceDeploy a Spring Boot JAR file web app to Azure App Service on Linux

本文示範如何使用適用於 App Service Web Apps 的 Maven 外掛程式,將 Spring Boot 應用程式封裝成 Java SE JAR,並部署至 Linux 上的 Azure App ServiceThis article demonstrates using the Maven Plugin for Azure App Service Web Apps to deploy a Spring Boot application packaged as a Java SE JAR to Azure App Service on Linux. 若要將應用程式相依性、執行階段及組態合併至單一可部署成品,請從 Tomcat 與 WAR 檔案中選擇 Java SE 部署。Choose Java SE deployment over Tomcat and WAR files when you want to consolidate your app's dependencies, runtime, and configuration into a single deployable artifact.

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don’t have an Azure subscription, create a free account before you begin.

必要條件Prerequisites

若要完成本教學課程中的步驟,您必須已安裝以下項目並設定完成:To complete the steps in this tutorial, you'll need to have the following installed and configured:

安裝並登入 Azure CLIInstall and sign in to Azure CLI

讓 Maven 外掛程式部署 Spring Boot 應用程式最簡單且最輕鬆的方式是使用 Azure CLIThe simplest and easiest way to get the Maven Plugin deploying your Spring Boot application is by using Azure CLI.

使用 Azure CLI 登入您的 Azure 帳戶:Sign into your Azure account by using the Azure CLI:

az login

依照指示完成登入程序。Follow the instructions to complete the sign-in process.

複製範例應用程式Clone the sample app

在本節中,您會在本機複製已完成的 Spring Boot 應用程式,並且進行測試。In this section, you will clone a completed Spring Boot application and test it locally.

  1. 開啟命令提示字元或終端機視窗,並建立本機目錄來保存您的 Spring Boot 應用程式,然後變更至該目錄;例如:Open a command prompt or terminal window and create a local directory to hold your Spring Boot application, and change to that directory; for example:

    md C:\SpringBoot
    cd C:\SpringBoot
    

    -- 或 ---- or --

    md ~/SpringBoot
    cd ~/SpringBoot
    
  2. Spring Boot Getting Started 範例專案複製到您建立的目錄中;例如:Clone the Spring Boot Getting Started sample project into the directory you created; for example:

    git clone https://github.com/spring-guides/gs-spring-boot
    
  3. 將目錄變更至已完成的專案;例如:Change directory to the completed project; for example:

    cd gs-spring-boot/complete
    
  4. 使用 Maven 建立 JAR 檔案;例如:Build the JAR file using Maven; for example:

    mvn clean package
    
  5. 建立 Web 應用程式後,使用 Maven 啟動 Web 應用程式,例如:When the web app has been created, start the web app using Maven; for example:

    mvn spring-boot:run
    
  6. 測試 Web 應用程式,方法是使用網頁瀏覽器在本機瀏覽它。Test the web app by browsing to it locally using a web browser. 例如,如果您有 curl 可用,可以使用下列命令:For example, you could use the following command if you have curl available:

    curl http://localhost:8080
    
  7. 您應該會看到下列訊息:Greetings from Spring Boot!You should see the following message displayed: Greetings from Spring Boot!

為 Azure App Service 設定 Maven 外掛程式Configure Maven Plugin for Azure App Service

在本節中,您將設定 Spring Boot 專案pom.xml,以便 Maven 可以將應用程式部署至 Linux 上的 Azure App Service。In this section, you will configure the Spring Boot project pom.xml so that Maven can deploy the app to Azure App Service on Linux.

  1. 在程式碼編輯器中,開啟 pom.xmlOpen pom.xml in a code editor.

  2. 在 pom.xml 的 <build> 區段中,於 <plugins> 標記內新增下列 <plugin> 項目。In the <build> section of the pom.xml, add the following <plugin> entry inside the <plugins> tag.

    <plugin>
     <groupId>com.microsoft.azure</groupId>
     <artifactId>azure-webapp-maven-plugin</artifactId>
     <version>1.5.4</version>
     <configuration>
       <deploymentType>jar</deploymentType>
    
       <!-- configure app to run on port 80, required by App Service -->
       <appSettings>
         <property> 
           <name>JAVA_OPTS</name> 
           <value>-Dserver.port=80</value> 
         </property> 
       </appSettings>
    
       <!-- Web App information -->
       <resourceGroup>${RESOURCEGROUP_NAME}</resourceGroup>
       <appName>${WEBAPP_NAME}</appName>
       <region>${REGION}</region>  
    
       <!-- Java Runtime Stack for Web App on Linux-->
       <linuxRuntime>jre8</linuxRuntime>
     </configuration>
    </plugin>
    
  3. 更新外掛程式組態中的下列預留位置:Update the following placeholders in the plugin configuration:

PlaceholderPlaceholder 說明Description
RESOURCEGROUP_NAME 要在其中建立 Web 應用程式的新資源群組名稱。Name for the new resource group in which to create your web app. 將應用程式的所有資源放在群組中,藉此同時管理。By putting all the resources for an app in a group, you can manage them together. 例如,刪除資源群組會刪除所有與應用程式相關聯的資源。For example, deleting the resource group would delete all resources associated with the app. 使用唯一的新資源群組名稱來更新此值,例如 TestResources 。Update this value with a unique new resource group name, for example, TestResources. 您在下一節中會使用此資源群組名稱來清除所有的 Azure 資源。You will use this resource group name to clean up all Azure resources in a later section.
WEBAPP_NAME 應用程式名稱會成為 Web 應用程式在部署至 Azure 時的部分主機名稱 (WEBAPP_NAME.azurewebsites.net)。The app name will be part the host name for the web app when deployed to Azure (WEBAPP_NAME.azurewebsites.net). 將此值更新為新 Azure Web 應用程式的唯一名稱 (例如 contoso ),它將會主控您的 Java 應用程式。Update this value with a unique name for the new Azure web app, which will host your Java app, for example contoso.
REGION 託管 Web 應用程式的 Azure 區域,例如 westus2An Azure region where the web app is hosted, for example westus2. 您可以使用 az account list-locations 命令,從 Cloud Shell 或 CLI 取得區域清單。You can get a list of regions from the Cloud Shell or CLI using the az account list-locations command.

您可以在 GitHub 上的 Maven 外掛程式參考中找到組態選項的完整清單。A full list of configuration options can be found in the Maven plugin reference on GitHub.

將應用程式部署至 AzureDeploy the app to Azure

一旦您已設定本文上述章節中的所有設定,您已準備好將 Web 應用程式部署至 Azure。Once you have configured all of the settings in the preceding sections of this article, you are ready to deploy your web app to Azure. 若要這樣做,請使用下列步驟:To do so, use the following steps:

  1. 如果您對 pom.xml 檔案進行任何變更,從您稍早使用的命令提示字元或終端機視窗,使用 Maven 重新建置 JAR 檔案;例如:From the command prompt or terminal window that you were using earlier, rebuild the JAR file using Maven if you made any changes to the pom.xml file; for example:

    mvn clean package
    
  2. 使用 Maven 將您的 Web 應用程式部署至 Azure;例如:Deploy your web app to Azure by using Maven; for example:

    mvn azure-webapp:deploy
    

Maven 會將您的 Web 應用程式部署至 Azure;如果 Web 應用程式或 Web 應用程式方案不存在,系統會為您建立。Maven will deploy your web app to Azure; if the web app or web app plan does not already exist, it will be created for you.

網站部署完成時,您就可以使用 Azure 入口網站來管理。When your web has been deployed, you will be able to manage it through the Azure portal.

  • 您的 Web 應用程式會列在應用程式服務 中:Your web app will be listed in App Services:

    列在 Azure 入口網站應用程式服務中的 Web 應用程式

  • Web 應用程式的 URL 會列在 Web 應用程式的 [概觀] 中:And the URL for your web app will be listed in the Overview for your web app:

    決定 Web 應用程式的 URL

使用與之前相同的 cURL 命令來確認部署是否成功;請使用入口網站中的 Web 應用程式 URL,不要使用 localhostVerify that the deployment was successful by using the same cURL command as before, using your web app URL from the Portal instead of localhost. 您應該會看到下列訊息:Greetings from Spring Boot!You should see the following message displayed: Greetings from Spring Boot!

後續步驟Next steps

若要深入了解 Spring 和 Azure,請繼續閱讀「Azure 上的 Spring」文件中心中的資訊。To learn more about Spring and Azure, continue to the Spring on Azure documentation center.

其他資源Additional Resources

如需本文所討論之各種技術的詳細資訊,請參閱下列文章:For more information about the various technologies discussed in this article, see the following articles: