Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Deploying Azure Cosmos DB state store to Azure Container Apps

Table of contents

In this bonus assignment, you will deploy the Azure Cosmos DB state store to Azure Container Apps (ACA). You will use the state management building block provided by Dapr.

Pre-requisites

Step 1: Deploy Azure Cosmos DB state store component to ACA

  1. Remove azure-cosmosdb-statestore.yaml from dapr/components folder.

  2. Open the file dapr/aca-azure-cosmosdb-statestore.yaml in your code editor and compare the content of the file with the content of the file dapr/azure-cosmosdb-statestore.yaml from the previous assignment.

  3. Copy or Move this file dapr/aca-azure-cosmosdb-statestore.yaml to dapr/components folder.

  4. Replace the following placeholders in this file dapr/components/aca-azure-cosmosdb-statestore.yaml with the values you noted down in the previous assignment:

    • <YOUR_COSMOSDB_ACCOUNT_URL> with the Cosmos DB account URL
    • <YOUR_COSMOSDB_MASTER_KEY> with the master key
  5. Go to the root folder of the repository.

  6. Enter the following command to deploy the statestore Dapr component:

     az containerapp env dapr-component set \
       --name cae-dapr-workshop-java \
       --resource-group rg-dapr-workshop-java \
       --dapr-component-name statestore \
       --yaml ./dapr/components/aca-azure-cosmosdb-statestore.yaml
    

Step 2: Build and redeploy traffic control service

In this step, you will rebuild and redeploy the TrafficControlService to use the Azure Cosmos DB state store instead of keeping the state in memory.

  1. Delete the image from local docker:

     docker rmi traffic-control-service:1.0-SNAPSHOT
    
  2. In the root folder of TrafficControlService, run the following command to build and push the image:

     mvn spring-boot:build-image
     docker tag traffic-control-service:1.0-SNAPSHOT "$CONTAINER_REGISTRY.azurecr.io/traffic-control-service:2.0"
     docker push "$CONTAINER_REGISTRY.azurecr.io/traffic-control-service:2.0"
    

    Where $CONTAINER_REGISTRY is the name of your Azure Container Registry.

  3. Update TrafficControlService container with the new image:

     az containerapp update \
       --name ca-traffic-control-service \
       --resource-group rg-dapr-workshop-java \
       --image "$CONTAINER_REGISTRY.azurecr.io/traffic-control-service:2.0"
    

    Where $CONTAINER_REGISTRY is the name of your Azure Container Registry.

Step 3 - Run the simulation

  1. Set the following environment variable:

    • Linux/Unix shell:

      export TRAFFIC_CONTROL_SERVICE_BASE_URL=https://$TRAFFIC_CONTROL_SERVICE_FQDN
      
    • Powershell:

      $env:TRAFFIC_CONTROL_SERVICE_BASE_URL = "https://$TRAFFIC_CONTROL_SERVICE_FQDN"
      
  2. In the root folder of the simulation (Simulation), start the simulation:

     mvn spring-boot:run
    

Step 4 - Test the microservices running in ACA

You can access the log of the container apps from the Azure Portal or directly in a terminal window. To access the logs in the portal, you need to go to your resource group rg-dapr-workshop-java and select the container app for which you need the log. Then select Log stream in Monitoring section.

To access the logs from the terminal, follow the instructions below for each microservice.

The logs can take a few minutes to appear in the Log Analytics Workspace. If the logs are not updated, open the log stream in the Azure Portal.

Traffic Control Service

  1. Run the following command to identify the running revision of traffic control service container apps:

    • Linux/Unix shell:

      TRAFFIC_CONTROL_SERVICE_REVISION=$(az containerapp revision list -n ca-traffic-control-service -g rg-dapr-workshop-java --query "[0].name" -o tsv)
      echo $TRAFFIC_CONTROL_SERVICE_REVISION
      
    • Powershell:

      $TRAFFIC_CONTROL_SERVICE_REVISION = az containerapp revision list -n ca-traffic-control-service -g rg-dapr-workshop-java --query "[0].name" -o tsv
      $TRAFFIC_CONTROL_SERVICE_REVISION
      
  2. Run the following command to get the last 10 lines of traffic control service logs from Log Analytics Workspace:

     az monitor log-analytics query \
       --workspace $LOG_ANALYTICS_WORKSPACE_CUSTOMER_ID \
       --analytics-query "ContainerAppConsoleLogs_CL | where RevisionName_s == '$TRAFFIC_CONTROL_SERVICE_REVISION' | project TimeGenerated, Log_s | sort by TimeGenerated desc | take 10" \
       --out table
    

Fine Collection Service

  1. Run the following command to identify the running revision of fine collection service container apps:

    • Linux/Unix shell:

      FINE_COLLECTION_SERVICE_REVISION=$(az containerapp revision list -n ca-fine-collection-service -g rg-dapr-workshop-java --query "[0].name" -o tsv)
      echo $FINE_COLLECTION_SERVICE_REVISION
      
    • Powershell:

      $FINE_COLLECTION_SERVICE_REVISION = az containerapp revision list -n ca-fine-collection-service -g rg-dapr-workshop-java --query "[0].name" -o tsv
      $FINE_COLLECTION_SERVICE_REVISION
      
  2. Run the following command to get the last 10 lines of fine collection service logs from Log Analytics Workspace:

     az monitor log-analytics query \
       --workspace $LOG_ANALYTICS_WORKSPACE_CUSTOMER_ID \
       --analytics-query "ContainerAppConsoleLogs_CL | where RevisionName_s == '$FINE_COLLECTION_SERVICE_REVISION' | project TimeGenerated, Log_s | sort by TimeGenerated desc | take 10" \
       --out table
    

Vehicle Registration Service

  1. Run the following command to identify the running revision of vehicle registration service container apps:

    • Linux/Unix shell:

      VEHICLE_REGISTRATION_SERVICE_REVISION=$(az containerapp revision list -n ca-vehicle-registration-service -g rg-dapr-workshop-java --query "[0].name" -o tsv)
      echo $VEHICLE_REGISTRATION_SERVICE_REVISION
      
    • Powershell:

      $VEHICLE_REGISTRATION_SERVICE_REVISION = az containerapp revision list -n ca-vehicle-registration-service -g rg-dapr-workshop-java --query "[0].name" -o tsv
      $VEHICLE_REGISTRATION_SERVICE_REVISION
      
  2. Run the following command to get the last 10 lines of vehicle registration service logs from Log Analytics Workspace:

     az monitor log-analytics query \
       --workspace $LOG_ANALYTICS_WORKSPACE_CUSTOMER_ID \
       --analytics-query "ContainerAppConsoleLogs_CL | where RevisionName_s == '$VEHICLE_REGISTRATION_SERVICE_REVISION' | project TimeGenerated, Log_s | sort by TimeGenerated desc | take 10" \
       --out table
    

Check Application Map of Application Insights in Azure Portal to see the connection between the TrafficControlService and the aca-azure-cosmosdb-statestore. Check in Azure Portal the data in Cosmos DB.

Cleanup

When the workshop is done, please follow the cleanup instructions to delete the resources created in this workshop.

< Cosmos DB as a state store