< Previous Challenge - Home - Next Challenge >
In this challenge, you’re going to add Dapr publish/subscribe messaging to send messages from the TrafficControlService
to the FineCollectionService
.
In challenge 2, you implemented direct, synchronous communication between two microservices. This pattern is common when an immediate response is required. Communication between services doesn’t always require an immediate response.
The publish/subscribe pattern allows your microservices to communicate asynchronously with each other purely by sending messages. In this system, the producer of a message sends it to a topic, with no knowledge of what service(s) will consume the message. A message can even be sent if there’s no consumer for it.
Similarly, a subscriber or consumer will receive messages from a topic without knowledge of what producer sent it. This pattern is especially useful when you need to decouple microservices from one another. See the diagram below for an overview of how this pattern works with Dapr:
You will need to modify the services to use the Dapr pub/sub building block.
TrafficControlService
(TrafficController
class) so it sends SpeedingViolation
messages using the Dapr pub/sub building block.FineCollectionService
(CollectionController
class) so it receives SpeedingViolation
messages using the Dapr pub/sub building block and unwraps it from the CloudEvents message format.This challenge targets the operations labeled as number 2 in the end-state setup:
TrafficControlService
to the FineCollectionService
using Dapr, not direct service invocation.docker run -d -p 5672:5672 -p 15672:15672 --name dtc-rabbitmq rabbitmq:3-management
guest
and the password is guest
. Shown below, the dashboard is helpful for troubleshooting RabbitMQ anomalies:
Resources/dapr/components
directory (you will see some existing files related to the Azure Kubernetes Service deployment in Challenge-08, you can ignore these for now and put your files here as well)dapr run ... --components-path ../dapr/components dotnet run
SpeedingViolation
class.
[FromBody] System.Text.Json.JsonDocument cloudEvent
.cloudEvent.RootElement.GetProperty("data").GetProperty("vehicleId").GetString()