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

Assignment 5 - Service Invocation using Dapr

Table of contents

This assignment is about using Dapr to invoke the VehicleRegistrationService from the FineCollectionService. You will use the service invocation building block provided by Dapr. This is the second step to reach the final state of the application for this challge. It is represented by the diagram below.

Azure Container Apps Challenge - Second Deployment

Step 1: Use Dapr to invoke the Vehicle Registration Service from the Fine Collection Service

With Dapr, services can invoke other services using their application id. This is done by using the Dapr client to make calls to the Dapr sidecar. The Vehicle Registration Service will be started with a Dapr sidecar.

  1. Open the FineCollectionService project in your code editor and navigate to the DaprVehicleRegistrationClient class. This class implements the VehicleRegistrationClient interface and uses the Dapr client to invoke the Vehicle Registration Service. Inspect the implementation of this class.

  2. Navigate to the FineCollectionConfiguration class to switch between the default and Dapr implementation of the VehicleRegistrationClient.

  3. Uncomment following @Bean method:

     //    @Bean
     //    public VehicleRegistrationClient vehicleRegistrationClient(final DaprClient daprClient) {
     //        return new DaprVehicleRegistrationClient(daprClient);
     //    }
    
  4. Uncomment following @Bean method, if not already done:

     //    @Bean
     //    public DaprClient daprClient() {
     //        return new DaprClientBuilder().build();
     //    }
    
  5. Comment out following @Bean method:

         @Bean
         public VehicleRegistrationClient vehicleRegistrationClient(final RestTemplate restTemplate) {
             return new DefaultVehicleRegistrationClient(restTemplate, vehicleInformationAddress);
         }
    
  6. Check all your code-changes are correct by building the code. Execute the following command in the terminal window:

     mvn package
    

Step 2: Enable Dapr for Vehicle Registration Service

In this step, you will enable Dapr for the VehicleRegistrationService to be discoverable by the FineCollectionService using Dapr’s service invocation building block.

FineCollectionService Dapr sidecar uses Vehicle Registration Service dapr-app-id to resolve the service invocation endpoint. The name (i.e. dapr-app-id) of VehicleRegistrationService is set in the application properties of FineCollectionService (i.e. application.yaml) as shown below:

vehicle-registration-service.name: ${VEHICLE_REGISTRATION_SERVICE:vehicleregistrationservice}

The default value is vehicleregistrationservice that will be override using the environment variable VEHICLE_REGISTRATION_SERVICE to the name set in the following step:

  1. Open a new termninale and run the following command to enable Dapr for VehicleRegistrationService:

     az containerapp dapr enable \
       --name ca-vehicle-registration-service \
       --resource-group rg-dapr-workshop-java \
       --dapr-app-id vehicle-registration-service \
       --dapr-app-port 6002 \
       --dapr-app-protocol http
    
  2. Note the dapr-app-id you set in the previous step. It is used to resolve the service invocation endpoint.

Step 3: Build and redeploy fine collection service

In this step, you will rebuild and redeploy the FineCollectionService to use the VehicleRegistrationService service invocation endpoint.

  1. Delete the image from local docker:

     docker rmi fine-collection-service:1.0-SNAPSHOT
    
  2. In the root folder of FineCollectionService, run the following command to build and push the image:

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

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

  3. Update FineCollectionService container app with the new image and with the environment variable to set the name (i.e. dapr-app-id) of VehicleRegistrationService:

     az containerapp update \
       --name ca-fine-collection-service \
       --resource-group rg-dapr-workshop-java \
       --image "$CONTAINER_REGISTRY.azurecr.io/fine-collection-service:2.0" \
       --set-env-vars "VEHICLE_REGISTRATION_SERVICE=vehicle-registration-service" \
       --remove-env-vars "VEHICLE_REGISTRATION_SERVICE_BASE_URL"
    
    

    Where $CONTAINER_REGISTRY is the name of your Azure Container Registry. The VEHICLE_REGISTRATION_SERVICE_BASE_URL is removed because it is not used anymore.

Step 4 - 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 5 - 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 FineCollectionService and the VehicleRegistrationService.

< Assignment 4 - Deploy to Azure Container Apps Assignment 6 - Cosmos DB as a state store >