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

快速入门:在 Azure 应用服务中创建 Java 应用Quickstart: Create a Java app on Azure App Service

Azure 应用服务提供高度可缩放、自修复的 Web 托管服务。Azure App Service provides a highly scalable, self-patching web hosting service. 本快速入门介绍如何将 Azure CLI用于 Maven 的 Azure Web 应用插件配合使用来部署 .jar 文件或 .war 文件。This quickstart shows how to use the Azure CLI with the Azure Web App Plugin for Maven to deploy a .jar file, or .war file. 使用选项卡在 Java SE 和 Tomcat 说明之间切换。Use the tabs to switch between Java SE and Tomcat instructions.

备注

也可使用 IntelliJ 和 Eclipse 等常见 IDE 执行相同的操作。The same can also be done using popular IDEs like IntelliJ and Eclipse. 请查看 Azure Toolkit for IntelliJ 快速入门Azure Toolkit for Eclipse 快速入门中的类似文档。Check out our similar documents at Azure Toolkit for IntelliJ Quickstart or Azure Toolkit for Eclipse Quickstart.

在 Azure 应用服务中运行的示例应用

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 可以将 Bash 或 PowerShell 与 Cloud Shell 配合使用来使用 Azure 服务。You can use either Bash or PowerShell with Cloud Shell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To start Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。Select Try It in the upper-right corner of a code block. 选择“试用”不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell”按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window
选择 Azure 门户右上角菜单栏上的 Cloud Shell 按钮。Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 启动 Cloud Shell。Start Cloud Shell.

  2. 选择代码块上的“复制”按钮以复制代码。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上选择 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上选择 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. 选择 Enter 运行此代码。Select Enter to run the code.

创建 Java 应用Create a Java app

克隆 Spring Boot 入门示例项目。Clone the Spring Boot Getting Started sample project.

git clone https://github.com/spring-guides/gs-spring-boot

将目录更改为已完成项目。Change directory to the completed project.

cd gs-spring-boot/complete

配置 Maven 插件Configure the Maven plugin

部署到 Azure 应用服务的过程会自动使用 Azure CLI 中的 Azure 凭据。The deployment process to Azure App Service will use your Azure credentials from the Azure CLI automatically. 如果未在本地安装 Azure CLI,则 Maven 插件将使用 Oauth 或设备登录名进行身份验证。If the Azure CLI is not installed locally, then the Maven plugin will authenticate with Oauth or device login. 有关详细信息,请参阅 Maven 插件的身份验证For more information, see authentication with Maven plugins.

运行下面的 Maven 命令来配置部署。Run the Maven command below to configure the deployment. 此命令将帮助你设置应用服务操作系统、Java 版本和 Tomcat 版本。This command will help you to set up the App Service operating system, Java version, and Tomcat version.

mvn com.microsoft.azure:azure-webapp-maven-plugin:1.12.0:config
  1. 当系统提示“订阅”选项时,通过在行首输入数字来选择适当的 SubscriptionWhen prompted with Subscription option, select the proper Subscription by entering the number print in the line start.

  2. 当系统提示“Web 应用”选项时,按 Enter 键接受默认选项 <create> 或选择一个现有应用。When prompted with Web App option, accept the defaut option <create> by pressing enter or select an existing app.

  3. 当系统提示 OS 选项时,通过输入 3 选择 Windows 。When prompted with OS option, select Windows by entering 3.

  4. 当系统提示“定价层”选项时,通过输入 2 选择 B2 。When prompted with Pricing Tier option, select B2 by entering 2.

  5. 按 Enter 使用默认的 Java 版本 Java 8。Use the default Java version, Java 8, by pressing enter.

  6. 最后,出现最后一个提示时按 Enter 来确认所做的选择。Finally, press enter on the last prompt to confirm your selections.

    摘要输出将类似于下面所示的代码片段。Your summary output will look similar to the snippet shown below.

    Please confirm webapp properties
    Subscription Id : ********-****-****-****-************
    AppName : spring-boot-1599007390755
    ResourceGroup : spring-boot-1599007390755-rg
    Region : westeurope
    PricingTier : Basic_B2
    OS : Windows
    Java : 1.8
    WebContainer : java 8
    Deploy to slot : false
    Confirm (Y/N)? : Y
    [INFO] Saving configuration to pom.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 41.118 s
    [INFO] Finished at: 2020-09-01T17:43:45-07:00
    [INFO] ------------------------------------------------------------------------
    
  1. 当系统提示“订阅”选项时,通过在行首输入数字来选择适当的 SubscriptionWhen prompted with Subscription option, select the proper Subscription by entering the number print in the line start.

  2. 当系统提示“Web 应用”选项时,按 Enter 键接受默认选项 <create> 或选择一个现有应用。When prompted with Web App option, accept the defaut option <create> by pressing enter or select an existing app.

  3. 当系统提示 OS 选项时,按 Enter 键选择 Linux 。When prompted with OS option, select Linux by pressing enter.

  4. 当系统提示“定价层”选项时,通过输入 2 选择 B2 。When prompted with Pricing Tier option, select B2 by entering 2.

  5. 按 Enter 使用默认的 Java 版本 Java 8。Use the default Java version, Java 8, by pressing enter.

  6. 最后,出现最后一个提示时按 Enter 来确认所做的选择。Finally, press enter on the last prompt to confirm your selections.

    Please confirm webapp properties
    Subscription Id : ********-****-****-****-************
    AppName : spring-boot-1599007116351
    ResourceGroup : spring-boot-1599007116351-rg
    Region : westeurope
    PricingTier : Basic_B2
    OS : Linux
    RuntimeStack : JAVA 8-jre8
    Deploy to slot : false
    Confirm (Y/N)? : Y
    [INFO] Saving configuration to pom.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 20.925 s
    [INFO] Finished at: 2020-09-01T17:38:51-07:00
    [INFO] ------------------------------------------------------------------------
    

如果需要,可以直接在 pom.xml 中修改应用服务的配置。You can modify the configurations for App Service directly in your pom.xml if needed. 下面列出了一些常见配置:Some common ones are listed below:

属性Property 必须Required 说明Description 版本Version
<schemaVersion> falsefalse 指定配置架构的版本。Specify the version of the configuration schema. 支持的值是:v1v2Supported values are: v1, v2. 1.5.21.5.2
<subscriptionId> falsefalse 指定订阅 ID。Specify the subscription id. 0.1.0+0.1.0+
<resourceGroup> true 用于 Web 应用的 Azure 资源组。Azure Resource Group for your Web App. 0.1.0+0.1.0+
<appName> true Web 应用的名称。The name of your Web App. 0.1.0+0.1.0+
<region> true 指定将托管 Web 应用的区域;默认值为“westeurope”。Specifies the region where your Web App will be hosted; the default value is westeurope. 支持的区域部分中列出了所有有效区域。All valid regions at Supported Regions section. 0.1.0+0.1.0+
<pricingTier> falsefalse Web 应用的定价层。The pricing tier for your Web App. 生产工作负载的默认值为 P1V2,而 B2 是建议用于 Java 开发/测试的最低值 。The default value is P1V2 for production workload, while B2 is the recommended minimum for Java dev/test. 了解详细信息Learn more 0.1.0+0.1.0+
<runtime> true 运行时环境配置,可以在此处查看详细信息。The runtime environment configuration, you could see the detail here. 0.1.0+0.1.0+
<deployment> true 部署配置,可以在此处查看详细信息。The deployment configuration, you could see the details here. 0.1.0+0.1.0+

请注意 <appName><resourceGroup> 的值(在演示中相应地为 helloworld-1590394316693helloworld-1590394316693-rg),后面会使用它们。Be careful about the values of <appName> and <resourceGroup>(helloworld-1590394316693 and helloworld-1590394316693-rg accordingly in the demo), they will be used later.

部署应用Deploy the app

Maven 插件会使用 Azure CLI 中的帐户凭据来部署到应用服务。The Maven plugin uses account credentials from the Azure CLI to deploy to App Services. 在继续操作之前使用 Azure CLI 登录Sign in with the Azure CLI before continuing.

az login

然后你可使用以下命令将 Java 应用部署到 Azure。Then you can deploy your Java app to Azure using the following command.

mvn package azure-webapp:deploy

部署完成后,应用程序会在 http://<appName>.azurewebsites.net/(在演示中为 http://helloworld-1590394316693.azurewebsites.net)处准备就绪。Once deployment has completed, your application will be ready at http://<appName>.azurewebsites.net/(http://helloworld-1590394316693.azurewebsites.net in the demo). 在本地 Web 浏览器中打开 url,你应该会看到Open the url with your local web browser, you should see

在 Azure 应用服务中运行的示例应用

祝贺你!Congratulations! 现已将第一个 Java 应用部署到应用服务。You've deployed your first Java app to App Service.

清理资源Clean up resources

在前面的步骤中,你在资源组中创建了 Azure 资源。In the preceding steps, you created Azure resources in a resource group. 如果认为将来不需要这些资源,请通过门户删除资源组,或在 Cloud Shell 中运行以下命令来这样做:If you don't expect to need these resources in the future, delete the resource group from portal, or by running the following command in the Cloud Shell:

az group delete --name <your resource group name; for example: helloworld-1558400876966-rg> --yes

此命令可能需要花费一点时间运行。This command may take a minute to run.

后续步骤Next steps