クイックスタート: アプリをビルドして Azure Spring Apps にデプロイする

注意

Azure Spring Apps は、Azure Spring Cloud サービスの新しい名前です。 サービスの名前は新しくなりましたが、スクリーンショット、ビデオ、図などの資産の更新に取り組んでいる間、場所によってはしばらく古い名前が表示されます。

この記事の適用対象: ✔️ Basic または Standard ❌ Enterprise

このクイックスタートでは、Azure CLI を使用して、Spring アプリケーションをビルドし、Azure Spring Apps にデプロイする方法について説明します。

前提条件

サンプル アプリ をダウンロードする

次のステップを実行し、サンプル アプリをダウンロードします。 Azure Cloud Shell を使用している場合は、ローカル コマンド プロンプトに切り替えます。

  1. 新しいフォルダーを作成し、サンプル アプリ リポジトリを複製します。

    mkdir source-code
    
    cd source-code
    
    git clone https://github.com/Azure-Samples/azure-spring-apps-samples
    
  2. リポジトリ ディレクトリに移動します。

    cd azure-spring-apps-samples
    

PlanetWeatherProvider をデプロイする

次の手順を使用して、PlanetWeatherProvider プロジェクトをデプロイします。

  1. Azure Spring Apps インスタンスの PlanetWeatherProvider プロジェクトにアプリを作成します。

    az spring app create --name planet-weather-provider --runtime-version NetCore_31
    

    自動サービス登録を有効にするために、プロジェクトの appsettings.json ファイル内の spring.application.name の値と同じ名前をアプリに設定しました。

    "spring": {
      "application": {
        "name": "planet-weather-provider"
      }
    }
    

    このコマンドの実行には数分かかることがあります。

  2. ディレクトリを PlanetWeatherProvider プロジェクト フォルダーに変更します。

    cd steeltoe-sample/src/planet-weather-provider
    
  3. デプロイするバイナリと .ZIP ファイルを作成します。

    dotnet publish -c release -o ./publish
    

    ヒント

    プロジェクト ファイルには、バイナリを ./publish フォルダーに書き込んだ後で .ZIP ファイルにパッケージ化するための、以下の XML が含まれています。

    <Target Name="Publish-Zip" AfterTargets="Publish">
      <ZipDirectory SourceDirectory="$(PublishDir)" DestinationFile="$(MSBuildProjectDirectory)/publish-deploy-planet.zip" Overwrite="true" />
    </Target>
    
  4. Azure にプロジェクトをデプロイします。

    次のコマンドを実行する前に、コマンド プロンプトがプロジェクト フォルダーにあることを確認してください。

    az spring app deploy \
        --name planet-weather-provider \
        --runtime-version NetCore_31 \
        --main-entry Microsoft.Azure.SpringCloud.Sample.PlanetWeatherProvider.dll \
        --artifact-path ./publish-deploy-planet.zip
    

    --main-entry オプションには、 .ZIP ファイルのルート フォルダーからアプリケーションのエントリ ポイントを含む .dll ファイルへの相対パスを指定します。 サービスは、.ZIP ファイルをアップロードした後、すべてのファイルとフォルダーを抽出してから、指定された .dll ファイル内のエントリ ポイントを実行しようとします。

    このコマンドの実行には数分かかることがあります。

SolarSystemWeather をデプロイする

次の手順を使用して、SolarSystemWeather プロジェクトをデプロイします。

  1. プロジェクトの Azure Spring Apps インスタンスに別のアプリを作成します。

    az spring app create --name solar-system-weather --runtime-version NetCore_31
    

    solar-system-weather は、SolarSystemWeather プロジェクトの appsettings.json ファイルに指定されている名前です。

    このコマンドの実行には数分かかることがあります。

  2. ディレクトリを SolarSystemWeather プロジェクトに変更します。

    cd ../solar-system-weather
    
  3. デプロイするバイナリと .ZIP ファイルを作成します。

    dotnet publish -c release -o ./publish
    
  4. Azure にプロジェクトをデプロイします。

    az spring app deploy \
        --name solar-system-weather \
        --runtime-version NetCore_31 \
        --main-entry Microsoft.Azure.SpringCloud.Sample.SolarSystemWeather.dll \
        --artifact-path ./publish-deploy-solar.zip
    

    このコマンドの実行には数分かかることがあります。

パブリック エンドポイントを割り当てる

アプリケーションをテストする前に、solar-system-weather アプリケーションに対する HTTP GET 要求のパブリック エンドポイントを取得します。

  1. 次のコマンドを実行してエンドポイントを割り当てます。

    az spring app update --name solar-system-weather --assign-endpoint true
    
  2. 次のコマンドを実行し、エンドポイントの URL を取得します。

    Windows:

    az spring app show --name solar-system-weather --output table
    

    Linux:

    az spring app show --name solar-system-weather | grep url
    

アプリケーションをテストする

アプリケーションをテストするには、GET 要求を solar-system-weather アプリに送信します。 ブラウザーで、パブリック URL に /weatherforecast を追加し、そこに移動します。 例: https://servicename-solar-system-weather.azuremicroservices.io/weatherforecast

出力は JSON です。

[{"Key":"Mercury","Value":"very warm"},{"Key":"Venus","Value":"quite unpleasant"},{"Key":"Mars","Value":"very cool"},{"Key":"Saturn","Value":"a little bit sandy"}]

この応答は、両方の Spring アプリが動作していることを示しています。 SolarSystemWeather アプリは、PlanetWeatherProvider アプリから取得したデータを返します。

この記事では、Spring アプリケーションをビルドし、Azure Spring Apps にデプロイする方法について説明します。 Azure CLI、Maven プラグイン、または Intellij を使用できます。 この記事では、それぞれの方法について説明します。

前提条件

Spring アプリケーションをローカルでビルドする

次のコマンドを使用して、サンプル リポジトリをクローンし、サンプル フォルダーに移動してからプロジェクトをビルドします。

git clone https://github.com/azure-samples/spring-petclinic-microservices
cd spring-petclinic-microservices
mvn clean package -DskipTests -Denv=cloud

プロジェクトのコンパイルには、5 から 10 分かかります。 プロジェクトがコンパイルされると、各サービスのそれぞれのフォルダーに個別の JAR ファイルが作成されます。

アプリを作成して Azure Spring Apps にデプロイする

CLI を使用してアプリを作成して Azure Spring Apps にデプロイするには、次の手順を使用します。

  1. 以前のクイックスタートで次のコマンドを実行していない場合は、ここで実行して CLI の既定値を設定します。

    az configure --defaults group=<resource-group-name> spring=<service-name>
    
  2. PetClinic の 2 つのコア Spring アプリケーション (api-gatewaycustomers-service) を作成します。

    az spring app create \
        --name api-gateway \
        --runtime-version Java_17 \
        --instance-count 1 \
        --memory 2Gi \
        --assign-endpoint
    az spring app create \
        --name customers-service \
        --runtime-version Java_17 \
        --instance-count 1 \
        --memory 2Gi
    
  3. 前の手順で作成された JAR ファイルをデプロイします。

    az spring app deploy \
        --name api-gateway \
        --artifact-path spring-petclinic-api-gateway/target/api-gateway-3.0.1.jar \
        --jvm-options="-Xms2048m -Xmx2048m"
    az spring app deploy \
        --name customers-service \
        --artifact-path spring-petclinic-customers-service/target/customers-service-3.0.1.jar \
        --jvm-options="-Xms2048m -Xmx2048m"
    
  4. 次のコマンドを使用して、デプロイ後のアプリの状態を照会します。

    az spring app list --output table
    

    次の例のような出力が表示されます。

    Name               Location    ResourceGroup    Production Deployment    Public Url                                           Provisioning Status    CPU    Memory    Running Instance    Registered Instance    Persistent Storage
    -----------------  ----------  ---------------  -----------------------  ---------------------------------------------------  ---------------------  -----  --------  ------------------  ---------------------  --------------------
    api-gateway        eastus      xxxxxx-sp         default                  https://<service name>-api-gateway.azuremicroservices.io   Succeeded              1      2         1/1                 1/1                    -
    customers-service  eastus      <service name>         default                                                                       Succeeded              1      2         1/1                 1/1                    -
    

サービスを確認する

先ほど表示したパブリック URL (https://<service name>-api-gateway.azuremicroservices.io の形式) を使用して、ブラウザーから api-gatewaycustomers-service にアクセスします。

[所有者] ページを示す PetClinic サンプル アプリのスクリーンショット。

ヒント

デプロイのトラブルシューティングを行うために、az spring app logs --name <app name> --follow コマンドを使用して、アプリが実行されているときにログ ストリーミングをリアルタイムで取得できます。

追加のアプリをデプロイする

管理サーバー、訪問、獣医などのすべての機能を使用して PetClinic アプリを機能させるには、次のコマンドを使用して他のアプリをデプロイします。

az spring app create \
    --name admin-server \
    --runtime-version Java_17 \
    --instance-count 1 \
    --memory 2Gi \
    --assign-endpoint
az spring app create \
    --name vets-service \
    --runtime-version Java_17 \
    --instance-count 1 \
    --memory 2Gi
az spring app create \
    --name visits-service \
    --runtime-version Java_17 \
    --instance-count 1 \
    --memory 2Gi
az spring app deploy \
    --name admin-server \
    --runtime-version Java_17 \
    --artifact-path spring-petclinic-admin-server/target/admin-server-3.0.1.jar \
    --jvm-options="-Xms1536m -Xmx1536m"
az spring app deploy \
    --name vets-service \
    --runtime-version Java_17 \
    --artifact-path spring-petclinic-vets-service/target/vets-service-3.0.1.jar \
    --jvm-options="-Xms1536m -Xmx1536m"
az spring app deploy \
    --name visits-service \
    --runtime-version Java_17 \
    --artifact-path spring-petclinic-visits-service/target/visits-service-3.0.1.jar \
    --jvm-options="-Xms1536m -Xmx1536m"

リソースをクリーンアップする

後続のクイック スタートおよびチュートリアルを引き続き実行する場合は、これらのリソースをそのまま残しておくことができます。 不要になったら、リソース グループを削除します。これにより、リソース グループ内のリソースが削除されます。 Azure CLI を使用してリソース グループを削除するには、次のコマンドを使用します。

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

次のステップ