快速入門:使用企業方案建置應用程式並將其部署至 Azure Spring Apps

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但是您暫時還是會在某些位置看到舊的名稱。我們正在致力更新螢幕擷取畫面、影片和圖表等資產。

本文適用於:❌ 基本/標準✔️企業

本快速入門說明如何使用企業方案,建置應用程式並將其部署至 Azure Spring Apps。

必要條件

  • 具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶
  • 了解並滿足 Azure Marketplace 中企業方案的需求一節。
  • Azure CLI 2.45.0 版或更高版本。
  • Git
  • Azure Spring Apps 企業版方案延伸模組。 使用下列命令來移除舊版並安裝最新的 Enterprise 方案擴充功能。 如果您先前已安裝擴充 spring-cloud 功能,請將其卸載,以避免設定和版本不符。
    az extension add --upgrade --name spring
    az extension remove --name spring-cloud
    

下載範例應用程式

使用下列命令來下載範例:

git clone https://github.com/Azure-Samples/acme-fitness-store
cd acme-fitness-store

布建服務實例

使用下列步驟來布建 Azure Spring Apps 服務實例。

  1. 使用下列命令登入 Azure CLI,然後選擇您的使用中訂用帳戶:

    az login
    az account list --output table
    az account set --subscription <subscription-ID>
    
  2. 使用下列命令來接受企業方案的法律條款和隱私聲明。 只有在您的訂用帳戶從未用來建立 Azure Spring Apps 的企業方案實例時,才需要此步驟。

    az provider register --namespace Microsoft.SaaS
    az term accept \
        --publisher vmware-inc \
        --product azure-spring-cloud-vmware-tanzu-2 \
        --plan asa-ent-hr-mtr
    
  3. 選取位置。 此位置必須是支援 Azure Spring Apps 企業版方案的位置。 如需詳細資訊,請參閱 Azure Spring Apps 常見問題

  4. 使用下列命令建立變數來保存資源名稱。 請務必將佔位元取代為您自己的值。 Azure Spring Apps 服務實例的名稱長度必須介於 4 到 32 個字元之間,且只能包含小寫字母、數位和連字元。 服務名稱的第一個字元必須是字母,最後一個字元必須是字母或數位。

    export LOCATION="<location>"
    export RESOURCE_GROUP="<resource-group-name>"
    export SERVICE_NAME="<Azure-Spring-Apps-service-instance-name>"
    export WORKSPACE_NAME="<workspace-name>"
    
  5. 使用下列命令來建立資源群組:

    az group create \
        --name ${RESOURCE_GROUP} \
        --location ${LOCATION}
    

    如需資源群組的詳細資訊,請參閱 什麼是 Azure Resource Manager?

  6. 使用下列命令來建立 Azure Spring Apps 服務實例:

    az spring create \
        --resource-group ${RESOURCE_GROUP} \
        --name ${SERVICE_NAME} \
        --sku enterprise \
        --enable-application-configuration-service \
        --enable-service-registry \
        --enable-gateway \
        --enable-api-portal
    
  7. 使用下列命令建立要用於 Azure Spring Apps 服務的 Log Analytics 工作區:

    az monitor log-analytics workspace create \
        --resource-group ${RESOURCE_GROUP} \
        --workspace-name ${WORKSPACE_NAME} \
        --location ${LOCATION}
    
  8. 使用下列命令來擷取 Log Analytics 工作區和 Azure Spring Apps 服務實例的資源識別碼:

    export LOG_ANALYTICS_RESOURCE_ID=$(az monitor log-analytics workspace show \
        --resource-group ${RESOURCE_GROUP} \
        --workspace-name ${WORKSPACE_NAME} \
        --query id \
        --output tsv)
    
    export AZURE_SPRING_APPS_RESOURCE_ID=$(az spring show \
        --resource-group ${RESOURCE_GROUP} \
        --name ${SERVICE_NAME} \
        --query id \
        --output tsv)
    
  9. 使用下列命令來設定 Azure Spring Apps 服務的診斷設定:

    az monitor diagnostic-settings create \
        --name "send-logs-and-metrics-to-log-analytics" \
        --resource ${AZURE_SPRING_APPS_RESOURCE_ID} \
        --workspace ${LOG_ANALYTICS_RESOURCE_ID} \
        --logs '[
             {
               "category": "ApplicationConsole",
               "enabled": true,
               "retentionPolicy": {
                 "enabled": false,
                 "days": 0
               }
             },
             {
                "category": "SystemLogs",
                "enabled": true,
                "retentionPolicy": {
                  "enabled": false,
                  "days": 0
                }
              },
             {
                "category": "IngressLogs",
                "enabled": true,
                "retentionPolicy": {
                  "enabled": false,
                  "days": 0
                 }
               }
           ]' \
           --metrics '[
             {
               "category": "AllMetrics",
               "enabled": true,
               "retentionPolicy": {
                 "enabled": false,
                 "days": 0
               }
             }
           ]'
    
  10. 使用下列指令建立、、order-servicepayment-servicecatalog-servicefrontend應用程式cart-service

    az spring app create \
        --resource-group ${RESOURCE_GROUP} \
        --name cart-service \
        --service ${SERVICE_NAME}
    
    az spring app create \
        --resource-group ${RESOURCE_GROUP} \
        --name order-service \
        --service ${SERVICE_NAME}
    
    az spring app create \
        --resource-group ${RESOURCE_GROUP} \
        --name payment-service \
        --service ${SERVICE_NAME}
    
    az spring app create \
        --resource-group ${RESOURCE_GROUP} \
        --name catalog-service \
        --service ${SERVICE_NAME}
    
    az spring app create \
        --resource-group ${RESOURCE_GROUP} \
        --name frontend \
        --service ${SERVICE_NAME}
    

使用應用程式組態服務將組態外部化

使用下列步驟來設定應用程式組態服務。

  1. 使用下列命令來建立應用程式群組態服務的組態存放庫:

    az spring application-configuration-service git repo add \
        --resource-group ${RESOURCE_GROUP} \
        --name acme-fitness-store-config \
        --service ${SERVICE_NAME} \
        --label main \
        --patterns "catalog/default,catalog/key-vault,identity/default,identity/key-vault,payment/default" \
        --uri "https://github.com/Azure-Samples/acme-fitness-store-config"
    
  2. 使用下列命令將應用程式系結至應用程式組態服務:

    az spring application-configuration-service bind \
        --resource-group ${RESOURCE_GROUP} \
        --app payment-service \
        --service ${SERVICE_NAME}
    
    az spring application-configuration-service bind \
        --resource-group ${RESOURCE_GROUP} \
        --app catalog-service \
        --service ${SERVICE_NAME}
    

啟用服務註冊和探索

若要使用中的服務註冊和探索,請使用下列命令將應用程式系結至服務登錄:

az spring service-registry bind \
    --resource-group ${RESOURCE_GROUP} \
    --app payment-service \
    --service ${SERVICE_NAME}

az spring service-registry bind \
    --resource-group ${RESOURCE_GROUP} \
    --app catalog-service \
    --service ${SERVICE_NAME}

使用 Tanzu Build Service 部署 polyglot 應用程式

使用下列步驟來部署和建置應用程式。 針對這些步驟,請確定終端機位於專案資料夾中,再執行任何命令。

  1. 使用下列命令在 Tanzu Build Service 中建立自訂建置器:

    az spring build-service builder create \
        --resource-group ${RESOURCE_GROUP} \
        --name quickstart-builder \
        --service ${SERVICE_NAME} \
        --builder-file azure-spring-apps-enterprise/resources/json/tbs/builder.json
    
  2. 使用下列命令來建置及部署付款服務:

    az spring app deploy \
        --resource-group ${RESOURCE_GROUP} \
        --name payment-service \
        --service ${SERVICE_NAME} \
        --config-file-pattern payment/default \
        --source-path apps/acme-payment \
        --build-env BP_JVM_VERSION=17
    
  3. 使用下列命令來建置及部署目錄服務:

    az spring app deploy \
        --resource-group ${RESOURCE_GROUP} \
        --name catalog-service \
        --service ${SERVICE_NAME} \
        --config-file-pattern catalog/default \
        --source-path apps/acme-catalog \
        --build-env BP_JVM_VERSION=17
    
  4. 使用下列命令來建置及部署訂單服務:

    az spring app deploy \
        --resource-group ${RESOURCE_GROUP} \
        --name order-service \
        --service ${SERVICE_NAME} \
        --builder quickstart-builder \
        --source-path apps/acme-order
    
  5. 使用下列命令來建置及部署購物車服務:

    az spring app deploy \
        --resource-group ${RESOURCE_GROUP} \
        --name cart-service \
        --service ${SERVICE_NAME} \
        --builder quickstart-builder \
        --env "CART_PORT=8080" \
        --source-path apps/acme-cart
    
  6. 使用下列命令來建置及部署前端應用程式:

    az spring app deploy \
        --resource-group ${RESOURCE_GROUP} \
        --name frontend \
        --service ${SERVICE_NAME} \
        --source-path apps/acme-shopping
    

提示

若要對部署進行問題,您可以使用下列命令,在應用程式執行時即時取得記錄串流: az spring app logs --name <app name> --follow

使用 Spring Cloud 閘道將要求路由傳送至應用程式

使用下列步驟來設定 Spring Cloud Gateway 並設定應用程式的路由。

  1. 使用下列命令將端點指派給 Spring Cloud Gateway:

    az spring gateway update \
        --resource-group ${RESOURCE_GROUP} \
        --service ${SERVICE_NAME} \
        --assign-endpoint true
    
  2. 使用下列命令來設定 Spring Cloud Gateway API 資訊:

    export GATEWAY_URL=$(az spring gateway show \
        --resource-group ${RESOURCE_GROUP} \
        --service ${SERVICE_NAME} \
        --query properties.url \
        --output tsv)
    
    az spring gateway update \
        --resource-group ${RESOURCE_GROUP} \
        --service ${SERVICE_NAME} \
        --api-description "Fitness Store API" \
        --api-title "Fitness Store" \
        --api-version "v1.0" \
        --server-url "https://${GATEWAY_URL}" \
        --allowed-origins "*"
    
  3. 使用下列命令來建立購物車服務的路線:

    az spring gateway route-config create \
        --resource-group ${RESOURCE_GROUP} \
        --name cart-routes \
        --service ${SERVICE_NAME} \
        --app-name cart-service \
        --routes-file azure-spring-apps-enterprise/resources/json/routes/cart-service.json
    
  4. 使用下列命令來建立訂單服務的路由:

    az spring gateway route-config create \
        --resource-group ${RESOURCE_GROUP} \
        --name order-routes \
        --service ${SERVICE_NAME} \
        --app-name order-service \
        --routes-file azure-spring-apps-enterprise/resources/json/routes/order-service.json
    
  5. 使用下列命令來建立目錄服務的路由:

    az spring gateway route-config create \
        --resource-group ${RESOURCE_GROUP} \
        --name catalog-routes \
        --service ${SERVICE_NAME} \
        --app-name catalog-service \
        --routes-file azure-spring-apps-enterprise/resources/json/routes/catalog-service.json
    
  6. 使用下列命令來建立前端的路由:

    az spring gateway route-config create \
        --resource-group ${RESOURCE_GROUP} \
        --name frontend-routes \
        --service ${SERVICE_NAME} \
        --app-name frontend \
        --routes-file azure-spring-apps-enterprise/resources/json/routes/frontend.json
    
  7. 使用下列命令來擷取 Spring Cloud Gateway 的 URL:

    export GATEWAY_URL=$(az spring gateway show \
        --resource-group ${RESOURCE_GROUP} \
        --service ${SERVICE_NAME} \
        --query properties.url \
        --output tsv)
    
    echo "https://${GATEWAY_URL}"
    

    您可以在瀏覽器中開啟輸出 URL,以探索已部署的應用程式。

使用 API 入口網站瀏覽並試用 API

使用下列步驟來設定 API 入口網站。

  1. 使用下列命令將端點指派給 API 入口網站:

    az spring api-portal update \
        --resource-group ${RESOURCE_GROUP} \
        --service ${SERVICE_NAME} \
        --assign-endpoint true
    
  2. 使用下列命令來擷取 API 入口網站的網址:

    export PORTAL_URL=$(az spring api-portal show \
        --resource-group ${RESOURCE_GROUP} \
        --service ${SERVICE_NAME} \
        --query properties.url \
        --output tsv)
    
    echo "https://${PORTAL_URL}"
    

    您可以在瀏覽器開啟輸出 URL,以探索應用程式 API。


清除資源

如果您打算繼續進行後續的快速入門和教學課程,您可以讓這些資源留在原處。 如果不再需要,請刪除資源群組,這會刪除資源群組中的資源。 若要使用 Azure CLI 刪除資源群組,請使用下列命令:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az group delete --name $resourceGroupName &&
echo "Press [ENTER] to continue ..."

下一步

既然您已成功建置並部署應用程式,請繼續進行下列任何選擇性快速入門: