将 Spring 启动应用部署到 Azure_应用程序

本文将指导你将应用程序部署到 Azure Spring 应用。


建议您查看官方 Azure 文档Azure Spring Apps以获取同一任务的最新说明。

您将构建的内容

你将从 GitHub 克隆一个示例 Spring Boot 应用程序,然后使用 Maven 将其部署到 Azure。

你需要什么

要按照本文中的步骤操作,需要满足以下先决条件:

  • Azure 订阅。如果还没有 Azure 订阅,可以注册免费 Azure 帐户或激活您的MSDN 订户权益.
  • 最新的Java 开发工具包 (JDK),版本 1.8 或更高版本。
  • 一个吉特客户。

在本地构建并运行示例 Spring Boot Web 应用程序

在本节中,您将克隆已经编写的 Spring 引导应用程序并在本地对其进行测试:

  1. 打开终端窗口。
  2. 创建一个本地目录来保存您的 Spring 引导应用程序,方法是键入mkdir SpringBoot
  3. 通过键入 更改为该目录。cd SpringBoot
  4. 克隆春季启动入门示例项目到您通过键入创建的目录中git clone https://github.com/spring-guides/gs-spring-boot
  5. 通过键入更改为已完成项目的目录cd gs-spring-boot/complete
  6. 使用 Maven 通过键入来构建 JAR 文件./mvnw clean package
  7. 创建 Web 应用后,通过键入来启动它./mvnw spring-boot:run
  8. 通过访问在本地进行测试http://localhost:8080或从另一个终端窗口键入。curl http://localhost:8080
  9. 您应该会看到显示以下消息:来自春季启动的问候!

配置应用并将其部署到 Azure Spring 应用

  1. 在开始之前,需要预配 Azure Spring 应用群集,例如使用Azure Portal.
  2. 在终端窗口中,使用 配置 Web 应用程序Maven Plugin for Azure Spring Apps通过键入 .此 maven 目标将首先使用 Azure 进行身份验证(如果已使用./mvnw com.microsoft.azure:azure-spring-cloud-maven-plugin:1.3.0:configAzure CLI,它将使用其现有的身份验证令牌。否则,它会让你登录azure-maven-plugin自然而然。
  3. 然后,可以配置部署,在命令提示符下运行 maven 命令,选择刚创建的 Azure Spring 应用群集,接受应用名称的默认值,然后按“y”公开此应用的公共访问权限。当您收到确认 (是/否) 提示时,按“y”,配置完成。
~@Azure:~/gs-spring-boot/complete$ mvn com.microsoft.azure:azure-spring-cloud-maven-plugin:1.3.0:config
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.example:spring-boot >-----------------------
[INFO] Building spring-boot 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- azure-spring-cloud-maven-plugin:1.3.0:config (default-cli) @ spring-boot ---
[INFO] [Correlation ID: xxx] Instance discovery was successful
Available Azure Spring Apps Services:
1. xxx*
Select Azure Spring Apps for deployment: [1-28] (1): 1
[INFO] Using service: xxx
Input the app name (spring-boot):
Expose public access for this app spring-boot? (y/N):y
Summary of properties:
Subscription id : xxx
Service name : xxx
App name : spring-boot
Public access : true
Instance count : 1
CPU count : 1
Memory size(GB) : 1
Runtime Java version : Java 8
Confirm to save all the above configurations (Y/n):
  1. (可选)打开您的pom.xml以查看编写的所有配置。
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-cloud-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<subscriptionId>xxx</subscriptionId>
<clusterName>xxx</clusterName>
<appName>spring-boot</appName>
<isPublic>true</isPublic>
<deployment>
<cpu>1</cpu>
<memoryInGB>1</memoryInGB>
<instanceCount>1</instanceCount>
<runtimeVersion>Java 8</runtimeVersion>
<resources>
<resource>
<filtering/>
<mergeId/>
<targetPath/>
<directory>${project.basedir}/target</directory>
<includes>
<include>*.jar</include>
</includes>
</resource>
</resources>
</deployment>
</configuration>
</plugin>
  1. 配置上述部分中的所有设置后,即可使用 .Maven 会将你的 Web 应用部署到 Azure。可能需要几分钟时间才能通过输出中显示的 URL 访问 Web 应用。在 Web 浏览器中导航到该 URL。您应该会看到显示的消息:来自春季启动的问候!请注意,这个简单的 Spring 启动示例应用程序未使用任何 Spring Cloud 组件(如发现客户端),因此在应用程序启动并正常运行时,注册状态将为关闭。mvn azure-spring-cloud:deploy

总结

祝贺!你生成了 Spring 启动应用并将其部署到 Azure。您可以访问Azure 门户来管理它。

如果不再需要,请不要忘记删除创建的 Azure 资源。