Rediģēt

Kopīgot, izmantojot


Tutorial: Event-driven work using Dapr Bindings

In this tutorial, you create a microservice to demonstrate Dapr's Bindings API to work with external systems as inputs and outputs. You'll:

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

The service listens to input binding events from a system CRON and then outputs the contents of local data to a PostreSql output binding.

Diagram of the binding application.

Prerequisites

Run the Node.js application locally

Before deploying the application to Azure Container Apps, start by running the PostgreSQL container and JavaScript service locally with Docker Compose and Dapr.

Prepare the project

  1. Clone the sample application to your local machine.

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

    cd bindings-dapr-nodejs-cron-postgres
    

Run the application using the Dapr CLI

  1. From the sample's root directory, change directories to db.

    cd db
    
  2. Run the PostgreSQL container with Docker Compose.

    docker compose up -d
    
  3. Open a new terminal window and navigate into /batch in the sample directory.

    cd bindings-dapr-nodejs-cron-postgres/batch
    
  4. Install the dependencies.

    npm install
    
  5. Run the JavaScript service application.

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

    The dapr run command runs the binding application locally. Once the application is running successfully, the terminal window shows the output binding data.

    Expected output

    The batch service listens to input binding events from a system CRON and then outputs the contents of local data to a PostgreSQL output binding.

    == 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. In the ./db terminal, stop the PostgreSQL container.

    docker compose stop
    

Deploy the application template using Azure Developer CLI

Now that you've run the application locally, let's deploy the bindings application to Azure Container Apps using azd. During deployment, we will swap the local containerized PostgreSQL for an Azure PostgreSQL component.

Prepare the project

Navigate into the sample's root directory.

cd bindings-dapr-nodejs-cron-postgres

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. Make sure you select a location available for Azure PostgreSQL.
    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/#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
    

Confirm successful deployment

In the Azure portal, verify the batch container app is logging each insert into Azure PostgreSQL every 10 seconds.

  1. Copy the Container App 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 App dashboard, select Monitoring > Log stream.

    Screenshot of the navigating to the log streams from the Azure Container Apps side menu.

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

    Screenshot of the container app'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 application locally

Prepare the project

  1. Clone the sample application to your local machine.

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

    cd bindings-dapr-python-cron-postgres
    

Run the application using the Dapr CLI

Before deploying the application to Azure Container Apps, start by running the PostgreSQL container and Python service locally with Docker Compose and Dapr.

  1. From the sample's root directory, change directories to db.

    cd db
    
  2. Run the PostgreSQL container with Docker Compose.

    docker compose up -d
    
  3. Open a new terminal window and navigate into /batch in the sample directory.

    cd bindings-dapr-python-cron-postgres/batch
    
  4. Install the dependencies.

    pip install -r requirements.txt
    
  5. Run the Python service application.

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

    The dapr run command runs the binding application locally. Once the application is running successfully, the terminal window shows the output binding data.

    Expected output

    The batch service listens to input binding events from a system CRON and then outputs the contents of local data to a PostgreSQL output binding.

    == 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. In the ./db terminal, stop the PostgreSQL container.

    docker compose stop
    

Deploy the application template using Azure Developer CLI

Now that you've run the application locally, let's deploy the bindings application to Azure Container Apps using azd. During deployment, we will swap the local containerized PostgreSQL for an Azure PostgreSQL component.

Prepare the project

Navigate into the sample's root directory.

cd bindings-dapr-python-cron-postgres

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. Make sure you select a location available for Azure PostgreSQL.
    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/#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
    

Confirm successful deployment

In the Azure portal, verify the batch container app is logging each insert into Azure PostgreSQL every 10 seconds.

  1. Copy the Container App 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 App dashboard, select Monitoring > Log stream.

    Screenshot of the navigating to the log streams from the Azure Container Apps side menu.

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

    Screenshot of the container app'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 application locally

Prepare the project

  1. Clone the sample application to your local machine.

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

    cd bindings-dapr-csharp-cron-postgres
    

Run the application using the Dapr CLI

Before deploying the application to Azure Container Apps, start by running the PostgreSQL container and .NET service locally with Docker Compose and Dapr.

  1. From the sample's root directory, change directories to db.

    cd db
    
  2. Run the PostgreSQL container with Docker Compose.

    docker compose up -d
    
  3. Open a new terminal window and navigate into /batch in the sample directory.

    cd bindings-dapr-csharp-cron-postgres/batch
    
  4. Install the dependencies.

    dotnet build
    
  5. Run the .NET service application.

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

    The dapr run command runs the binding application locally. Once the application is running successfully, the terminal window shows the output binding data.

    Expected output

    The batch service listens to input binding events from a system CRON and then outputs the contents of local data to a PostgreSQL output binding.

    == 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. In the ./db terminal, stop the PostgreSQL container.

    docker compose stop
    

Deploy the application template using Azure Developer CLI

Now that you've run the application locally, let's deploy the bindings application to Azure Container Apps using azd. During deployment, we will swap the local containerized PostgreSQL for an Azure PostgreSQL component.

Prepare the project

Navigate into the sample's root directory.

cd bindings-dapr-csharp-cron-postgres

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. Make sure you select a location available for Azure PostgreSQL.
    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/#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
    

Confirm successful deployment

In the Azure portal, verify the batch container app is logging each insert into Azure PostgreSQL every 10 seconds.

  1. Copy the Container App 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 App dashboard, select Monitoring > Log stream.

    Screenshot of the navigating to the log streams from the Azure Container Apps side menu.

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

    Screenshot of the container app'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