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.
-
Open the
FineCollectionService
project in your code editor and navigate to theDaprVehicleRegistrationClient
class. This class implements theVehicleRegistrationClient
interface and uses the Dapr client to invoke the Vehicle Registration Service. Inspect the implementation of this class. -
Navigate to the
FineCollectionConfiguration
class to switch between the default and Dapr implementation of theVehicleRegistrationClient
. -
Uncomment following @Bean method:
// @Bean // public VehicleRegistrationClient vehicleRegistrationClient(final DaprClient daprClient) { // return new DaprVehicleRegistrationClient(daprClient); // }
-
Uncomment following @Bean method, if not already done:
// @Bean // public DaprClient daprClient() { // return new DaprClientBuilder().build(); // }
-
Comment out following @Bean method:
@Bean public VehicleRegistrationClient vehicleRegistrationClient(final RestTemplate restTemplate) { return new DefaultVehicleRegistrationClient(restTemplate, vehicleInformationAddress); }
-
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.
-
Make sure no services from previous tests are running (close the command-shell windows).
-
Open the terminal window and make sure the current folder is
VehicleRegistrationService
. -
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 Serviceapp-id
to resolve the service invocation endpoint. The name (i.e.app-id
) ofVehicleRegistrationService
is set in the application properties ofFineCollectionService
(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 variableVEHICLE_REGISTRATION_SERVICE
. -
Open a new terminal window and change the current folder to
FineCollectionService
. -
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
-
Open a new terminal window and change the current folder to
TrafficControlService
. -
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
-
Open a new terminal window and change the current folder to
Simulation
. -
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.