Dapr in IoT. Understanding of concept building distributed application in IoT
This story I would like to share my personal experience and perception of using Dapr in IoT application. In our days we can see so many different systems, services, solutions, databases like native, serverless, stateless, distributed, etc. And of course microservice’s architecture is the best approach of coming across these ones in one solution. Usually, it is difficult to combine them together and better to leave them independently. But the problem is that they should communicate each other and we should organise such communication via different protocols, languages, frameworks. This is one challenge. Another one is that our solution is always depending on changes from services that we work with and we should manage and control these as well.
Before jumping inside the code, I have a try to use simple words about Dapr explanation just to be sure that everyone understands this platform. Dapr is translated like distributed application portable or platform runtime in IT terminology I hope you knew it, it is not Department of the Army Program Review or something else that you can find in some vocabularies. Let’s explain word by word. Distributed means “shared or spread out”, in this concept it is something like having several independent services that can use one or more the same resources during the work. These resources are distributed. Next word is application. That means Dapr works on application layer, not network (such as mesh services) or something else. Last thing that I would like to mention is that you can develop own business logic using your favourite language, Dapr supports it. Next one is portable / platform means you can integrate different systems into your application without hard coding, remember a possibility to configure Kafka messages to Azure Event hub. You only need to change configuration file and that is all. (https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-for-kafka-ecosystem-overview). And last one is runtime means you can run Dapr on all devices does not matter what kind of platform you use even on Raspberry Pi.
Let’s get started. First, what you need to do is install all necessary tools that described on this article https://docs.dapr.io/getting-started/install-dapr-selfhost/ . Dapr uses docker to run own containers and you need to install them as well. Also, you need to install IoT components such as Arduino IDE, scratches and all configuration to connect your device to the cloud. I use Mx chip developer kit https://microsoft.github.io/azure-iot-developer-kit/ and also I use Azure IoT Hub and Event Hub to send messages from device.
If all equipments are configured let’s move to the next step. Next, needs to build own application. Well, in this application I will create message management that can receive data from devices via Azure Event Hub. Somebody can tell that you can develop the same using .net SDK + processor method to receive the data. I can say yes, you can but you need to have some understanding how to do it and also you need to develop some piece of code. Better is to have one file with needed configuration and that is all. Let’s do it. As you understood, I will use binding with Azure Event Hub and my configuration component is something like bellow,
Using bindings, you can trigger your app with events coming in from external systems, or interface with external systems. Benefits of using such block are bellow,
- Remove the complexities of connecting to, and polling from, messaging systems such as queues and message buses
- Focus on business logic and not implementation details of how to interact with a system
- Keep your code free from SDKs or libraries
- Handle retries and failure recovery
- Switch between bindings at run time
- Build portable applications where environment-specific bindings are set-up and no code changes are required
Next, you need to add Dapr integration into your code by using special builder extension,
The last thing that you need to add is how to handle the incoming messages from Azure Event Hub. That is easy to create one method to do it,
To test and run this application needs to execute the command,
dapr run — app-id eventBindingApp — app-port 5000 — port 3500 — components-path components dotnet run
That pretty all, good luck.
P.S. More information you can find here https://docs.dapr.io