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

Invoke Vehicle Registration Service from Fine Collection Service

Table of contents

In this assignment, you will use Dapr to invoke the VehicleRegistrationService from the FineCollectionService. You will use the service invocation building block provided by Dapr.

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
    

Now you can test the application.

Step 2: Test the application

You’re going to start all the services now.

  1. Make sure no services from previous tests are running (close the command-shell windows).

  2. Open the terminal window and make sure the current folder is VehicleRegistrationService.

  3. Enter the following command to run the VehicleRegistrationService with a Dapr sidecar:

    dapr run --app-id vehicleregistrationservice --app-port 6002 --dapr-http-port 3602 --dapr-grpc-port 60002 --resources-path ../dapr/components mvn spring-boot:run
    

    FineCollectionService Dapr sidecar uses Vehicle Registration Service app-id to resolve the service invocation endpoint. The name (i.e. 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 can be override using the environment variable VEHICLE_REGISTRATION_SERVICE.

  4. Open a new terminal window and change the current folder to FineCollectionService.

  5. Enter the following command to run the FineCollectionService with a Dapr sidecar:

    dapr run --app-id finecollectionservice --app-port 6001 --dapr-http-port 3601 --dapr-grpc-port 60001 --resources-path ../dapr/components mvn spring-boot:run
    
  6. Open a new terminal window and change the current folder to TrafficControlService.

  7. Enter the following command to run the TrafficControlService with a Dapr sidecar:

    dapr run --app-id trafficcontrolservice --app-port 6000 --dapr-http-port 3600 --dapr-grpc-port 60000 --resources-path ../dapr/components mvn spring-boot:run
    
  8. Open a new terminal window and change the current folder to Simulation.

  9. Start the simulation:

    mvn spring-boot:run
    

You should see the same logs as before. Obviously, the behavior of the application is exactly the same as before.

Deploy to AKS Deploy to ACA