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

Azure 应用服务提供高度可缩放、自修复的 Web 托管服务。Azure App Service provides a highly scalable, self-patching web hosting service. 本快速入门介绍如何将 Azure CLI用于 Maven 的 Azure Web 应用插件配合使用来部署 Java Web 存档 (WAR) 文件。This quickstart shows how to use the Azure CLI with the Azure Web App Plugin for Maven to deploy a Java web archive (WAR) file.

备注

也可使用 IntelliJ 和 Eclipse 等常见 IDE 执行相同的操作。The same thing 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 trial account before you begin.

创建 Java 应用Create a Java app

在 Azure CLI 提示符下,执行以下 Maven 命令来创建一个名为 helloworld 的新应用:Execute the following Maven command in the Azure CLI prompt to create a new app named helloworld:

mvn archetype:generate -DgroupId=example.demo -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-webapp -Dversion=1.0-SNAPSHOT

然后,将工作目录更改为项目文件夹:Then change your working directory to the project folder:

cd helloworld

配置 Maven 插件Configure the Maven plugin

可以在命令提示符中运行以下 maven 命令以配置部署,在第一步中为 Windows OS 选择“2”并按 Enter 接受默认配置,直到出现“确认(Y/N)”提示,然后按“y”完成配置 。You can run the following maven command in the Command Prompt to configure the deployment, choose '2' for the windows OS in the first step, then accept the default configurations by pressing ENTER until you get the Confirm (Y/N) prompt, then press 'y' and the configuration is done.

mvn com.microsoft.azure:azure-webapp-maven-plugin:1.9.1:config

示例过程如下所示:A sample process looks like:

<plugins>
    <!--*************************************************-->
    <!-- Deploy to Tomcat in App Service Windows         -->
    <!--*************************************************-->
    <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.9.0</version>
        <configuration>
            <!-- Specify v2 schema -->
            <schemaVersion>v2</schemaVersion>
            <!-- App information -->
            <subscriptionId>SUBSCRIPTION_ID</subscriptionId>
            <resourceGroup>RESOURCEGROUP_NAME</resourceGroup>
            <appName>WEBAPP_NAME</appName>
            <region>REGION</region>
            <!-- Java Runtime Stack for App Service on Windows-->
            <runtime>
                <os>windows</os>
                <javaVersion>1.8</javaVersion>
                <webContainer>tomcat 9.0</webContainer>
            </runtime>
            <deployment>
                <resources>
                    <resource>
                        <directory>${project.basedir}/target</directory>
                        <includes>
                            <include>*.war</include>
                        </includes>
                    </resource>
                </resources>
            </deployment>
        </configuration>
    </plugin>
</plugins>

打开 pom.xml,查看更新后的配置。Open to pom.xml to see the updated configuration.

code pom.xml

如果需要,可以直接在 pom 文件中修改应用服务的配置,下面列出了一些常见配置:You can modify the configurations for App Service directly in your pom file 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
<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. 默认值为 P1V2The default value is P1V2. 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+

部署应用Deploy the app

部署到 Azure 应用服务的过程中会使用 Azure CLI 中的帐户凭据。The deploy process to Azure App Service uses account credentials from the Azure CLI. 在继续操作之前使用 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

部署完成后,在 Web 浏览器中使用以下 URL 浏览到已部署的应用程序,例如 http://<webapp>.chinacloudsites.cn/Once deployment has completed, browse to the deployed application using the following URL in your web browser, for example http://<webapp>.chinacloudsites.cn/.

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

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

清理资源Clean up resources

若要清理资源,请运行以下命令:To clean up your resources, run the following command:

az group delete --name myResourceGroup

后续步骤Next steps