教學課程:使用 Dapr 連結的事件驅動工作

在本教學課程中,您會建立微服務來示範 Dapr 的連結 API,以使用外部系統作為輸入和輸出。 您將會:

  • 使用 Dapr CLI 在本機執行應用程式。
  • 透過 Azure Developer CLI 搭配提供的 Bicep 將應用程式部署至 Azure 容器應用程式。

服務會接聽來自系統 CRON 的輸入繫結事件,然後將本機資料的內容輸出至 PostreSql 輸出資料繫結。

繫結應用程式的圖表。

必要條件

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

將應用程式部署至 Azure 容器應用程式之前,請先使用 Docker Compose 和 Dapr 在本機執行 PostgreSQL 容器和 JavaScript 服務開始。

準備專案

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

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

    cd bindings-dapr-nodejs-cron-postgres
    

使用 Dapr CLI 執行應用程式

  1. 從範例的根目錄,請將目錄變更為 db

    cd db
    
  2. 使用 Docker Compose 的 PostgreSQL 容器。

    docker compose up -d
    
  3. 開啟新終端機視窗並瀏覽至範例目錄中的 /batch

    cd bindings-dapr-nodejs-cron-postgres/batch
    
  4. 安裝相依性。

    npm install
    
  5. 執行 JavaScript 服務應用程式。

    dapr run --app-id batch-sdk --app-port 5002 --dapr-http-port 3500 --resources-path ../components -- node index.js
    

    dapr run 命令會在本機執行繫結應用程式。 一旦成功執行應用程式後,終端視窗會顯示輸出繫結資料。

    預期的輸出

    批次服務會接聽來自系統 CRON 的輸入繫結事件,然後將本機資料的內容輸出至 PostgreSQL 輸出資料繫結。

    == APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
    == APP == Finished processing batch
    == APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
    == APP == Finished processing batch
    == APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
    == APP == Finished processing batch
    
  6. ./db 終端機中,停止 PostgreSQL 容器。

    docker compose stop
    

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

既然您已在本機執行應用程式,讓我們使用 azd 將繫結應用程式部署至 Azure 容器應用程式。 在部署期間,我們將交換本機容器化 PostgreSQL 作為 Azure PostgreSQL 元件。

準備專案

瀏覽至範例的根目錄。

cd bindings-dapr-nodejs-cron-postgres

使用 Azure Developer CLI 佈建和部署

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

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

    參數 描述
    環境名稱 建立以保存所有 Azure 資源的資源群組前綴。
    Azure 位置 您資源的 Azure 位置。 確定您選取 Azure PostgreSQL 的可用位置
    Azure 訂用帳戶 您資源的 Azure 訂閱。
  3. 執行 azd up 以使用單一命令佈建基礎結構,並將應用程式部署至 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/#blade/HubsExtension/DeploymentDetailsBlade/overview
    
      (✓) 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: Azure Database for PostgreSQL flexible server: postgres-server
      (✓) Done: Key vault: key-vault-name
      (✓) Done: Container Apps Environment: container-apps-env-name
      (✓) Done: Container App: container-app-name
    
    
    Deploying services (azd deploy)
    
      (✓) Done: Deploying service api
      - Endpoint: https://your-container-app-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/your-subscription-ID/resourceGroups/your-resource-group/overview
    

確認成功部署

在 Azure 入口網站中,確認批次容器應用程式每 10 秒會記錄每次插入的 Azure PostgreSQL。

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

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

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

    從 Azure 容器應用程式側邊功能表瀏覽至記錄資料流的螢幕擷取畫面。

  4. 確認容器正在記錄的輸出與稍早的終端機相同。

    Azure 入口網站中容器應用程式的記錄資料流的螢幕擷取畫面。

發生什麼事?

azd up 命令成功完成時:

  • Azure Developer CLI 已將範例專案 ./infra 目錄中所參考的 Azure 資源佈建到您指定的 Azure 訂閱。 您現在可以透過 Azure 入口網站檢視這些 Azure 資源。
  • 應用程式會部署至 Azure 容器應用程式。 您可以從入口網站瀏覽全部的功能應用程式。

在本機執行 Python 應用程式

準備專案

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

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

    cd bindings-dapr-python-cron-postgres
    

使用 Dapr CLI 執行應用程式

將應用程式部署至 Azure 容器應用程式之前,請先使用 Docker Compose 和 Dapr 在本機執行 PostgreSQL 容器和 Python 服務開始。

  1. 從範例的根目錄,請將目錄變更為 db

    cd db
    
  2. 使用 Docker Compose 的 PostgreSQL 容器。

    docker compose up -d
    
  3. 開啟新終端機視窗並瀏覽至範例目錄中的 /batch

    cd bindings-dapr-python-cron-postgres/batch
    
  4. 安裝相依性。

    pip install -r requirements.txt
    
  5. 執行 Python 服務應用程式。

    dapr run --app-id batch-sdk --app-port 5001 --dapr-http-port 3500 --resources-path ../components -- python3 app.py
    

    dapr run 命令會在本機執行繫結應用程式。 一旦成功執行應用程式後,終端視窗會顯示輸出繫結資料。

    預期的輸出

    批次服務會接聽來自系統 CRON 的輸入繫結事件,然後將本機資料的內容輸出至 PostgreSQL 輸出資料繫結。

    == APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
    == APP == Finished processing batch
    == APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
    == APP == Finished processing batch
    == APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
    == APP == Finished processing batch
    
  6. ./db 終端機中,停止 PostgreSQL 容器。

    docker compose stop
    

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

既然您已在本機執行應用程式,讓我們使用 azd 將繫結應用程式部署至 Azure 容器應用程式。 在部署期間,我們將交換本機容器化 PostgreSQL 作為 Azure PostgreSQL 元件。

準備專案

瀏覽至範例的根目錄。

cd bindings-dapr-python-cron-postgres

使用 Azure Developer CLI 佈建和部署

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

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

    參數 描述
    環境名稱 建立以保存所有 Azure 資源的資源群組前綴。
    Azure 位置 您資源的 Azure 位置。 確定您選取 Azure PostgreSQL 的可用位置
    Azure 訂用帳戶 您資源的 Azure 訂閱。
  3. 執行 azd up 以使用單一命令佈建基礎結構,並將應用程式部署至 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/#blade/HubsExtension/DeploymentDetailsBlade/overview
    
      (✓) 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: Azure Database for PostgreSQL flexible server: postgres-server
      (✓) Done: Key vault: key-vault-name
      (✓) Done: Container Apps Environment: container-apps-env-name
      (✓) Done: Container App: container-app-name
    
    
    Deploying services (azd deploy)
    
      (✓) Done: Deploying service api
      - Endpoint: https://your-container-app-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/your-subscription-ID/resourceGroups/your-resource-group/overview
    

確認成功部署

在 Azure 入口網站中,確認批次容器應用程式每 10 秒會記錄每次插入的 Azure PostgreSQL。

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

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

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

    從 Azure 容器應用程式側邊功能表瀏覽至記錄資料流的螢幕擷取畫面。

  4. 確認容器正在記錄的輸出與稍早的終端機相同。

    Azure 入口網站中容器應用程式的記錄資料流的螢幕擷取畫面。

發生什麼事?

azd up 命令成功完成時:

  • Azure Developer CLI 已將範例專案 ./infra 目錄中所參考的 Azure 資源佈建到您指定的 Azure 訂閱。 您現在可以透過 Azure 入口網站檢視這些 Azure 資源。
  • 應用程式會部署至 Azure 容器應用程式。 您可以從入口網站瀏覽全部的功能應用程式。

在本機執行 .NET 應用程式

準備專案

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

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

    cd bindings-dapr-csharp-cron-postgres
    

使用 Dapr CLI 執行應用程式

將應用程式部署至 Azure 容器應用程式前,請透過 Docker Compose 和 Dapr 在本機執行 PostgreSQL 容器和 .NET 服務。

  1. 從範例的根目錄,請將目錄變更為 db

    cd db
    
  2. 使用 Docker Compose 的 PostgreSQL 容器。

    docker compose up -d
    
  3. 開啟新終端機視窗並瀏覽至範例目錄中的 /batch

    cd bindings-dapr-csharp-cron-postgres/batch
    
  4. 安裝相依性。

    dotnet build
    
  5. 執行 .NET 服務應用程式。

    dapr run --app-id batch-sdk --app-port 7002 --resources-path ../components -- dotnet run
    

    dapr run 命令會在本機執行繫結應用程式。 一旦成功執行應用程式後,終端視窗會顯示輸出繫結資料。

    預期的輸出

    批次服務會接聽來自系統 CRON 的輸入繫結事件,然後將本機資料的內容輸出至 PostgreSQL 輸出資料繫結。

    == APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
    == APP == Finished processing batch
    == APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
    == APP == Finished processing batch
    == APP == {"sql": "insert into orders (orderid, customer, price) values (1, 'John Smith', 100.32);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (2, 'Jane Bond', 15.4);"}
    == APP == {"sql": "insert into orders (orderid, customer, price) values (3, 'Tony James', 35.56);"}
    == APP == Finished processing batch
    
  6. ./db 終端機中,停止 PostgreSQL 容器。

    docker compose stop
    

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

既然您已在本機執行應用程式,讓我們使用 azd 將繫結應用程式部署至 Azure 容器應用程式。 在部署期間,我們將交換本機容器化 PostgreSQL 作為 Azure PostgreSQL 元件。

準備專案

瀏覽至範例的根目錄。

cd bindings-dapr-csharp-cron-postgres

使用 Azure Developer CLI 佈建和部署

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

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

    參數 描述
    環境名稱 建立以保存所有 Azure 資源的資源群組前綴。
    Azure 位置 您資源的 Azure 位置。 確定您選取 Azure PostgreSQL 的可用位置
    Azure 訂用帳戶 您資源的 Azure 訂閱。
  3. 執行 azd up 以使用單一命令佈建基礎結構,並將應用程式部署至 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/#blade/HubsExtension/DeploymentDetailsBlade/overview
    
      (✓) 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: Azure Database for PostgreSQL flexible server: postgres-server
      (✓) Done: Key vault: key-vault-name
      (✓) Done: Container Apps Environment: container-apps-env-name
      (✓) Done: Container App: container-app-name
    
    
    Deploying services (azd deploy)
    
      (✓) Done: Deploying service api
      - Endpoint: https://your-container-app-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/your-subscription-ID/resourceGroups/your-resource-group/overview
    

確認成功部署

在 Azure 入口網站中,確認批次容器應用程式每 10 秒會記錄每次插入的 Azure PostgreSQL。

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

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

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

    從 Azure 容器應用程式側邊功能表瀏覽至記錄資料流的螢幕擷取畫面。

  4. 確認容器正在記錄的輸出與稍早的終端機相同。

    Azure 入口網站中容器應用程式的記錄資料流的螢幕擷取畫面。

發生什麼事?

azd up 命令成功完成時:

  • Azure Developer CLI 已將範例專案 ./infra 目錄中所參考的 Azure 資源佈建到您指定的 Azure 訂閱。 您現在可以透過 Azure 入口網站檢視這些 Azure 資源。
  • 應用程式會部署至 Azure 容器應用程式。 您可以從入口網站瀏覽全部的功能應用程式。

清除資源

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

azd down

下一步