Tutorial: Implement Dapr components for Go Applications

Tutorials

/

Posted on

August 10, 2023

Distributed Application Runtime (Dapr) is an open-source framework that simplifies the development of resilient, scalable microservices across various platforms. A standout feature of the Dapr SDK is its flexibility; as long as components provide the same type, the underlying service for handling data can be swapped without changing the application code, such as switching from Azure Servicebus to Azure Redis for Pub/Sub messaging.  

This tutorial, a continuation of our previous post on deploying Azure Container Apps with Dapr and Terraform, explores practical examples of working with Dapr components for Go applications. Whether you're a beginner or looking to expand your existing knowledge, this guide offers insights and hands-on guidance to leverage the power of Dapr in your containerized workloads.

Implementing Queue Output Bindings in Dapr

To utilize the message queue pattern and component in Dapr, we'll need both a client and a service that leverage bindings. In Dapr context, bindings serve as versatile connectors, facilitating both the input and output of data, acting as bridges between different parts of the application or even different applications.

We'll start by implementing the client within the ‘endpoint’ service of our sample application. Following that, we'll create the corresponding service inside the worker service. The examples presented in the subsequent sections demonstrate these implementations in their most straightforward and essential forms, offering a foundation that can be expanded and customized according to your specific needs.

Client Implementation

Download the necessary dependencies and implement the client:

Prerequisites:

  • The `data` that we assign before sending the message can be a simple text string or serializable data that can be deserialized by the subscriber (like JSON).
  • `<name>`: The name of the Dapr component (in the sample application and setup, `reports`)
  • `<action>`: The action to perform with the binding. This varies between components. For output to an Azure Servicebus queue, it is `create`.
  • The `Metadata` map should contain key/values (strings) the binding component requires. In this case, the queue name.

Just to reiterate, the `name` parameter must be the same name as given to the Dapr binding component setup together with the Container App.

Service Implementation

Download the necessary dependencies and implement the service:  

Prerequisites:

  • A handler must be registered to each event and binding supported by the service, in this case, the `queueHandler` function.  

The sample application has additional structure and functionality to ease the component's use from within the application (and graceful shutdown of the service). For an example of how this can be done, see the source code for the server.  

For more information on components and what types are available, check the Dapr documentation.

Implementing The Azure Storage Binding Component

Implementing the Azure Storage binding component is like how the Azure Servicebus output binding client was created. Download the necessary dependencies and implement the client:  

Prerequisites:

  • The `Metadata` map should contain key/values (strings) the binding component requires. For blob storage:

For more information on the different binding components, check the Dapr documentation.

Summary

Throughout this tutorial, we've uncovered the dynamic capabilities of Dapr components within Go applications, emphasizing the framework's adaptability and robustness. The ability to interchange underlying services without modifying application code not only simplifies the development process but also offers a pathway to scalable and efficient solutions.

My sample application serves as a blueprint, showcasing how these principles can be applied practically. It's evident that the synergy between Azure Container Apps and Dapr holds immense potential, and I'm committed to leveraging their combined strengths in my future projects.

If this exploration has ignited your interest, the preceding post in this series is a rich resource awaiting your discovery. Your feedback is always welcome, and I'm here for any further inquiries. Stay tuned for more insights and happy coding!

Written by

Karl Wallenius