本快速入門說明如何將 Spring Batch 暫時應用程式部署至 Azure Spring Apps。 範例專案衍生自 Spring Batch 範例 足球作業。 這是統計數據載入作業。 在原始範例中,單元測試會觸發作業。 在調整的範例中, main 起始作業的方法 FootballJobApplication 。
本文適用於: ❎基本/標準 ✅ 企業
下圖顯示系統的架構:
本文提供下列部署至 Azure Spring Apps 的選項:
- [Azure 入口網站] 選項是建立資源和部署應用程式的最簡單且最快的方式,只需按兩下即可。 這個選項適用於想要將應用程式快速部署至 Azure 雲端服務的 Spring 開發人員。
-
Azure CLI 選項會使用功能強大的命令行工具來管理 Azure 資源。 此選項適用於熟悉 Azure 雲端服務的 Spring 開發人員。
1.Prerequisites
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 隨用隨付的定價方案。 |
|
條款 |
Selected |
與 Marketplace 供應項目相關聯的合約複選框。 您必須選取此複選框。 |
|
部署範例專案 |
未選取 |
使用內建範例應用程式的選項。 |
選取 [檢閱並建立] 以檢閱您的選取項目。 然後,選取 [建立] 以布建 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}
使用下列命令來列出所有可用的訂用帳戶,以判斷要使用的訂用帳戶識別碼:
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 實例,展開 瀏覽窗格中的 [設定 ],然後選取 [ 應用程式]。
在 [ 應用程式] 窗格中,選取 [建立應用程式 ] 以開啟 [ 建立應用程式 ] 頁面。
將 [應用程式名稱] 設定為足球看板,選取 [系結] 數據行上的 [服務登錄],然後選取 [建立]。
建立應用程式之後,選取 [部署應用程式 ],然後複製 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。 設定作業參數,例如平行處理原則、重試限制和逾時。 視需要新增環境變數和秘密環境變數。 確認之後,選取 [ 建立]。
建立作業之後,展開 瀏覽窗格中的 [受控元件 ],然後選取 [服務登錄]。 然後,依序選取 [作業系結]、[系結作業] 以選取足球作業,然後選取 [套用至系結]。
系結作業之後,返回 [作業] 窗格,選取 [部署作業],然後複製 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. 建立和執行作業
使用下列步驟來建立和執行作業:
使用下列命令,從您先前離開的 足球看板 資料夾瀏覽至範例項目資料夾:
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.後續步驟
如需詳細資訊,請參閱下列文章: