本快速入门介绍如何将 Spring Batch 临时应用程序部署到 Azure Spring Apps。 示例项目源自 Spring Batch 示例 Football Job。 它是一个统计加载作业。 在原始示例中,单元测试会触发该作业。 在改编的示例中,main 的 FootballJobApplication 方法启动该作业。
本文适用于: ❎ 基本计划/标准计划 ✅ 企业计划
下图显示了系统的体系结构:
本文介绍用于部署到 Azure Spring Apps 的以下选项:
- “Azure 门户”选项是创建资源和部署应用程序的最简单、最快的方式,只需点击一下即可。 此选项适用于需要将应用程序快速部署到 Azure 云服务的 Spring 开发人员。
- “Azure CLI”选项使用功能强大的命令行工具管理 Azure 资源。 此选项适合熟悉 Azure 云服务的 Spring 开发人员。
1.先决条件
2. 准备 Spring 项目
使用以下命令从 GitHub 克隆示例项目:
git clone https://github.com/Azure-Samples/azure-spring-apps-samples.git
3. 准备云环境
运行此示例所需的主要资源是 Azure Spring Apps 实例。 本部分提供创建此资源的步骤。
3.1. 登录到 Azure 门户
转到 Azure 门户,输入登录到门户所需的凭据。 默认视图是服务仪表板。
3.2. 创建 Azure Spring Apps 实例
使用以下步骤创建 Azure Spring Apps 服务实例:
在 Azure 门户一角选择“创建资源”。
选择“计算>Azure Spring Apps”。
填写“基本”表单,其中包含以下信息:
| 设置 |
建议的值 |
描述 |
|
订阅 |
你的订阅名称。 |
要用于服务器的 Azure 订阅。 如果你有多个订阅,请选择要计费的资源所在的订阅。 |
|
资源组 |
myresourcegroup |
新的资源组名称,或订阅中的现有资源组。 |
|
名称 |
myasa |
用于标识 Azure Spring Apps 服务的唯一名称。 该名称必须为 4 到 32 个字符,只能包含小写字母、数字及连字符。 服务名称的第一个字符必须是字母,最后一个字符必须是字母或数字。 |
|
计划 |
企业 |
定价计划确定与实例关联的资源和成本。 |
|
区域 |
离用户最近的区域。 |
最靠近用户的位置。 |
|
区域冗余 |
未选定 |
在 Azure 可用性区域中创建 Azure Spring Apps 服务的选项。 此功能目前在所有区域都不受支持。 |
|
软件 IP 计划 |
即用即付 |
使用 Azure Spring Apps 进行即用即付的定价计划。 |
|
Terms |
已选择 |
与市场产品/服务关联的协议复选框。 需要选中此复选框。 |
|
部署示例项目 |
未选定 |
使用内置示例应用程序的选项。 |
选择“查看 + 创建”,查看所选内容。 然后,选择“创建”以预配 Azure Spring Apps 实例。
在工具栏上选择“通知”图标(铃铛)以监视部署过程。 完成部署后,可以选择“固定到仪表板”,以便在 Azure 门户仪表板上为此服务创建磁贴作为前往此服务的“概述”页的快捷方式。
选择“转到资源”,转到“Azure Spring Apps 概述”页面。
3.3. 启用服务注册表
转到创建的 Azure Spring Apps 实例,在导航窗格中展开“托管组件”,然后选择“服务注册表”。 然后,在“概述”页面上,选择“管理”打开“管理”页面,选择“启用服务注册表”,然后选择“应用”。
3.4. 设置 Log Analytics 工作区
有关在日志中查询数据的信息,请参阅快速入门:设置 Log Analytics 工作区。
3.1. 提供每个资源的名称
使用以下命令创建变量以保存资源名称。 请务必将占位符替换为你自己的值。
export LOCATION=<location>
export RESOURCE_GROUP=<resource-group-name>
export AZURE_SPRING_APPS_INSTANCE=<Azure-Spring-Apps-instance-name>
3.2. 创建新的资源组
按照以下步骤创建新的资源组:
使用以下命令登录到 Azure CLI:
az login
使用以下命令设置默认位置:
az configure --defaults location=${LOCATION}
使用以下命令列出所有可用的订阅,以确定要使用的订阅 ID:
az account list --output table
使用以下命令设置默认订阅:
az account set --subscription <subscription-ID>
使用以下命令来创建资源组:
az group create --resource-group ${RESOURCE_GROUP}
使用以下命令将新创建的资源组设置为默认资源组:
az configure --defaults group=${RESOURCE_GROUP}
3.3. 创建 Azure Spring Apps 实例
Azure Spring Apps 用于托管 Spring Web 应用。 使用以下步骤创建 Azure Spring Apps 实例,并在其中创建一个应用程序:
使用以下命令创建 Azure Spring Apps 服务实例并启用服务注册表:
az spring create \
--name ${AZURE_SPRING_APPS_INSTANCE} \
--sku enterprise \
--enable-sr
使用以下命令验证 Azure Spring Apps 企业计划服务实例是否已成功创建:
az spring show --name ${AZURE_SPRING_APPS_INSTANCE}
使用以下步骤部署应用:
转到创建的 Azure Spring Apps 实例,展开导航窗格中的“设置”,然后选择“应用”。
在“应用”窗格中,选择“创建应用”打开“创建应用”页面。
将“应用名称”设置为 football-billboard,在“绑定”列中选择“服务注册表”,然后选择“创建”。
创建应用后,选择“部署应用”并复制用于部署应用的 Azure CLI 命令。 然后,打开 Bash 窗口并将命令粘贴到命令行中(请将命令中的项目路径替换为适用于你的系统的正确值)。 然后运行该命令。 等待几分钟,直到生成和部署成功。 该命令及其输出应如以下示例所示:
$ az spring app deploy -s job-demo -g job-demo -n football-billboard --artifact-path target/spring-batch-football-billboard-0.0.1-SNAPSHOT.jar
This command usually takes minutes to run. Add '--verbose' parameter if needed.
[1/5] Requesting for upload URL.
[2/5] Uploading package to blob.
[3/5] Creating or Updating build 'football-billboard'.
[4/5] Waiting for building container image to finish. This may take a few minutes.
部署完成后,返回“应用”窗格并选择 football-billboard 应用。 然后,转到概述页面并选择“分配终结点”以公开应用的公共终结点。
使用以下步骤部署应用:
使用以下命令从存储库的根文件夹导航到示例项目文件夹:
cd azure-spring-apps-samples/job-samples/football-billboard
使用以下命令在 Azure Spring Apps 实例中创建应用程序并绑定服务注册表:
az spring app create \
--service ${AZURE_SPRING_APPS_INSTANCE} \
--name football-billboard \
--assign-endpoint true \
--bind-sr
使用以下命令部署应用:
az spring app deploy \
--service ${AZURE_SPRING_APPS_INSTANCE} \
--name football-billboard \
--source-path . \
--build-env BP_JVM_VERSION=17
5.将作业示例部署到 Azure Spring Apps
本部分提供部署示例的步骤。
5.1. 创建和执行作业
使用以下步骤创建和执行作业:
导航到“作业”窗格,然后选择“创建作业”。 填写 football 作为作业名称。 配置作业参数,例如并行度、重试限制和超时。 根据需要添加环境变量和机密环境变量。 确认后,选择“创建”。
创建作业后,展开导航窗格中的“托管组件”,然后选择“服务注册表”。 然后,选择“作业绑定”,选择“绑定作业”以选择 football 作业,然后选择“应用”进行绑定。
绑定作业后,返回“作业”窗格,选择“部署作业”,然后复制用于部署作业的 Azure CLI 命令。 然后,打开 Bash 窗口并将命令粘贴到命令行中(请将命令中的项目路径替换为适用于你的系统的正确值)。 然后运行该命令。 等待几分钟,直到生成和部署成功。 该命令及其输出应如以下示例所示:
$ az spring job deploy -s job-demo -g job-demo -n football --artifact-path target/spring-batch-football-0.0.1-SNAPSHOT-jar-with-dependencies.jar --build-env BP_JVM_VERSION=17
This command is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
This command usually takes minutes to run. Add '--verbose' parameter if needed.
[1/5] Requesting for upload URL.
[2/5] Uploading package to blob.
[3/5] Creating or Updating build 'football'.
[4/5] Waiting for building container image to finish. This may take a few minutes.
部署后,通过选择作业名称打开 football 作业的概述页面。 选择“运行”以启动任务执行。 可以使用不同的参数(例如环境变量)自定义作业的每次执行,或者直接选择“运行”来触发执行。 此时会出现一条消息,指出作业正在运行。
5.1. 创建和执行作业
使用以下步骤创建和执行作业:
使用以下命令从之前退出的 football-billboard 文件夹导航到示例项目文件夹:
cd ..
cd football
使用以下命令在 Azure Spring Apps 实例中创建作业并绑定服务注册表:
az spring job create \
--service ${AZURE_SPRING_APPS_INSTANCE} \
--name football \
--bind-sr
使用以下命令将 football 示例项目部署到作业。 此命令在 Azure 中上传并编译源代码,并使其准备启动。
az spring job deploy \
--service ${AZURE_SPRING_APPS_INSTANCE} \
--name football \
--source-path . \
--build-env BP_JVM_VERSION=17
使用以下命令启动作业,并将 EXECUTION_NAME 变量设置为作业执行名称:
export EXECUTION_NAME=$(az spring job start \
--service ${AZURE_SPRING_APPS_INSTANCE} \
--name football \
--query name \
--output tsv)
6.检查作业执行结果和标牌 UI
现在可以访问作业的执行并检查其结果。
按照以下步骤来验证:
在“执行”窗格中,检查作业执行结果。 等待几秒钟,然后刷新,随即会看到状态变为“已完成”。 此值表示作业执行成功完成。
选择“查看日志”以查询作业执行的日志。
在浏览器窗口中打开应用的公共终结点以查看标牌 UI。 让应用保持打开状态。
返回 football 作业的概述页面,并选择“运行”以再次触发执行。
返回浏览器中的终结点页面,并选择“刷新”以查看 UI 的变化,如以下屏幕截图所示:
按照以下步骤来验证:
使用以下命令按照作业名称及其执行名称查询执行结果:
az spring job logs \
--service ${AZURE_SPRING_APPS_INSTANCE} \
--name football \
--execution ${EXECUTION_NAME} \
--all-instances
流式处理作业执行的作业日志应如以下示例所示:
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - No TaskExecutor has been set, defaulting to synchronous executor.
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO com.microsoft.sample.FootballJobApplication - There is {} player summary before job execution
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [SimpleJob: [name=footballJob]] launched with the following parameters: [{}]
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.batch.core.job.SimpleStepHandler - Executing step: [playerLoad]
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.batch.core.step.AbstractStep - Step: [playerLoad] executed in 200ms
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.batch.core.job.SimpleStepHandler - Executing step: [gameLoad]
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.batch.core.step.AbstractStep - Step: [gameLoad] executed in 290ms
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.batch.core.job.SimpleStepHandler - Executing step: [summarizationStep]
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.batch.core.step.AbstractStep - Step: [summarizationStep] executed in 7ms
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [SimpleJob: [name=footballJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 519ms
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO com.microsoft.sample.FootballJobApplication - There is 1 player summary after job execution
[football-xxxxxxxxxxxxx-xxxxxx-x-xxxxx] [main] INFO org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory - Shutting down embedded database: url='jdbc:hsqldb:mem:testdb'
...
使用以下命令检索 football-billboard 应用的终结点:
az spring app show \
--service ${AZURE_SPRING_APPS_INSTANCE} \
--name football-billboard \
--query properties.url
在浏览器窗口中打开应用的公共终结点以查看标牌 UI。 让应用保持打开状态。
使用以下命令再次启动作业:
az spring job start \
--service ${AZURE_SPRING_APPS_INSTANCE} \
--name football
返回浏览器中的终结点页面,并选择“刷新”以查看 UI 的变化,如以下屏幕截图所示:
7.清理资源
当不再需要在本文中创建的资源时,请务必将其删除。 可以删除 Azure 资源组,其中包括资源组中的所有资源。
使用以下步骤删除整个资源组,包括新创建的服务实例:
在 Azure 门户中查找资源组。 在导航菜单上,选择“资源组”,然后选择资源组的名称。
在“资源组”页上,选择“删除”。 在文本框中输入资源组的名称以确认删除,然后选择“删除”。
使用以下命令删除整个资源组(包括新创建的服务实例):
az group delete --name ${RESOURCE_GROUP}
8.后续步骤
有关详细信息,请参阅以下文章: