共用方式為


快速入門:使用 WAR 或 JAR 檔案在 Azure Container Apps 中啟動您的第一個 Java 應用程式

本文說明如何使用 Web 應用程式封存 (WAR) 檔案或 Java 封存 (JAR) 檔案,將 Spring PetClinic 範例應用程式部署至 Azure Container Apps。

有數個選項可用於部署 Java 應用程式,包括下列選項:

  • 從本機檔系統或程式代碼存放庫進行部署。
  • 使用 Maven 或 IDE 進行部署。
  • 使用 WAR 檔案、JAR 檔案或直接從原始程式碼進行部署。

在本教學課程結束時,您會部署可透過 Azure 入口網站管理的 Web 應用程式。 下列螢幕快照顯示部署至 Azure Container Apps 之 PetClinic 應用程式的首頁:

PetClinic 應用程式的首頁螢幕快照。

先決條件

建置專案

使用下列步驟建置專案:

  1. 使用下列命令複製 Azure Container Apps Java 範例 存放庫:

    git clone https://github.com/Azure-Samples/azure-container-apps-java-samples.git
    
  1. 使用下列命令複製 Spring PetClinic 範例應用程式 存放庫:

    git clone https://github.com/spring-petclinic/spring-framework-petclinic.git
    
  1. 使用下列命令瀏覽至 spring-petclinic 資料夾:

    cd azure-container-apps-java-samples/spring-petclinic/spring-petclinic/
    
  2. 使用下列命令,將 Spring PetClinic 範例應用程式 存放庫初始化並更新為最新版本:

    git submodule update --init --recursive
    
  3. 使用下列命令清除 Maven 建置區域、編譯項目的程式代碼,以及建立 JAR 檔案,並略過這些程式期間的所有測試:

    mvn clean verify
    

您現在有 /target/petclinic.jar 檔案。

  1. 使用下列命令瀏覽至 spring-framework-petclinic 資料夾:

    cd spring-framework-petclinic
    
  2. 使用下列命令清除 Maven 建置區域、編譯項目的程式代碼,以及建立 JAR 檔案,並略過這些程式期間的所有測試:

    mvn clean verify
    

您現在有 /target/petclinic.war 檔案。

部署專案

使用下列命令將 JAR 套件部署至 Azure Container Apps:

附註

預設的 JDK 版本為 17。 您可以選擇使用環境變數來指定版本。 若要變更 JDK 版本以與您的應用程式相容,請使用 --build-env-vars BP_JVM_VERSION=<your-JDK-version> 自變數。 如需詳細資訊,請參閱在 Azure Container Apps 中建置 Java 的環境變數(預覽版)。

az containerapp up \
    --resource-group <resource-group> \
    --name <container-app-name> \
    --subscription <subscription-ID>\
    --location <location> \
    --environment <environment-name> \
    --artifact <JAR-file-path-and-name> \
    --ingress external \
    --target-port 8080 \
    --query properties.configuration.ingress.fqdn

使用下列命令將 WAR 檔案部署至 Azure Container Apps:

附註

預設的 Tomcat 版本為 9。 若要變更版本以與您的應用程式相容,請使用 --build-env-vars BP_TOMCAT_VERSION=<your-Tomcat-version> 自變數。 在此範例中,通過設置 BP_TOMCAT_VERSION=10.*將 Tomcat 版本設定為 10,包括任何次要版本。 如需詳細資訊,請參閱在 Azure Container Apps 中建置 Java 的環境變數(預覽版)。

az containerapp up \
    --resource-group <resource-group> \
    --name <container-app-name> \
    --subscription <subscription>\
    --location <location> \
    --environment <environment-name> \
    --artifact <WAR-file-path-and-name> \
    --build-env-vars BP_TOMCAT_VERSION=10.* \
    --ingress external \
    --target-port 8080 \
    --query properties.configuration.ingress.fqdn

確認應用程式狀態

在此範例中,containerapp up 命令包含 --query properties.configuration.ingress.fqdn 引數,用於傳回完整功能變數名稱 (FQDN),也稱為應用程式的 URL。

將這個 URL 貼到瀏覽器中即可檢視應用程式。

清理資源

如果您打算繼續使用更多快速入門和教學課程,您可能會想要保留這些資源。 當您不再需要資源時,您可以使用下列命令來移除這些資源,以避免產生 Azure 費用:

az group delete --name <resource-group>