< Previous Challenge - Home - Next Challenge >
In this challenge, you’re going to add a Dapr input binding in the TrafficControlService
. It’ll receive entry-cam and exit-cam messages over the MQTT protocol.
In this challenge you’ll focus on Dapr input bindings. The following diagram depicts how input bindings work:
For this hands-on challenge, you will add an input binding leveraging the Dapr binding building block.
Simulation
app to put trafficcontrol/entrycam
& trafficcontrol/exitcam
messages on the MQTT queue.
MqttTrafficControlService
class to do this (look at the HttpTrafficControlService
as an example).Program
class to use this new service.Simulation
application.Simulation
to use Azure IoT Hub & modify the Dapr configuration to use Azure Event Hub as the MQTT message broker.This challenge targets the operation labeled as number 5 in the end-state setup:
Local
Azure
TrafficControlService
receives messages via its Dapr component.Simulation
application publishes entry-cam and exit-cam messages to the MQTT broker.In order to connect to Mosquitto, you need to pass in a custom configuration file when starting it. With Docker, you can pass a configuration file when starting a container using a Volume mount. The folder Resources/Infrastructure/mosquitto
already contains a config file you can use.
Open a terminal window in VS Code and make sure the current folder is Resources/Infrastructure/mosquitto
.
Start a Mosquitto MQTT container by entering the following command: When running on Windows PowerShell:
docker run -d -p 1883:1883 -p 9001:9001 -v $pwd/:/mosquitto/config/ --name dtc-mosquitto eclipse-mosquitto
When running on Mac or Linux:
docker run -d -p 1883:1883 -p 9001:9001 -v $(pwd)/:/mosquitto/config/ --name dtc-mosquitto eclipse-mosquitto
This will pull the docker image eclipse-mosquitto
from Docker Hub and start it. The name of the container will be dtc-mosquitto
. The server will be listening for connections on port 1883
for MQTT traffic.
The -v
flag specifies a Docker volume mount. It mounts the current folder (containing the config file) as the /mosquitto/config/
folder in the container. Mosquitto reads its config file from that folder.
To peak into the Mosquitto server, open a new terminal window and execute the following command:
docker logs dtc-mosquitto
Add the following MQTT configuration flags when connecting to the Mosquitto MQTT queue in the Simulation
app.
var configuration = new MqttConfiguration()
{
KeepAliveSecs = 60,
Port = 1883
};
Use Azure IoT Hub & Event Hub for deployments to Azure.
Create a IoT Device in Azure IoT Hub to represent your Simulation app.
az iot hub device-identity create --device-id simulation --hub-name <iot-hub-name>
Get the IoT Hub Connection String for the device you just created.
az iot hub device-identity connection-string show --device-id simulation --hub-name <iot-hub-name>
Microsoft.Azure.Devices.Client.Message
class allows you to add additional properties to facilitate message routing (look in the Azure portal, under the IoT Hub you provisioned, at the Message Routing
blade, Routing queries
section for details on which properties to add).