快速入门:在 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 trail subscription, create a trial subscription before you begin.

备注

请先运行 az cloud set -n AzureChinaCloud 更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI in Azure China , please run az cloud set -n AzureChinaCloud first to change the cloud environment. 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

创建 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 : chinanorth2
    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 : chinanorth2
    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 应用的区域;默认值为“chinanorth2”。Specifies the region where your Web App will be hosted; the default value is chinanorth2. 支持的区域部分中列出了所有有效区域。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 cloud set -n AzureChinaCloud
az login

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

mvn package azure-webapp:deploy

部署完成后,应用程序会在 http://<appName>.chinacloudsites.cn/(在演示中为 http://helloworld-1590394316693.chinacloudsites.cn)处准备就绪。Once deployment has completed, your application will be ready at http://<appName>.chinacloudsites.cn/(http://helloworld-1590394316693.chinacloudsites.cn 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. 如果认为将来不需要这些资源,请通过门户删除资源组,或在 Azure CLI 中运行以下命令: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 Azure CLI:

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

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

后续步骤Next steps