在深入探讨应用程序的逐步执行之前,您可以简单地点击“部署到 Azure”按钮。这将立即将应用程序部署到 Azure Spring Apps。
部署到 Azure Spring Apps | |
消耗计划 | |
基本/标准计划 | |
企业版计划 |
本文带您了解如何将 Spring Boot 应用程序部署到 Azure Spring Apps。
建议您查看 Azure Spring Apps 快速入门文档 以获取相同任务的最新说明。
你将构建什么
您将从 GitHub 克隆一个示例 Spring Boot 应用程序,然后使用 Maven 将其部署到 Azure Spring Apps。
所需准备
为了按照本文的步骤进行操作,需要满足以下先决条件:
-
一个 Azure 订阅。如果您没有 Azure 订阅,可以注册一个免费的 Azure 账户或激活您的MSDN 订阅者权益。
-
一个Java 开发工具包 (JDK),版本 17 或更高。
-
一个Git客户端。
本地构建并运行 Web 应用
在本节中,您将克隆一个 Spring Boot 应用程序并在本地进行测试。
-
打开一个终端窗口。
-
通过输入
mkdir SpringBoot
创建一个本地目录来存放您的 Spring Boot 应用程序。 -
通过输入
cd SpringBoot
切换到该目录。 -
通过输入
git clone https://github.com/spring-guides/gs-spring-boot-for-azure
将 将 Spring Boot 应用程序部署到 Azure 示例项目克隆到您创建的目录中。 -
通过输入
cd gs-spring-boot-for-azure/complete
切换到完成项目的目录。 -
通过输入
./mvnw clean package
使用 Maven 构建 JAR 文件。 -
当 Web 应用程序创建完成后,通过输入
./mvnw spring-boot:run
启动它。 -
通过在另一个终端窗口中访问 http://localhost:8080 或输入
curl http://localhost:8080
进行本地测试。 -
您应该会看到以下消息显示:Hello World。
配置并将应用程序部署到 Azure Spring Apps
配置一个 Azure Spring Apps 实例
-
在网页浏览器中,打开 Azure 门户 并登录您的账户。
-
搜索
Azure Spring Apps
,然后选择Azure Spring Apps
。 -
在概览页面上,选择创建,然后执行以下操作:
-
在服务名称框中,指定您的服务实例名称。名称长度必须为4到32个字符,并且只能包含小写字母、数字和连字符。服务名称的第一个字符必须是字母,最后一个字符必须是字母或数字。
-
在订阅下拉列表中,选择您希望为此资源计费的订阅。
-
在资源组框中,创建一个新的资源组。为新的资源创建资源组是最佳实践。
-
在位置下拉列表中,选择您的服务实例的位置。
-
-
服务部署大约需要5分钟。
添加部署配置
- 使用 Maven Plugin for Azure Spring Apps 通过以下命令配置 Web 应用:
export MAVEN_OPTS="--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED"
./mvnw com.microsoft.azure:azure-spring-apps-maven-plugin:1.18.0:config -DadvancedOptions
-
命令解释:
-
使用
export MAVEN_OPTS="--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED"
可以避免 azure-maven-plugins#2222 中跟踪的问题。 -
这个 Maven 目标会首先与 Azure 进行身份验证,如果您已经使用 Azure CLI 登录,它将使用现有的身份验证令牌。否则,它会自动通过 azure-maven-plugin 让您登录。
-
-
输入必要信息:
-
选择订阅:选择托管您刚刚创建的 Azure Spring Apps 的订阅。
-
选择用于部署的 Azure Spring Apps:选择您刚刚创建的 Azure Spring Apps。
-
输入运行时 Java 版本(Java 11):Java 17
-
是否为此应用程序 boot-for-azure 公开公共访问?(y/N):y
-
对于其他选项,只需选择默认值。
-
-
这是一个示例终端输出:
~@Azure:~/gs-spring-boot/complete$ ./mvnw com.microsoft.azure:azure-spring-apps-maven-plugin:1.14.0:config -DadvancedOptions [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building boot-for-azure 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- azure-spring-apps-maven-plugin:1.14.0:config (default-cli) @ demo --- ... Available subscription: 1. xxx ... Select subscription [1-105] (20): ... Available Azure Spring Apps: 1. xxx ... Select Azure Spring Apps for deployment: [1-28] (1): 1 [INFO] Using Azure Spring Apps: xxx Input the app name (demo): Expose public access for this app boot-for-azure? (y/N):y Summary of properties: Subscription id : xxx Resource group name : xxx Azure Spring Apps name : xxx App name : demo Public access : yes Instance count : 1 CPU count : 1 Memory size(GB) : 1 Runtime Java version : Java 17 Confirm to save all the above configurations (Y/n):Y
- 可选地,打开 pom.xml 文件以查看新添加的内容。
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-apps-maven-plugin</artifactId>
<version>1.14.0</version>
<configuration>
<subscriptionId>xxx</subscriptionId>
<resourceGroup>xxx</resourceGroup>
<clusterName>xxx</clusterName>
<appName>demo</appName>
<isPublic>true</isPublic>
<deployment>
<cpu>1</cpu>
<memoryInGB>1</memoryInGB>
<instanceCount>1</instanceCount>
<runtimeVersion>Java 17</runtimeVersion>
<resources>
<resource>
<filtering/>
<mergeId/>
<targetPath/>
<directory>${project.basedir}/target</directory>
<includes>
<include>*.jar</include>
</includes>
</resource>
</resources>
</deployment>
</configuration>
</plugin>
部署应用程序
- 运行以下命令来部署应用程序:
./mvnw azure-spring-apps:deploy
- 部署可能需要几分钟时间。部署完成后,输出中会显示一个 URL。在 Web 浏览器中导航到该 URL。您应该会看到显示的消息:
Hello World
。
总结
恭喜!您已成功构建并将 Spring Boot 应用程序部署到 Azure Spring Apps。您可以访问 Azure 门户 来管理它。
如果不再需要,别忘了删除创建的 Azure 资源。
使用 AZD 运行
您还可以使用 Azure Developer CLI (azd) 在 Azure Spring Apps 上快速运行此应用程序。Azure Developer CLI 是一个开源工具,可以加速将应用程序从本地开发环境部署到 Azure 的过程。
关于如何使用 azd 运行它,您可以参考 将您的第一个应用程序部署到 Azure Spring Apps。
另请参阅
有关在 Azure 中使用 Spring 的更多信息可在此处获取: