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
- The first part Use Azure Cosmos DB to store the state of a vehicle using Dapr is a pre-requisite for this assignment. The account URL and the master key of the Azure Cosmos DB instance are required for this assignment. Please use the same Azure Cosmos DB instance as used in the first part of this assignment.
- Assignment 5 - Deploying to Azure Container Apps is also a pre-requisite for this assignment.
Step 1: Deploy Azure Cosmos DB state store component to ACA
-
Remove
azure-cosmosdb-statestore.yaml
fromdapr/components
folder. -
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 filedapr/azure-cosmosdb-statestore.yaml
from the previous assignment. -
Copy or Move this file
dapr/aca-azure-cosmosdb-statestore.yaml
todapr/components
folder. -
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
-
Go to the root folder of the repository.
-
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.
-
Delete the image from local docker:
docker rmi traffic-control-service:1.0-SNAPSHOT
-
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. -
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
-
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"
-
-
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
-
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
-
-
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
-
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
-
-
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
-
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
-
-
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.