共用方式為


教學課程:使用 Dapr 服務叫用的微服務通訊

在本教學課程中,您將會:

  • 透過 Dapr 的服務叫用 API 建立和執行兩個微服務,使用自動 mTLS 安全地通訊,並可靠地使用內建重試。
  • 透過 Azure Developer CLI 搭配提供的 Bicep 將應用程式部署至 Azure 容器應用程式。

範例服務調用專案包括:

  1. 在迴圈上使用 Dapr HTTP Proxy 處理功能的 checkout 服務,在 order-processor 服務上叫用要求。
  2. checkout 服務接收要求 order-processor 服務。

Diagram of the Dapr service invocation services.

必要條件

在本端執行 Node.js 應用程式

將應用程式部署至 Azure 容器應用程式之前,請先使用 Dapr 在本機執行 order-processorcheckout 服務。

準備專案

  1. 範例 Dapr 應用程式複製到本機電腦。

    git clone https://github.com/Azure-Samples/svc-invoke-dapr-nodejs.git
    
  2. 瀏覽至範例的根目錄。

    cd svc-invoke-dapr-nodejs
    

使用 Dapr CLI 執行 Dapr 應用程式

從執行 order-processor 服務開始。

  1. 從範例的根目錄,請將目錄變更為 order-processor

    cd order-processor
    
  2. 安裝相依性。

    npm install
    
  3. 使用 Dapr 執行 order-processor 服務。

    dapr run --app-port 5001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- npm start
    
  4. 在新的終端機視窗中,從範例的根目錄瀏覽至 checkout 呼叫者服務。

    cd checkout
    
  5. 安裝相依性。

    npm install
    
  6. 使用 Dapr 執行 checkout 服務。

    dapr run  --app-id checkout --app-protocol http --dapr-http-port 3500 -- npm start
    

    預期的輸出

    在這兩個終端機中,checkout 服務會呼叫迴圈中 order-processor 服務的順序。

    checkout 輸出:

    == APP == Order passed: {"orderId":1}
    == APP == Order passed: {"orderId":2}
    == APP == Order passed: {"orderId":3}
    == APP == Order passed: {"orderId":4}
    == APP == Order passed: {"orderId":5}
    == APP == Order passed: {"orderId":6}
    == APP == Order passed: {"orderId":7}
    == APP == Order passed: {"orderId":8}
    == APP == Order passed: {"orderId":9}
    == APP == Order passed: {"orderId":10}
    == APP == Order passed: {"orderId":11}
    == APP == Order passed: {"orderId":12}
    == APP == Order passed: {"orderId":13}
    == APP == Order passed: {"orderId":14}
    == APP == Order passed: {"orderId":15}
    == APP == Order passed: {"orderId":16}
    == APP == Order passed: {"orderId":17}
    == APP == Order passed: {"orderId":18}
    == APP == Order passed: {"orderId":19}
    == APP == Order passed: {"orderId":20}
    

    order-processor 輸出:

    == APP == Order received: { orderId: 1 }
    == APP == Order received: { orderId: 2 }
    == APP == Order received: { orderId: 3 }
    == APP == Order received: { orderId: 4 }
    == APP == Order received: { orderId: 5 }
    == APP == Order received: { orderId: 6 }
    == APP == Order received: { orderId: 7 }
    == APP == Order received: { orderId: 8 }
    == APP == Order received: { orderId: 9 }
    == APP == Order received: { orderId: 10 }
    == APP == Order received: { orderId: 11 }
    == APP == Order received: { orderId: 12 }
    == APP == Order received: { orderId: 13 }
    == APP == Order received: { orderId: 14 }
    == APP == Order received: { orderId: 15 }
    == APP == Order received: { orderId: 16 }
    == APP == Order received: { orderId: 17 }
    == APP == Order received: { orderId: 18 }
    == APP == Order received: { orderId: 19 }
    == APP == Order received: { orderId: 20 }
    
  7. 在兩個終端機中按下 Cmd/Ctrl + C ,以退出服務對服務調用。

使用 Azure Developer CLI 部署 Dapr 應用程式範本

使用 azd 將 Dapr 應用程式部署至 Azure 容器應用程式。

準備專案

在新的終端機視窗中,瀏覽至範例的根目錄。

cd svc-invoke-dapr-nodejs

使用 Azure Developer CLI 佈建和部署

  1. 執行 azd init 來初始化專案。

    azd init
    
  2. 在終端機出現提示時,請提供下列參數。

    參數 描述
    環境名稱 建立以保存所有 Azure 資源的資源群組前綴。
    Azure 位置 您資源的 Azure 位置。
    Azure 訂用帳戶 您資源的 Azure 訂閱。
  3. 執行 azd up 以使用單一命令佈建基礎結構,並將 Dapr 應用程式部署至 Azure 容器應用程式。

    azd up
    

    此程序需要一些時間才會完成。 當 azd up 命令完成時,CLI 輸出會顯示兩個 Azure 入口網站連結,以監視部署進度。 輸出也會示範如何 azd up

    • 使用 azd provision 透過 ./infra 目錄提供的 Bicep 檔案,建立和設定所有必要的 Azure 資源。 一旦透過 Azure Developer CLI 佈建後,便可以透過 Azure 入口網站存取這些資源。 佈建 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: Log Analytics workspace: log-analytics-name
      (✓) Done: Application Insights: app-insights-name
      (✓) Done: Portal dashboard: dashboard-name
      (✓) Done: Container Apps Environment: container-apps-env-name
      (✓) Done: Container App: ca-checkout-name
      (✓) Done: Container App: ca-order-processor-name
    
    
    Deploying services (azd deploy)
    
      (✓) Done: Deploying service api
      - Endpoint: https://ca-order-processor-name.eastus.azurecontainerapps.io/
      (✓) Done: Deploying service worker
    
    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/<your-azure-subscription>/resourceGroups/resource-group-name/overview
    

確認成功部署

在 Azure 入口網站中,確認 checkout 服務已將訂單傳送至 order-processor 服務。

  1. 從終端機輸出複製 checkout 的容器應用程式名稱。

  2. 登入 Azure 入口網站,然後依名稱搜尋容器應用程式資源。

  3. 在「容器應用程式」儀表板中,選取「監視>記錄資料流」。

    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 入口網站檢視這些 Azure 資源。
  • 應用程式會部署至 Azure 容器應用程式。 您可以從入口網站瀏覽全部的功能應用程式。

在本機執行 Python 應用程式

將應用程式部署至 Azure 容器應用程式之前,請先使用 Dapr 在本機執行 order-processorcheckout 服務。

準備專案

  1. 範例 Dapr 應用程式複製到本機電腦。

    git clone https://github.com/Azure-Samples/svc-invoke-dapr-python.git
    
  2. 瀏覽至範例的根目錄。

    cd svc-invoke-dapr-python
    

使用 Dapr CLI 執行 Dapr 應用程式

從執行 order-processor 服務開始。

  1. 從範例的根目錄,請將目錄變更為 order-processor

    cd order-processor
    
  2. 安裝相依性。

    pip3 install -r requirements.txt
    
  3. 使用 Dapr 執行 order-processor 服務。

    dapr run --app-port 8001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- python3 app.py
    
  4. 在新的終端機視窗中,從範例的根目錄瀏覽至 checkout 呼叫者服務。

    cd checkout
    
  5. 安裝相依性。

    pip3 install -r requirements.txt
    
  6. 使用 Dapr 執行 checkout 服務。

    dapr run  --app-id checkout --app-protocol http --dapr-http-port 3500 -- python3 app.py
    

    預期的輸出

    在這兩個終端機中,checkout 服務會呼叫迴圈中 order-processor 服務的順序。

    checkout 輸出:

    == APP == Order passed: {"orderId":1}
    == APP == Order passed: {"orderId":2}
    == APP == Order passed: {"orderId":3}
    == APP == Order passed: {"orderId":4}
    == APP == Order passed: {"orderId":5}
    == APP == Order passed: {"orderId":6}
    == APP == Order passed: {"orderId":7}
    == APP == Order passed: {"orderId":8}
    == APP == Order passed: {"orderId":9}
    == APP == Order passed: {"orderId":10}
    == APP == Order passed: {"orderId":11}
    == APP == Order passed: {"orderId":12}
    == APP == Order passed: {"orderId":13}
    == APP == Order passed: {"orderId":14}
    == APP == Order passed: {"orderId":15}
    == APP == Order passed: {"orderId":16}
    == APP == Order passed: {"orderId":17}
    == APP == Order passed: {"orderId":18}
    == APP == Order passed: {"orderId":19}
    == APP == Order passed: {"orderId":20}
    

    order-processor 輸出:

    == APP == Order received: { orderId: 1 }
    == APP == Order received: { orderId: 2 }
    == APP == Order received: { orderId: 3 }
    == APP == Order received: { orderId: 4 }
    == APP == Order received: { orderId: 5 }
    == APP == Order received: { orderId: 6 }
    == APP == Order received: { orderId: 7 }
    == APP == Order received: { orderId: 8 }
    == APP == Order received: { orderId: 9 }
    == APP == Order received: { orderId: 10 }
    == APP == Order received: { orderId: 11 }
    == APP == Order received: { orderId: 12 }
    == APP == Order received: { orderId: 13 }
    == APP == Order received: { orderId: 14 }
    == APP == Order received: { orderId: 15 }
    == APP == Order received: { orderId: 16 }
    == APP == Order received: { orderId: 17 }
    == APP == Order received: { orderId: 18 }
    == APP == Order received: { orderId: 19 }
    == APP == Order received: { orderId: 20 }
    
  7. 在兩個終端機中按下 Cmd/Ctrl + C,以退出服務對服務調用

使用 Azure Developer CLI 部署 Dapr 應用程式範本

使用 azd 將 Dapr 應用程式部署至 Azure 容器應用程式。

準備專案

  1. 在新的終端機視窗中,瀏覽至範例的根目錄。

    cd svc-invoke-dapr-python
    

使用 Azure Developer CLI 佈建和部署

  1. 執行 azd init 來初始化專案。

    azd init
    
  2. 在終端機出現提示時,請提供下列參數。

    參數 描述
    環境名稱 建立以保存所有 Azure 資源的資源群組前綴。
    Azure 位置 您資源的 Azure 位置。
    Azure 訂用帳戶 您資源的 Azure 訂閱。
  3. 執行 azd up 以使用單一命令佈建基礎結構,並將 Dapr 應用程式部署至 Azure 容器應用程式。

    azd up
    

    此程序需要一些時間才會完成。 當 azd up 命令完成時,CLI 輸出會顯示兩個 Azure 入口網站連結,以監視部署進度。 輸出也會示範如何 azd up

    • 使用 azd provision 透過 ./infra 目錄提供的 Bicep 檔案,建立和設定所有必要的 Azure 資源。 一旦透過 Azure Developer CLI 佈建後,便可以透過 Azure 入口網站存取這些資源。 佈建 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: Log Analytics workspace: log-analytics-name
      (✓) Done: Application Insights: app-insights-name
      (✓) Done: Portal dashboard: dashboard-name
      (✓) Done: Container Apps Environment: container-apps-env-name
      (✓) Done: Container App: ca-checkout-name
      (✓) Done: Container App: ca-order-processor-name
    
    
    Deploying services (azd deploy)
    
      (✓) Done: Deploying service api
      - Endpoint: https://ca-order-processor-name.eastus.azurecontainerapps.io/
      (✓) Done: Deploying service worker
    
    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/<your-azure-subscription>/resourceGroups/resource-group-name/overview
    

確認成功部署

在 Azure 入口網站中,確認 checkout 服務已將訂單傳送至 order-processor 服務。

  1. 從終端機輸出複製 checkout 的容器應用程式名稱。

  2. 登入 Azure 入口網站,然後依名稱搜尋容器應用程式資源。

  3. 在「容器應用程式」儀表板中,選取「監視>記錄資料流」。

    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 入口網站檢視這些 Azure 資源。
  • 應用程式會部署至 Azure 容器應用程式。 您可以從入口網站瀏覽全部的功能應用程式。

在本機執行 .NET 應用程式

將應用程式部署至 Azure 容器應用程式之前,請先使用 Dapr 在本機執行 order-processorcheckout 服務。

準備專案

  1. 範例 Dapr 應用程式複製到本機電腦。

    git clone https://github.com/Azure-Samples/svc-invoke-dapr-csharp.git
    
  2. 瀏覽至範例的根目錄。

    cd svc-invoke-dapr-csharp
    

使用 Dapr CLI 執行 Dapr 應用程式

首先,使用 Dapr 執行 order-processor 被呼叫者服務。

  1. 從範例的根目錄,請將目錄變更為 order-processor

    cd order-processor
    
  2. 安裝相依性。

    dotnet build
    
  3. 使用 Dapr 執行 order-processor 服務。

    dapr run --app-port 7001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- dotnet run
    
  4. 在新的終端機視窗中,從範例的根目錄瀏覽至 checkout 呼叫者服務。

    cd checkout
    
  5. 安裝相依性。

    dotnet build
    
  6. 使用 Dapr 執行 checkout 服務。

    dapr run  --app-id checkout --app-protocol http --dapr-http-port 3500 -- dotnet run
    

    預期的輸出

    在這兩個終端機中,checkout 服務會呼叫迴圈中 order-processor 服務的順序。

    checkout 輸出:

    == APP == Order passed: {"orderId":1}
    == APP == Order passed: {"orderId":2}
    == APP == Order passed: {"orderId":3}
    == APP == Order passed: {"orderId":4}
    == APP == Order passed: {"orderId":5}
    == APP == Order passed: {"orderId":6}
    == APP == Order passed: {"orderId":7}
    == APP == Order passed: {"orderId":8}
    == APP == Order passed: {"orderId":9}
    == APP == Order passed: {"orderId":10}
    == APP == Order passed: {"orderId":11}
    == APP == Order passed: {"orderId":12}
    == APP == Order passed: {"orderId":13}
    == APP == Order passed: {"orderId":14}
    == APP == Order passed: {"orderId":15}
    == APP == Order passed: {"orderId":16}
    == APP == Order passed: {"orderId":17}
    == APP == Order passed: {"orderId":18}
    == APP == Order passed: {"orderId":19}
    == APP == Order passed: {"orderId":20}
    

    order-processor 輸出:

    == APP == Order received: { orderId: 1 }
    == APP == Order received: { orderId: 2 }
    == APP == Order received: { orderId: 3 }
    == APP == Order received: { orderId: 4 }
    == APP == Order received: { orderId: 5 }
    == APP == Order received: { orderId: 6 }
    == APP == Order received: { orderId: 7 }
    == APP == Order received: { orderId: 8 }
    == APP == Order received: { orderId: 9 }
    == APP == Order received: { orderId: 10 }
    == APP == Order received: { orderId: 11 }
    == APP == Order received: { orderId: 12 }
    == APP == Order received: { orderId: 13 }
    == APP == Order received: { orderId: 14 }
    == APP == Order received: { orderId: 15 }
    == APP == Order received: { orderId: 16 }
    == APP == Order received: { orderId: 17 }
    == APP == Order received: { orderId: 18 }
    == APP == Order received: { orderId: 19 }
    == APP == Order received: { orderId: 20 }
    
  7. 在兩個終端機中按下 Cmd/Ctrl + C ,以退出服務對服務調用。

使用 Azure Developer CLI 部署 Dapr 應用程式範本

使用 azd 將 Dapr 應用程式部署至 Azure 容器應用程式。

準備專案

在新的終端機視窗中,瀏覽至範例的根目錄。

cd svc-invoke-dapr-csharp

使用 Azure Developer CLI 佈建和部署

  1. 執行 azd init 來初始化專案。

    azd init
    
  2. 在終端機出現提示時,請提供下列參數。

    參數 描述
    環境名稱 建立以保存所有 Azure 資源的資源群組前綴。
    Azure 位置 您資源的 Azure 位置。
    Azure 訂用帳戶 您資源的 Azure 訂閱。
  3. 執行 azd up 以使用單一命令佈建基礎結構,並將 Dapr 應用程式部署至 Azure 容器應用程式。

    azd up
    

    此程序需要一些時間才會完成。 當 azd up 命令完成時,CLI 輸出會顯示兩個 Azure 入口網站連結,以監視部署進度。 輸出也會示範如何 azd up

    • 使用 azd provision 透過 ./infra 目錄提供的 Bicep 檔案,建立和設定所有必要的 Azure 資源。 一旦透過 Azure Developer CLI 佈建後,便可以透過 Azure 入口網站存取這些資源。 佈建 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: Log Analytics workspace: log-analytics-name
      (✓) Done: Application Insights: app-insights-name
      (✓) Done: Portal dashboard: dashboard-name
      (✓) Done: Container Apps Environment: container-apps-env-name
      (✓) Done: Container App: ca-checkout-name
      (✓) Done: Container App: ca-order-processor-name
    
    
    Deploying services (azd deploy)
    
      (✓) Done: Deploying service api
      - Endpoint: https://ca-order-processor-name.eastus.azurecontainerapps.io/
      (✓) Done: Deploying service worker
    
    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/<your-azure-subscription>/resourceGroups/resource-group-name/overview
    

確認成功部署

在 Azure 入口網站中,確認 checkout 服務已將訂單傳送至 order-processor 服務。

  1. 從終端機輸出複製 checkout 的容器應用程式名稱。

  2. 登入 Azure 入口網站,然後依名稱搜尋容器應用程式資源。

  3. 在「容器應用程式」儀表板中,選取「監視>記錄資料流」。

    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 入口網站檢視這些 Azure 資源。
  • 應用程式會部署至 Azure 容器應用程式。 您可以從入口網站瀏覽全部的功能應用程式。

清除資源

若您不打算繼續使用此應用程式,請刪除使用下列命令佈建的 Azure 資源:

azd down

下一步