教程:使用 Dapr 发布和订阅的微服务通信

在本教程中,你将:

  • 创建一个发布服务器微服务和一个订阅服务器微服务,它们利用 Dapr 发布/订阅 API 通过事件驱动体系结构的消息进行通信。
  • 使用提供的 Bicep 通过 Azure Developer CLI 将应用程序部署到 Azure 容器应用。

示例发布/订阅项目包括:

  1. 一个消息生成者(发布服务器)checkout 服务,用于生成特定主题的消息。
  2. 一个(订阅服务器)order-processor 服务,用于侦听来自特定主题的 checkout 服务的消息。

Diagram of the Dapr pub/sub sample.

先决条件

在本地运行 Node.js 应用程序

在将应用程序部署到 Azure 容器应用之前,请使用 Dapr 和 Azure 服务总线在本地运行 order-processorcheckout 服务。

准备项目

  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 容器应用。

准备项目

在新的终端窗口中,导航到示例的根目录。

cd pubsub-dapr-nodejs-servicebus

使用 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: 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.dev.cn/
    
    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/subscription-id/resourceGroups/resource-group-name/overview
    

确认部署是否成功

在 Azure 门户中,验证 checkout 服务是否正在将消息发布到 Azure 服务总线主题。

  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 和 Azure 服务总线在本地运行 order-processorcheckout 服务。

准备项目

  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 容器应用。

准备项目

在新的终端窗口中,导航到示例的根目录。

cd pubsub-dapr-python-servicebus

使用 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: 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.dev.cn/
    
    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/subscription-id/resourceGroups/resource-group-name/overview
    

确认部署是否成功

在 Azure 门户中,验证 checkout 服务是否正在将消息发布到 Azure 服务总线主题。

  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 和 Azure 服务总线在本地运行 order-processorcheckout 服务。

准备项目

  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 容器应用。

准备项目

在新的终端窗口中,导航到示例的根目录。

cd pubsub-dapr-csharp-servicebus

使用 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: 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.dev.cn/
    
    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/subscription-id/resourceGroups/resource-group-name/overview
    

确认部署是否成功

在 Azure 门户中,验证 checkout 服务是否正在将消息发布到 Azure 服务总线主题。

  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

后续步骤