使用 Maven 部署 Spring Boot 应用程序

注意

Azure Spring Apps 是 Azure Spring Cloud 服务的新名称。 虽然该服务有新名称,但一些地方仍会使用旧名称,我们仍在更新屏幕截图、视频和图形等资产。

本文介绍如何使用 Azure Spring Apps Maven 插件配置应用程序并将其部署到 Azure Spring Apps。

先决条件

生成 Spring 项目

若要创建用于本文的 Spring 项目,请使用以下步骤:

  1. 导航到 Spring Initializr 以生成一个包含推荐的 Azure Spring Apps 依赖项的示例项目。 此链接使用以下 URL 为你提供默认设置。

    https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.7&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=hellospring&name=hellospring&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.hellospring&dependencies=web,cloud-eureka,actuator,cloud-config-client
    

    下图显示了针对此示例项目推荐的 Spring Initializr 设置。

    Screenshot of Spring Initializr.

    此示例使用 Java 版本 8。 如果要使用 Java 版本 11,请更改“项目元数据”下的选项。

  2. 设置好所有依赖项后,选择“生成”。

  3. 下载包并将其解压缩,然后为 Web 应用程序创建 Web 控制器。 添加具有以下内容的文件 src/main/java/com/example/hellospring/HelloController.java:

    package com.example.hellospring;
    
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RestController
    public class HelloController {
    
        @RequestMapping("/")
        public String index() {
            return "Greetings from Azure Spring Apps!";
        }
    
    }
    

本地构建 Spring 应用程序

若要使用 Maven 生成项目,请运行以下命令:

cd hellospring 
mvn clean package -DskipTests -Denv=cloud

编译项目需要几分钟时间。 完成后,相应的文件夹中应会包含每个服务的各个 JAR 文件。

预配 Azure Spring Apps 的实例

以下步骤使用 Azure 门户创建 Azure Spring Apps 的实例。

  1. 在新选项卡中,打开 Azure 门户

  2. 在顶部搜索框中,搜索“Azure Spring Apps”。

  3. 从结果中选择“Azure Spring Apps”。

    Screenshot of Azure portal showing Azure Spring Apps service in search results.

  4. 在 Azure Spring Apps 页面上,选择“创建”。

    Screenshot of Azure portal showing Azure Spring Apps resource with Create button highlighted.

  5. 在 Azure Spring Apps 的“创建”页中填写表单。 遵循以下指南:

    • 订阅:选择要在其中收取此资源费用的订阅。
    • 资源组:最佳做法是为新资源创建新的资源组。 在后面的步骤中,你将使用此资源组作为 <资源组名称>。
    • 服务详细信息/名称:指定 <服务实例名称>。 该名称必须为 4 到 32 个字符,只能包含小写字母、数字及连字符。 服务名称的第一个字符必须是字母,最后一个字符必须是字母或数字。
    • 位置:为服务实例选择区域。

    Screenshot of Azure portal showing Azure Spring Apps Create page.

  6. 选择“查看并创建”。

生成配置并将其部署到 Azure Spring Apps

若要生成配置并部署应用,请执行以下步骤:

  1. 从包含 POM 文件的 hellospring 根文件夹运行以下命令。 如果已使用 Azure CLI 登录,则该命令将自动提取凭据。 否则,该命令将提示登录说明。 有关详细信息,请参阅 GitHub 上 azure-maven-plugins 存储库中的身份验证

    mvn com.microsoft.azure:azure-spring-apps-maven-plugin:1.10.0:config
    

    系统会提示你选择:

    • 订阅 ID - 用于创建 Azure Spring Apps 实例的订阅。
    • 服务实例 - Azure Spring Apps 实例的名称。
    • 应用名称 - 所选的应用名称,或使用默认值 artifactId
    • 公共终结点 - 若要公开应用访问权限,则为 true;否则为 false。
  2. 验证 POM 文件中的 appName 元素是否具有正确的值。 POM 文件的相关部分应类似于以下示例。

    <build>
        <plugins>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-spring-apps-maven-plugin</artifactId>
                <version>1.10.0</version>
                <configuration>
                    <subscriptionId>xxxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx</subscriptionId>
                    <clusterName>v-spr-cld</clusterName>
                    <appName>hellospring</appName>
    

    POM 文件现在包含插件依赖项和配置。

  3. 使用以下命令部署应用。

    mvn azure-spring-apps:deploy
    

验证服务

完成部署后,可通过 https://<service instance name>-hellospring.microservices.azure.cn/ 访问应用。

Screenshot of app in browser.

清理资源

如果打算继续使用示例应用程序,则可能需要保留这些资源。 不再需要时,请删除包含 Azure Spring Apps 实例的资源组。 若要使用 Azure CLI 删除资源组,请使用以下命令:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az group delete --name $resourceGroupName &&
echo "Press [ENTER] to continue ..."

后续步骤