教程:使用 Dapr 服务调用的微服务通信

在本教程中,你将:

  • 在本地创建并运行两个微服务,它们使用自动 mTLS 安全地通信,并通过 Dapr 的服务调用 API 使用内置重试可靠地通信。
  • 使用提供的 Bicep 通过 Azure Developer CLI 将应用程序部署到 Azure 容器应用。

示例服务调用项目包括:

  1. 一个 checkout 服务,它循环使用 Dapr 的 HTTP 代理功能来调用对 order-processor 服务的请求。
  2. 一个 order-processor 服务,它从 checkout 服务接收请求。

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 资源目录
      • 一个 core 参考库,其中包含 azd 模板使用的 Bicep 模块
    • 使用 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.cn
    
      (✓) 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.chinanorth3.azurecontainerapps-dev.cn/
      (✓) 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.cn/#@/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 资源目录
      • 一个 core 参考库,其中包含 azd 模板使用的 Bicep 模块
    • 使用 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.cn
    
      (✓) 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.chinanorth3.azurecontainerapps-dev.cn/
      (✓) 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.cn/#@/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 资源目录
      • 一个 core 参考库,其中包含 azd 模板使用的 Bicep 模块
    • 使用 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.cn
    
      (✓) 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.chinanorth3.azurecontainerapps-dev.cn.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.cn/#@/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

后续步骤