您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Linux 上将 Spring Boot JAR 文件 Web 应用部署到 Azure 应用服务Deploy a Spring Boot JAR file web app to Azure App Service on Linux

本文演示如何使用适用于 Azure 应用服务 Web 应用的 Maven 插件将打包为 Java SE JAR 的 Spring Boot 应用程序部署到 Linux 上的 Azure 应用服务This 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

获取用于部署 Spring Boot 应用程序的 Maven 插件的最简单方法是使用 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 入门示例项目克隆到已创建的目录;例如: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 浏览器在本地浏览到 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 应用服务的 Maven 插件Configure Maven Plugin for Azure App Service

在本部分中,将配置 Spring Boot 项目 pom.xml,以便 Maven 可以将应用部署到 Linux 上的 Azure 应用服务。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.6.0</version>
    </plugin>
    
  3. 然后可以配置部署,在命令提示符下运行 maven 命令 mvn azure-webapp:config,并使用数字选择提示中的这些选项:Then you can configure the deployment, run the maven command mvn azure-webapp:config in the Command Prompt and use the number to choose these options in the prompt:

    • OS:linuxOS: linux
    • javaVersion:jre8javaVersion: jre8
    • runtimeStack:jre8runtimeStack: jre8

显示 Confirm (Y/N) 提示时,请按 'y' ,配置将完成。When you get the Confirm (Y/N) prompt, press 'y' and the configuration is done.

~@Azure:~/gs-spring-boot/complete$ mvn azure-webapp:config
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< org.springframework:gs-spring-boot >-----------------
[INFO] Building gs-spring-boot 0.1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- azure-webapp-maven-plugin:1.6.0:config (default-cli) @ gs-spring-boot ---
[WARNING] The plugin may not work if you change the os of an existing webapp.
Define value for OS(Default: Linux):
1. linux [*]
2. windows
3. docker
Enter index to use:
Define value for javaVersion(Default: jre8):
1. jre8 [*]
2. java11
Enter index to use:
Define value for runtimeStack(Default: TOMCAT 8.5):
1. TOMCAT 9.0
2. jre8
3. TOMCAT 8.5 [*]
4. WILDFLY 14
Enter index to use: 2
Please confirm webapp properties
AppName : gs-spring-boot-1559091271202
ResourceGroup : gs-spring-boot-1559091271202-rg
Region : westeurope
PricingTier : Premium_P1V2
OS : Linux
RuntimeStack : JAVA 8-jre8
Deploy to slot : false
Confirm (Y/N)? : Y
  1. <appSettings> 部分添加到 <azure-webapp-maven-plugin><configuration> 部分,以便在 80 端口上侦听。Add the <appSettings> section to the <configuration> section of <azure-webapp-maven-plugin> to listen on the 80 port.

    <plugin>
       <groupId>com.microsoft.azure</groupId>
       <artifactId>azure-webapp-maven-plugin</artifactId>
       <version>1.6.0</version>
       <configuration>
          <schemaVersion>V2</schemaVersion>
          <resourceGroup>gs-spring-boot-1559091271202-rg</resourceGroup>
          <appName>gs-spring-boot-1559091271202</appName>
          <region>westeurope</region>
          <pricingTier>P1V2</pricingTier>
    
          <!-- Begin of App Settings  -->
          <appSettings>
             <property>
                   <name>JAVA_OPTS</name>
                   <value>-Dserver.port=80</value>
             </property>
          </appSettings>
          <!-- End of App Settings  -->
          ...
         </configuration>
    </plugin>
    

将应用部署到 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.

Web 部署完成后即可通过 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而不是 localhost)验证部署是否成功。Verify 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: