संपादित करें

इसके माध्यम से साझा किया गया


Tutorial: Microservices communication using Dapr Service Invocation

In this tutorial, you create and run two microservices that communicate securely using auto-mTLS and reliably using built-in retries via the Dapr Service Invocation API. You'll:

  • Run the application locally.
  • Deploy the application to Azure Container Apps via the Azure Developer CLI with the provided Bicep.

The sample service invocation project includes:

  1. A checkout service that uses HTTP proxying on a loop to invoke a request on the order-processor service.
  2. An order-processor service that receives the request from the checkout service.

Diagram of the service invocation services.

Prerequisites

Run the Node.js applications locally

Before deploying the application to Azure Container Apps, start by running the order-processor and checkout services locally with Dapr.

Prepare the project

  1. Clone the sample applications to your local machine.

    git clone https://github.com/Azure-Samples/svc-invoke-dapr-nodejs.git
    
  2. Navigate into the sample's root directory.

    cd svc-invoke-dapr-nodejs
    

Run the applications using the Dapr CLI

Start by running the order-processor service.

  1. From the sample's root directory, change directories to order-processor.

    cd order-processor
    
  2. Install the dependencies.

    npm install
    
  3. Run the order-processor service.

    dapr run --app-port 5001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- npm start
    
  4. In a new terminal window, from the sample's root directory, navigate to the checkout caller service.

    cd checkout
    
  5. Install the dependencies.

    npm install
    
  6. Run the checkout service.

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

    Expected output

    In both terminals, the checkout service is calling orders to the order-processor service in a loop.

    checkout output:

    == 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 output:

    == 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. Press Cmd/Ctrl + C in both terminals to exit out of the service-to-service invocation.

Deploy the application template using Azure Developer CLI

Deploy the application to Azure Container Apps using azd.

Prepare the project

In a new terminal window, navigate into the sample's root directory.

cd svc-invoke-dapr-nodejs

Provision and deploy using Azure Developer CLI

  1. Run azd init to initialize the project.

    azd init
    
  2. When prompted in the terminal, provide the following parameters.

    Parameter Description
    Environment Name Prefix for the resource group created to hold all Azure resources.
    Azure Location The Azure location for your resources.
    Azure Subscription The Azure subscription for your resources.
  3. Run azd up to provision the infrastructure and deploy the application to Azure Container Apps in a single command.

    azd up
    

    This process may take some time to complete. As the azd up command completes, the CLI output displays two Azure portal links to monitor the deployment progress. The output also demonstrates how azd up:

    • Creates and configures all necessary Azure resources via the provided Bicep files in the ./infra directory using azd provision. Once provisioned by Azure Developer CLI, you can access these resources via the Azure portal. The files that provision the Azure resources include:
      • main.parameters.json
      • main.bicep
      • An app resources directory organized by functionality
      • A core reference library that contains the Bicep modules used by the azd template
    • Deploys the code using azd deploy

    Expected output

    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
    

Confirm successful deployment

In the Azure portal, verify the checkout service is passing orders to the order-processor service.

  1. Copy the checkout container app's name from the terminal output.

  2. Sign in to the Azure portal and search for the container app resource by name.

  3. In the Container Apps dashboard, select Monitoring > Log stream.

    Screenshot of navigating to the Log stream page in the Azure portal.

  4. Confirm the checkout container is logging the same output as in the terminal earlier.

    Screenshot of the checkout service container's log stream in the Azure portal.

  5. Do the same for the order-processor service.

    Screenshot of the order processor service container's log stream in the Azure portal.

What happened?

Upon successful completion of the azd up command:

  • Azure Developer CLI provisioned the Azure resources referenced in the sample project's ./infra directory to the Azure subscription you specified. You can now view those Azure resources via the Azure portal.
  • The app deployed to Azure Container Apps. From the portal, you can browse the fully functional app.

Run the Python applications locally

Before deploying the application to Azure Container Apps, start by running the order-processor and checkout services locally with Dapr.

Prepare the project

  1. Clone the sample applications to your local machine.

    git clone https://github.com/Azure-Samples/svc-invoke-dapr-python.git
    
  2. Navigate into the sample's root directory.

    cd svc-invoke-dapr-python
    

Run the applications using the Dapr CLI

Start by running the order-processor service.

  1. From the sample's root directory, change directories to order-processor.

    cd order-processor
    
  2. Install the dependencies.

    pip3 install -r requirements.txt
    
  3. Run the order-processor service.

    dapr run --app-port 8001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- python3 app.py
    
  4. In a new terminal window, from the sample's root directory, navigate to the checkout caller service.

    cd checkout
    
  5. Install the dependencies.

    pip3 install -r requirements.txt
    
  6. Run the checkout service.

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

    Expected output

    In both terminals, the checkout service is calling orders to the order-processor service in a loop.

    checkout output:

    == 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 output:

    == 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. Press Cmd/Ctrl + C in both terminals to exit out of the service-to-service invocation

Deploy the application template using Azure Developer CLI

Deploy the application to Azure Container Apps using azd.

Prepare the project

  1. In a new terminal window, navigate into the sample's root directory.

    cd svc-invoke-dapr-python
    

Provision and deploy using Azure Developer CLI

  1. Run azd init to initialize the project.

    azd init
    
  2. When prompted in the terminal, provide the following parameters.

    Parameter Description
    Environment Name Prefix for the resource group created to hold all Azure resources.
    Azure Location The Azure location for your resources.
    Azure Subscription The Azure subscription for your resources.
  3. Run azd up to provision the infrastructure and deploy the application to Azure Container Apps in a single command.

    azd up
    

    This process may take some time to complete. As the azd up command completes, the CLI output displays two Azure portal links to monitor the deployment progress. The output also demonstrates how azd up:

    • Creates and configures all necessary Azure resources via the provided Bicep files in the ./infra directory using azd provision. Once provisioned by Azure Developer CLI, you can access these resources via the Azure portal. The files that provision the Azure resources include:
      • main.parameters.json
      • main.bicep
      • An app resources directory organized by functionality
      • A core reference library that contains the Bicep modules used by the azd template
    • Deploys the code using azd deploy

    Expected output

    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
    

Confirm successful deployment

In the Azure portal, verify the checkout service is passing orders to the order-processor service.

  1. Copy the checkout container app's name from the terminal output.

  2. Sign in to the Azure portal and search for the container app resource by name.

  3. In the Container Apps dashboard, select Monitoring > Log stream.

    Screenshot of navigating to the Log stream page in the Azure portal.

  4. Confirm the checkout container is logging the same output as in the terminal earlier.

    Screenshot of the checkout service container's log stream in the Azure portal.

  5. Do the same for the order-processor service.

    Screenshot of the order processor service container's log stream in the Azure portal.

What happened?

Upon successful completion of the azd up command:

  • Azure Developer CLI provisioned the Azure resources referenced in the sample project's ./infra directory to the Azure subscription you specified. You can now view those Azure resources via the Azure portal.
  • The app deployed to Azure Container Apps. From the portal, you can browse the fully functional app.

Run the .NET applications locally

Before deploying the application to Azure Container Apps, start by running the order-processor and checkout services locally with Dapr.

Prepare the project

  1. Clone the sample applications to your local machine.

    git clone https://github.com/Azure-Samples/svc-invoke-dapr-csharp.git
    
  2. Navigate into the sample's root directory.

    cd svc-invoke-dapr-csharp
    

Run the applications using the Dapr CLI

Start by running the order-processor callee service.

  1. From the sample's root directory, change directories to order-processor.

    cd order-processor
    
  2. Install the dependencies.

    dotnet build
    
  3. Run the order-processor service.

    dapr run --app-port 7001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- dotnet run
    
  4. In a new terminal window, from the sample's root directory, navigate to the checkout caller service.

    cd checkout
    
  5. Install the dependencies.

    dotnet build
    
  6. Run the checkout service.

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

    Expected output

    In both terminals, the checkout service is calling orders to the order-processor service in a loop.

    checkout output:

    == 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 output:

    == 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. Press Cmd/Ctrl + C in both terminals to exit out of the service-to-service invocation.

Deploy the application template using Azure Developer CLI

Deploy the application to Azure Container Apps using azd.

Prepare the project

In a new terminal window, navigate into the sample's root directory.

cd svc-invoke-dapr-csharp

Provision and deploy using Azure Developer CLI

  1. Run azd init to initialize the project.

    azd init
    
  2. When prompted in the terminal, provide the following parameters.

    Parameter Description
    Environment Name Prefix for the resource group created to hold all Azure resources.
    Azure Location The Azure location for your resources.
    Azure Subscription The Azure subscription for your resources.
  3. Run azd up to provision the infrastructure and deploy the application to Azure Container Apps in a single command.

    azd up
    

    This process may take some time to complete. As the azd up command completes, the CLI output displays two Azure portal links to monitor the deployment progress. The output also demonstrates how azd up:

    • Creates and configures all necessary Azure resources via the provided Bicep files in the ./infra directory using azd provision. Once provisioned by Azure Developer CLI, you can access these resources via the Azure portal. The files that provision the Azure resources include:
      • main.parameters.json
      • main.bicep
      • An app resources directory organized by functionality
      • A core reference library that contains the Bicep modules used by the azd template
    • Deploys the code using azd deploy

    Expected output

    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
    

Confirm successful deployment

In the Azure portal, verify the checkout service is passing orders to the order-processor service.

  1. Copy the checkout container app's name from the terminal output.

  2. Sign in to the Azure portal and search for the container app resource by name.

  3. In the Container Apps dashboard, select Monitoring > Log stream.

    Screenshot of navigating to the Log stream page in the Azure portal.

  4. Confirm the checkout container is logging the same output as in the terminal earlier.

    Screenshot of the checkout service container's log stream in the Azure portal.

  5. Do the same for the order-processor service.

    Screenshot of the order processor service container's log stream in the Azure portal.

What happened?

Upon successful completion of the azd up command:

  • Azure Developer CLI provisioned the Azure resources referenced in the sample project's ./infra directory to the Azure subscription you specified. You can now view those Azure resources via the Azure portal.
  • The app deployed to Azure Container Apps. From the portal, you can browse the fully functional app.

Clean up resources

If you're not going to continue to use this application, delete the Azure resources you've provisioned with the following command:

azd down

Next steps