次の方法で共有


チュートリアル: Dapr Publish と Subscribe を使用したマイクロサービス通信

このチュートリアルでは、次のことについて説明します。

  • Dapr pub/sub API を活用して、イベント ドリブン アーキテクチャのメッセージを使用して通信するパブリッシャー マイクロサービスとサブスクライバー マイクロサービスを作成します。
  • 提供されている Bicep を使用して、Azure Developer CLI を介して Azure Container Apps にアプリケーションをデプロイします。

サンプルの pub/sub プロジェクトには、次のものが含まれます。

  1. 特定のトピックのメッセージを生成するメッセージ ジェネレーター (パブリッシャー) checkout サービス。
  2. 特定のトピックの checkout サービスからのメッセージをリッスンする (サブスクライバー) order-processor サービス。

Diagram of the Dapr pub/sub sample.

前提条件

Node.js アプリケーションをローカルで実行する

Azure Container Apps にアプリケーションをデプロイする前に、Dapr と Azure Service Bus を使用して order-processor サービスと checkout サービスをローカルで実行します。

プロジェクトを準備する

  1. サンプル Dapr アプリケーションをローカル コンピューターに複製します。

    git clone https://github.com/Azure-Samples/pubsub-dapr-nodejs-servicebus.git
    
  2. サンプルのルート ディレクトリに移動します。

    cd pubsub-dapr-nodejs-servicebus
    

Dapr CLI を使用して Dapr アプリケーションを実行する

まず、Dapr を使用して order-processor サブスクライバー サービスを実行することから始めます。

  1. サンプルのルート ディレクトリから、ディレクトリを order-processor に変更します。

    cd order-processor
    
  2. 依存関係をインストールします。

    npm install
    
  3. Dapr を使用して order-processor サービスを実行します。

    dapr run --app-port 5001 --app-id order-processing --app-protocol http --dapr-http-port 3501 --resources-path ../components -- npm run start
    
  4. 新しいターミナル ウィンドウで、サンプルのルート ディレクトリから checkout パブリッシャー サービスに移動します。

    cd checkout
    
  5. 依存関係をインストールします。

    npm install
    
  6. Dapr を使用して checkout サービスを実行します。

    dapr run --app-id checkout --app-protocol http --resources-path ../components -- npm run start
    

    想定される出力

    どちらのターミナルでも、checkout サービスは終了する前に order-processor サービスが受信した 10 個のメッセージを発行します。

    checkout 出力:

    == APP == Published data: {"orderId":1}
    == APP == Published data: {"orderId":2}
    == APP == Published data: {"orderId":3}
    == APP == Published data: {"orderId":4}
    == APP == Published data: {"orderId":5}
    == APP == Published data: {"orderId":6}
    == APP == Published data: {"orderId":7}
    == APP == Published data: {"orderId":8}
    == APP == Published data: {"orderId":9}
    == APP == Published data: {"orderId":10}
    

    order-processor 出力:

    == APP == Subscriber received: {"orderId":1}
    == APP == Subscriber received: {"orderId":2}
    == APP == Subscriber received: {"orderId":3}
    == APP == Subscriber received: {"orderId":4}
    == APP == Subscriber received: {"orderId":5}
    == APP == Subscriber received: {"orderId":6}
    == APP == Subscriber received: {"orderId":7}
    == APP == Subscriber received: {"orderId":8}
    == APP == Subscriber received: {"orderId":9}
    == APP == Subscriber received: {"orderId":10}
    
  7. 次のコマンドを実行して、両方のアプリケーションが停止していることを確認します。 チェックアウト ターミナルで、次の手順を実行します。

    dapr stop --app-id checkout
    

    オーダー プロセッサー ターミナルで、次の手順を実行します。

    dapr stop --app-id order-processor
    

Azure Developer CLI を使用して Dapr アプリケーション テンプレートをデプロイする

azd を使用して、Dapr アプリケーションを Azure Container Apps にデプロイします。

プロジェクトを準備する

新しいターミナル ウィンドウで、サンプルのルート ディレクトリに移動します。

cd pubsub-dapr-nodejs-servicebus

Azure Developer CLI を使用したプロビジョニングとデプロイ

  1. azd init を実行してプロジェクトを初期化します。

    azd init
    
  2. ターミナルでメッセージが表示されたら、次のパラメーターを指定します。

    パラメーター 説明
    Environment Name すべての Azure リソースを保持するために作成されたリソース グループのプレフィックス。
    Azure の場所 リソースの Azure の場所。
    Azure サブスクリプション リソースの Azure サブスクリプション。
  3. azd up を実行して、1 つのコマンドでインフラストラクチャをプロビジョニングして Dapr アプリケーションを Azure Container Apps にデプロイします。

    azd up
    

    この処理は、完了までに時間がかかる場合があります。 azd up コマンドが完了すると、CLI 出力に、デプロイの進行状況を監視するための 2 つの Azure portal リンクが表示されます。 また、出力では、次の azd up の方法も示されます。

    • azd provision を使用して、./infra ディレクトリ内の指定されている Bicep ファイルを使用して、必要なすべての Azure リソースを作成して構成します。 Azure Developer CLI によってプロビジョニングされると、Azure portal からこれらのリソースにアクセスできます。 Azure リソースをプロビジョニングするファイルは次のとおりです。
      • main.parameters.json
      • main.bicep
      • 機能別に整理された app リソース ディレクトリ
      • azd テンプレートで使用される Bicep モジュールを含む core リファレンス ライブラリ
    • azd deploy を使用したコードのデプロイ

    想定される出力

    Initializing a new project (azd init)
    
    
    Provisioning Azure resources (azd provision)
    Provisioning Azure resources can take some time
    
      You can view detailed progress in the Azure Portal:
      https://portal.azure.com
    
      (✓) Done: Resource group: resource-group-name
      (✓) Done: Application Insights: app-insights-name
      (✓) Done: Portal dashboard: portal-dashboard-name
      (✓) Done: Log Analytics workspace: log-analytics-name
      (✓) Done: Key vault: key-vault-name
      (✓) Done: Container Apps Environment: ca-env-name
      (✓) Done: Container App: ca-checkout-name
      (✓) Done: Container App: ca-orders-name
    
    
    Deploying services (azd deploy)
    
      (✓) Done: Deploying service checkout
      (✓) Done: Deploying service orders
      - Endpoint: https://ca-orders-name.endpoint.region.azurecontainerapps.io/
    
    SUCCESS: Your Azure app has been deployed!
    You can view the resources created under the resource group resource-group-name in Azure Portal:
    https://portal.azure.com/#@/resource/subscriptions/subscription-id/resourceGroups/resource-group-name/overview
    

デプロイが成功したことを確認する

Azure portal で、checkout サービスが Azure Service Bus トピックにメッセージを公開していることを確認します。

  1. ターミナル出力から checkout コンテナー アプリ名をコピーします。

  2. Azure portal にサインインし、名前でコンテナー アプリ リソースを検索します。

  3. Container Apps ダッシュボードで、[監視]>[ログ ストリーム] を選択します。

    Screenshot of navigating to the Log stream page in the Azure portal.

  4. checkout コンテナーが先ほどのターミナルと同じ出力をログに記録しているかどうかを確認します。

    Screenshot of the checkout service container's log stream in the Azure portal.

  5. order-processor サービスに対して同じことを行います。

    Screenshot of the order processor service container's log stream in the Azure portal.

何が起きましたか?

azd up コマンドが正常に完了した場合、次のようになります。

  • Azure Developer CLI は、サンプル プロジェクトの ./infra ディレクトリで参照されている Azure リソースを、指定した Azure サブスクリプションにプロビジョニングしました。 これにより Azure portal から、これらの Azure リソースを表示できるようになりました。
  • Azure Container Apps にデプロイされたアプリ。 ポータルから、完全に機能するアプリを参照できます。

Python アプリケーションをローカルで実行する

Azure Container Apps にアプリケーションをデプロイする前に、Dapr と Azure Service Bus を使用して order-processor サービスと checkout サービスをローカルで実行します。

プロジェクトを準備する

  1. サンプル Dapr アプリケーションをローカル コンピューターに複製します。

    git clone https://github.com/Azure-Samples/pubsub-dapr-python-servicebus.git
    
  2. サンプルのルート ディレクトリに移動します。

    cd pubsub-dapr-python-servicebus
    

Dapr CLI を使用して Dapr アプリケーションを実行する

まず、Dapr を使用して order-processor サブスクライバー サービスを実行することから始めます。

  1. サンプルのルート ディレクトリから、ディレクトリを order-processor に変更します。

    cd order-processor
    
  2. 依存関係をインストールします。

    pip3 install -r requirements.txt
    
  3. Dapr を使用して order-processor サービスを実行します。

    dapr run --app-id order-processor --resources-path ../components/ --app-port 5001 -- python3 app.py
    
  4. 新しいターミナル ウィンドウで、サンプルのルート ディレクトリから checkout パブリッシャー サービスに移動します。

    cd checkout
    
  5. 依存関係をインストールします。

    pip3 install -r requirements.txt
    
  6. Dapr を使用して checkout サービスを実行します。

    dapr run --app-id checkout --resources-path ../components/ -- python3 app.py
    

    想定される出力

    どちらのターミナルでも、checkout サービスは終了する前に order-processor サービスが受信した 10 個のメッセージを発行します。

    checkout 出力:

    == APP == Published data: {"orderId":1}
    == APP == Published data: {"orderId":2}
    == APP == Published data: {"orderId":3}
    == APP == Published data: {"orderId":4}
    == APP == Published data: {"orderId":5}
    == APP == Published data: {"orderId":6}
    == APP == Published data: {"orderId":7}
    == APP == Published data: {"orderId":8}
    == APP == Published data: {"orderId":9}
    == APP == Published data: {"orderId":10}
    

    order-processor 出力:

    == APP == Subscriber received: {"orderId":1}
    == APP == Subscriber received: {"orderId":2}
    == APP == Subscriber received: {"orderId":3}
    == APP == Subscriber received: {"orderId":4}
    == APP == Subscriber received: {"orderId":5}
    == APP == Subscriber received: {"orderId":6}
    == APP == Subscriber received: {"orderId":7}
    == APP == Subscriber received: {"orderId":8}
    == APP == Subscriber received: {"orderId":9}
    == APP == Subscriber received: {"orderId":10}
    
  7. 次のコマンドを実行して、両方のアプリケーションが停止していることを確認します。 チェックアウト ターミナルで、次の手順を実行します。

    dapr stop --app-id checkout
    

    オーダー プロセッサー ターミナルで、次の手順を実行します。

    dapr stop --app-id order-processor
    

Azure Developer CLI を使用して Dapr アプリケーション テンプレートをデプロイする

azd を使用して、Dapr アプリケーションを Azure Container Apps にデプロイします。

プロジェクトを準備する

新しいターミナル ウィンドウで、サンプルのルート ディレクトリに移動します。

cd pubsub-dapr-python-servicebus

Azure Developer CLI を使用したプロビジョニングとデプロイ

  1. azd init を実行してプロジェクトを初期化します。

    azd init
    
  2. ターミナルでメッセージが表示されたら、次のパラメーターを指定します。

    パラメーター 説明
    Environment Name すべての Azure リソースを保持するために作成されたリソース グループのプレフィックス。
    Azure の場所 リソースの Azure の場所。
    Azure サブスクリプション リソースの Azure サブスクリプション。
  3. azd up を実行して、1 つのコマンドでインフラストラクチャをプロビジョニングして Dapr アプリケーションを Azure Container Apps にデプロイします。

    azd up
    

    この処理は、完了までに時間がかかる場合があります。 azd up コマンドが完了すると、CLI 出力に、デプロイの進行状況を監視するための 2 つの Azure portal リンクが表示されます。 また、出力では、次の azd up の方法も示されます。

    • azd provision を使用して、./infra ディレクトリ内の指定されている Bicep ファイルを使用して、必要なすべての Azure リソースを作成して構成します。 Azure Developer CLI によってプロビジョニングされると、Azure portal からこれらのリソースにアクセスできます。 Azure リソースをプロビジョニングするファイルは次のとおりです。
      • main.parameters.json
      • main.bicep
      • 機能別に整理された app リソース ディレクトリ
      • azd テンプレートで使用される Bicep モジュールを含む core リファレンス ライブラリ
    • azd deploy を使用したコードのデプロイ

    想定される出力

    Initializing a new project (azd init)
    
    
    Provisioning Azure resources (azd provision)
    Provisioning Azure resources can take some time
    
      You can view detailed progress in the Azure Portal:
      https://portal.azure.com
    
      (✓) Done: Resource group: resource-group-name
      (✓) Done: Application Insights: app-insights-name
      (✓) Done: Portal dashboard: portal-dashboard-name
      (✓) Done: Log Analytics workspace: log-analytics-name
      (✓) Done: Key vault: key-vault-name
      (✓) Done: Container Apps Environment: ca-env-name
      (✓) Done: Container App: ca-checkout-name
      (✓) Done: Container App: ca-orders-name
    
    
    Deploying services (azd deploy)
    
      (✓) Done: Deploying service checkout
      (✓) Done: Deploying service orders
      - Endpoint: https://ca-orders-name.endpoint.region.azurecontainerapps.io/
    
    SUCCESS: Your Azure app has been deployed!
    You can view the resources created under the resource group resource-group-name in Azure Portal:
    https://portal.azure.com/#@/resource/subscriptions/subscription-id/resourceGroups/resource-group-name/overview
    

デプロイが成功したことを確認する

Azure portal で、checkout サービスが Azure Service Bus トピックにメッセージを公開していることを確認します。

  1. ターミナル出力から checkout コンテナー アプリ名をコピーします。

  2. Azure portal にサインインし、名前でコンテナー アプリ リソースを検索します。

  3. Container Apps ダッシュボードで、[監視]>[ログ ストリーム] を選択します。

    Screenshot of navigating to the Log stream page in the Azure portal.

  4. checkout コンテナーが先ほどのターミナルと同じ出力をログに記録しているかどうかを確認します。

    Screenshot of the checkout service container's log stream in the Azure portal.

  5. order-processor サービスに対して同じことを行います。

    Screenshot of the order processor service container's log stream in the Azure portal.

何が起きましたか?

azd up コマンドが正常に完了した場合、次のようになります。

  • Azure Developer CLI は、サンプル プロジェクトの ./infra ディレクトリで参照されている Azure リソースを、指定した Azure サブスクリプションにプロビジョニングしました。 これにより Azure portal から、これらの Azure リソースを表示できるようになりました。
  • Azure Container Apps にデプロイされたアプリ。 ポータルから、完全に機能するアプリを参照できます。

.NET アプリケーションをローカルで実行する

Azure Container Apps にアプリケーションをデプロイする前に、Dapr と Azure Service Bus を使用して order-processor サービスと checkout サービスをローカルで実行します。

プロジェクトを準備する

  1. サンプル Dapr アプリケーションをローカル コンピューターに複製します。

    git clone https://github.com/Azure-Samples/pubsub-dapr-csharp-servicebus.git
    
  2. サンプルのルート ディレクトリに移動します。

    cd pubsub-dapr-csharp-servicebus
    

Dapr CLI を使用して Dapr アプリケーションを実行する

まず、Dapr を使用して order-processor サブスクライバー サービスを実行することから始めます。

  1. サンプルのルート ディレクトリから、ディレクトリを order-processor に変更します。

    cd order-processor
    
  2. 依存関係をインストールします。

    dotnet build
    
  3. Dapr を使用して order-processor サービスを実行します。

    dapr run --app-id order-processor --resources-path ../components/ --app-port 7001 -- dotnet run --project .
    
  4. 新しいターミナル ウィンドウで、サンプルのルート ディレクトリから checkout パブリッシャー サービスに移動します。

    cd checkout
    
  5. 依存関係をインストールします。

    dotnet build
    
  6. Dapr を使用して checkout サービスを実行します。

    dapr run --app-id checkout --resources-path ../components/ -- dotnet run --project .
    

    想定される出力

    どちらのターミナルでも、checkout サービスは終了する前に order-processor サービスが受信した 10 個のメッセージを発行します。

    checkout 出力:

    == APP == Published data: {"orderId":1}
    == APP == Published data: {"orderId":2}
    == APP == Published data: {"orderId":3}
    == APP == Published data: {"orderId":4}
    == APP == Published data: {"orderId":5}
    == APP == Published data: {"orderId":6}
    == APP == Published data: {"orderId":7}
    == APP == Published data: {"orderId":8}
    == APP == Published data: {"orderId":9}
    == APP == Published data: {"orderId":10}
    

    order-processor 出力:

    == APP == Subscriber received: {"orderId":1}
    == APP == Subscriber received: {"orderId":2}
    == APP == Subscriber received: {"orderId":3}
    == APP == Subscriber received: {"orderId":4}
    == APP == Subscriber received: {"orderId":5}
    == APP == Subscriber received: {"orderId":6}
    == APP == Subscriber received: {"orderId":7}
    == APP == Subscriber received: {"orderId":8}
    == APP == Subscriber received: {"orderId":9}
    == APP == Subscriber received: {"orderId":10}
    
  7. 次のコマンドを実行して、両方のアプリケーションが停止していることを確認します。 チェックアウト ターミナルで。

    dapr stop --app-id checkout
    

    オーダー プロセッサー ターミナルで、次の手順を実行します。

    dapr stop --app-id order-processor
    

Azure Developer CLI を使用して Dapr アプリケーション テンプレートをデプロイする

azd を使用して、Dapr アプリケーションを Azure Container Apps にデプロイします。

プロジェクトを準備する

新しいターミナル ウィンドウで、サンプルのルート ディレクトリに移動します。

cd pubsub-dapr-csharp-servicebus

Azure Developer CLI を使用したプロビジョニングとデプロイ

  1. azd init を実行してプロジェクトを初期化します。

    azd init
    
  2. ターミナルでメッセージが表示されたら、次のパラメーターを指定します。

    パラメーター 説明
    Environment Name すべての Azure リソースを保持するために作成されたリソース グループのプレフィックス。
    Azure の場所 リソースの Azure の場所。
    Azure サブスクリプション リソースの Azure サブスクリプション。
  3. azd up を実行して、1 つのコマンドでインフラストラクチャをプロビジョニングして Dapr アプリケーションを Azure Container Apps にデプロイします。

    azd up
    

    この処理は、完了までに時間がかかる場合があります。 azd up コマンドが完了すると、CLI 出力に、デプロイの進行状況を監視するための 2 つの Azure portal リンクが表示されます。 また、出力では、次の azd up の方法も示されます。

    • azd provision を使用して、./infra ディレクトリ内の指定されている Bicep ファイルを使用して、必要なすべての Azure リソースを作成して構成します。 Azure Developer CLI によってプロビジョニングされると、Azure portal からこれらのリソースにアクセスできます。 Azure リソースをプロビジョニングするファイルは次のとおりです。
      • main.parameters.json
      • main.bicep
      • 機能別に整理された app リソース ディレクトリ
      • azd テンプレートで使用される Bicep モジュールを含む core リファレンス ライブラリ
    • azd deploy を使用したコードのデプロイ

    想定される出力

    Initializing a new project (azd init)
    
    
    Provisioning Azure resources (azd provision)
    Provisioning Azure resources can take some time
    
      You can view detailed progress in the Azure Portal:
      https://portal.azure.com
    
      (✓) Done: Resource group: resource-group-name
      (✓) Done: Application Insights: app-insights-name
      (✓) Done: Portal dashboard: portal-dashboard-name
      (✓) Done: Log Analytics workspace: log-analytics-name
      (✓) Done: Key vault: key-vault-name
      (✓) Done: Container Apps Environment: ca-env-name
      (✓) Done: Container App: ca-checkout-name
      (✓) Done: Container App: ca-orders-name
    
    
    Deploying services (azd deploy)
    
      (✓) Done: Deploying service checkout
      (✓) Done: Deploying service orders
      - Endpoint: https://ca-orders-name.endpoint.region.azurecontainerapps.io/
    
    SUCCESS: Your Azure app has been deployed!
    You can view the resources created under the resource group resource-group-name in Azure Portal:
    https://portal.azure.com/#@/resource/subscriptions/subscription-id/resourceGroups/resource-group-name/overview
    

デプロイが成功したことを確認する

Azure portal で、checkout サービスが Azure Service Bus トピックにメッセージを公開していることを確認します。

  1. ターミナル出力から checkout コンテナー アプリ名をコピーします。

  2. Azure portal にサインインし、名前でコンテナー アプリ リソースを検索します。

  3. Container Apps ダッシュボードで、[監視]>[ログ ストリーム] を選択します。

    Screenshot of navigating to the Log stream page in the Azure portal.

  4. checkout コンテナーが先ほどのターミナルと同じ出力をログに記録しているかどうかを確認します。

    Screenshot of the checkout service container's log stream in the Azure portal.

  5. order-processor サービスに対して同じことを行います。

    Screenshot of the order processor service container's log stream in the Azure portal.

何が起きましたか?

azd up コマンドが正常に完了した場合、次のようになります。

  • Azure Developer CLI は、サンプル プロジェクトの ./infra ディレクトリで参照されている Azure リソースを、指定した Azure サブスクリプションにプロビジョニングしました。 これにより Azure portal から、これらの Azure リソースを表示できるようになりました。
  • Azure Container Apps にデプロイされたアプリ。 ポータルから、完全に機能するアプリを参照できます。

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

このアプリケーションを引き続き使用しない場合は、次のコマンドを使用して、プロビジョニングした Azure リソースを削除します。

azd down

次のステップ