在本教程中,会创建并运行两个微服务,它们使用自动 mTLS 安全地通信,并通过 Dapr 的服务调用 API 使用内置重试可靠地通信。 你将:
- 在本地运行应用程序。
- 使用提供的 Bicep 通过 Azure Developer CLI 将应用程序部署到 Azure 容器应用。
示例服务调用项目包括:
- 一个 checkout服务,它循环使用 HTTP 代理功能来调用对order-processor服务的请求。
- 一个 order-processor服务,它从checkout服务接收请求。
 
先决条件
- 安装 Azure Developer CLI
- 安装并初始化 Dapr
- Docker Desktop
- 安装 Git
在本地运行 Node.js 应用程序
在将应用程序部署到 Azure 容器应用之前,首先请使用 Dapr 在本地运行 order-processor 和 checkout 服务。
准备项目
- 将示例应用程序克隆到本地计算机。 - git clone https://github.com/Azure-Samples/svc-invoke-dapr-nodejs.git
- 导航到示例的根目录。 - cd svc-invoke-dapr-nodejs
使用 Dapr CLI 运行应用程序
首先运行 order-processor 服务。
- 在示例的根目录中,将目录更改为 - order-processor。- cd order-processor
- 安装依赖项。 - npm install
- 运行 - order-processor服务。- dapr run --app-port 5001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- npm start
- 在新的终端窗口中,从示例的根目录导航到 - checkout调用方服务。- cd checkout
- 安装依赖项。 - npm install
- 运行 - 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 }
- 在这两个终端中,按 Cmd/Ctrl + C 退出服务到服务调用。 
使用 Azure Developer CLI 部署应用程序模板
使用 azd 将应用程序部署到 Azure 容器应用。
准备项目
在新的终端窗口中,导航到示例的根目录。
cd svc-invoke-dapr-nodejs
使用 Azure Developer CLI 进行预配和部署
- 运行 - azd init以初始化项目。- azd init
- 当终端中出现提示时,请提供以下参数。 - 参数 - 说明 - 环境名称 - 为保存所有 Azure 资源而创建的资源组的前缀。 - Azure 位置 - 资源的 Azure 位置。 - Azure 订阅 - 资源的 Azure 订阅。 
- 运行 - 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资源目录
- 一个 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/ (✓) 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 服务。
- 复制终端输出中的 - checkout容器应用名称。
- 转到 Azure 门户并按名称搜索容器应用资源。 
- 在容器应用仪表板中,选择“监视”>“日志流”。   
- 确认 - checkout容器记录的输出是否与前面在终端中显示的输出相同。  
- 对 - order-processor服务执行相同的操作。  
发生了什么情况?
成功完成 azd up 命令后:
- Azure Developer CLI 已将示例项目的 ./infra目录中引用的 Azure 资源预配到指定的 Azure 订阅。 现在可以通过 Azure 门户查看这些 Azure 资源。
- 部署到 Azure 容器应用的应用。 在门户中,可以浏览功能齐全的应用。
在本地运行 Python 应用程序
在将应用程序部署到 Azure 容器应用之前,首先请使用 Dapr 在本地运行 order-processor 和 checkout 服务。
准备项目
- 将示例应用程序克隆到本地计算机。 - git clone https://github.com/Azure-Samples/svc-invoke-dapr-python.git
- 导航到示例的根目录。 - cd svc-invoke-dapr-python
使用 Dapr CLI 运行应用程序
首先运行 order-processor 服务。
- 在示例的根目录中,将目录更改为 - order-processor。- cd order-processor
- 安装依赖项。 - pip3 install -r requirements.txt
- 运行 - order-processor服务。- dapr run --app-port 8001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- python3 app.py
- 在新的终端窗口中,从示例的根目录导航到 - checkout调用方服务。- cd checkout
- 安装依赖项。 - pip3 install -r requirements.txt
- 运行 - 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 }
- 在这两个终端中,按 Cmd/Ctrl + C 退出服务到服务调用 
使用 Azure Developer CLI 部署应用程序模板
使用 azd 将应用程序部署到 Azure 容器应用。
准备项目
- 在新的终端窗口中,导航到示例的根目录。 - cd svc-invoke-dapr-python
使用 Azure Developer CLI 进行预配和部署
- 运行 - azd init以初始化项目。- azd init
- 当终端中出现提示时,请提供以下参数。 - 参数 - 说明 - 环境名称 - 为保存所有 Azure 资源而创建的资源组的前缀。 - Azure 位置 - 资源的 Azure 位置。 - Azure 订阅 - 资源的 Azure 订阅。 
- 运行 - 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资源目录
- 一个 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/ (✓) 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 服务。
- 复制终端输出中的 - checkout容器应用名称。
- 转到 Azure 门户并按名称搜索容器应用资源。 
- 在容器应用仪表板中,选择“监视”>“日志流”。   
- 确认 - checkout容器记录的输出是否与前面在终端中显示的输出相同。  
- 对 - order-processor服务执行相同的操作。  
发生了什么情况?
成功完成 azd up 命令后:
- Azure Developer CLI 已将示例项目的 ./infra目录中引用的 Azure 资源预配到指定的 Azure 订阅。 现在可以通过 Azure 门户查看这些 Azure 资源。
- 部署到 Azure 容器应用的应用。 在门户中,可以浏览功能齐全的应用。
在本地运行 .NET 应用程序
在将应用程序部署到 Azure 容器应用之前,首先请使用 Dapr 在本地运行 order-processor 和 checkout 服务。
准备项目
- 将示例应用程序克隆到本地计算机。 - git clone https://github.com/Azure-Samples/svc-invoke-dapr-csharp.git
- 导航到示例的根目录。 - cd svc-invoke-dapr-csharp
使用 Dapr CLI 运行应用程序
首先运行 order-processor 被调用方服务。
- 在示例的根目录中,将目录更改为 - order-processor。- cd order-processor
- 安装依赖项。 - dotnet build
- 运行 - order-processor服务。- dapr run --app-port 7001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- dotnet run
- 在新的终端窗口中,从示例的根目录导航到 - checkout调用方服务。- cd checkout
- 安装依赖项。 - dotnet build
- 运行 - 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 }
- 在这两个终端中,按 Cmd/Ctrl + C 退出服务到服务调用。 
使用 Azure Developer CLI 部署应用程序模板
使用 azd 将应用程序部署到 Azure 容器应用。
准备项目
在新的终端窗口中,导航到示例的根目录。
cd svc-invoke-dapr-csharp
使用 Azure Developer CLI 进行预配和部署
- 运行 - azd init以初始化项目。- azd init
- 当终端中出现提示时,请提供以下参数。 - 参数 - 说明 - 环境名称 - 为保存所有 Azure 资源而创建的资源组的前缀。 - Azure 位置 - 资源的 Azure 位置。 - Azure 订阅 - 资源的 Azure 订阅。 
- 运行 - 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资源目录
- 一个 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 服务。
- 复制终端输出中的 - checkout容器应用名称。
- 转到 Azure 门户并按名称搜索容器应用资源。 
- 在容器应用仪表板中,选择“监视”>“日志流”。   
- 确认 - checkout容器记录的输出是否与前面在终端中显示的输出相同。  
- 对 - order-processor服务执行相同的操作。  
发生了什么情况?
成功完成 azd up 命令后:
- Azure Developer CLI 已将示例项目的 ./infra目录中引用的 Azure 资源预配到指定的 Azure 订阅。 现在可以通过 Azure 门户查看这些 Azure 资源。
- 部署到 Azure 容器应用的应用。 在门户中,可以浏览功能齐全的应用。
清理资源
如果你今后不打算继续使用此应用程序,请使用以下命令删除预配的 Azure 资源:
azd down
后续步骤
- 详细了解如何将 Dapr 应用程序部署到 Azure 容器应用。
- 详细了解 Azure Developer CLI 以及如何使应用程序与 azd兼容。