Chapter 1 - Introducing Dapr
Chapter 1 - Introducing Dapr
PREV NEXT
⏮ ⏭
Section 1: Introduction to Dapr Chapter 2: Debugging Dapr Solutions
🔎
An overview of Dapr
Setting up Dapr
Our first steps into the project will start with exploring Dapr and understanding
how it works.
Technical requirements
The code for this sample can be found in GitHub at
https://github.com/PacktPublishing/Practical-Microservices-with-Dapr-
and-.NET/tree/main/chapter01.
In this chapter, the working area for scripts and code is expected to be
<repository path>chapter01. In my local environment, it is C:Reposdapr-
sampleschapter01.
Please refer to the Setting up Dapr section for a complete guide to the tools
needed to develop with Dapr and work with the samples.
An overview of Dapr
Dapr is an event-driven, portable runtime created by Microsoft with an open
source approach and, at the time of writing this book, is still in active
development.
The following diagram shows the many building blocks of a Dapr architecture:
Dapr has been built on the experience gained by Microsoft in developing hy-
perscale cloud-native applications. It has been inspired by the design of
Orleans and Service Fabric, which in turn enables many Microsoft Azure
cloud services to operate resiliently and at a large scale.
Dapr offers developers an approach to design the tools to build and the run-
time to operate applications, based on a microservice architecture style.
What if you could leverage a runtime such as Dapr to help you get through
the common patterns you will likely need to adopt, and thus ease your
operations?
As we will experience later, it is not necessary to adopt the Dapr SDK in your
application: a call to a Dapr service can be as simple as an HTTP call to an
endpoint such as http://localhost:3500/v1.0/invoke/<app-
id>/method/<method name>. Nevertheless, using the SDK does provide
many benefits if you are writing a Dapr service or leveraging the Dapr actor
model.
Dapr is not a service mesh. While many similarities can be found in the
general objectives of Dapr and service meshes, Dapr does provide these
benefits at the application level while a service mesh operates on the
infrastructure. For instance, Dapr applies retry logic in its interaction with
state stores and services, but it is the developer's responsibility to decide
how to handle errors Dapr might return if there is a conflict or an
intermittent issue: whether raising an error back to the client,
compensating an operation, or adopting a retry policy (maybe leveraging
Polly in .NET), these are explicit choices only the developer can make.
Dapr is not a Microsoft cloud service: it does help the developer build
microservice applications in the cloud, and it surely provides many
integrations with Azure cloud services, but it also has as many
components for AWS, GCP, and other services.
It is also true that Dapr does not run better on Azure than on any other
Kubernetes environment in the cloud. I would hope to convince you that
Azure Kubernetes Service (AKS) is the best-managed Kubernetes offer-
ing in the cloud space, but this is a different conversation for another time.
Important note
While this book is heavily skewed toward .NET, Dapr does provide the
same benefits to Python developers (just as an example) as it provides
SDKs for Dapr and Dapr Actor, working on macOS, and with Kubernetes
as the deployment target—Dapr welcomes all developers in a vendor-
neutral and open approach.
The Dapr API: The API that defines how an application can interact with
the Dapr runtime in order to leverage its building blocks.
The Dapr runtime: This is the core of Dapr that implements the API. If
you are curious, you can take a look at how it is developed in Go at Dapr's
repository: https://github.com/dapr/dapr.
You can learn about this building block in Chapter 4, Introducing State
Management.
You can learn about this building block in Chapter 7, Using Actors.
Observability: Dapr enables the developer and operator to observe the
behavior of the system services and applications without having to
instrument them.
After learning about Dapr architecture and components, before we can start
using them, we need to set up Dapr in our development environment, which
will be the topic of the next section.
Setting up Dapr
Dapr is a runtime for every platform and every language. The focus of this
book is on C# in .NET, used with Visual Studio Code (VS Code). The code
snippets in the book can be appreciated by developers from any background,
but nevertheless, you will get the most out of it from a .NET perspective.
The development environment I use is Windows 10, as you will be able to tell
from the screenshots we use in the book. While the CLI, configuration, and
files will be the same, if you need more details on how to perform a particular
action on Linux or a macOS development machine, I encourage you to check
the Dapr documentation at https://docs.dapr.io/.
The samples and scripts in this book have been updated and tested with v1.1
of Dapr.
Docker
Dapr requires Docker locally on your development environment, therefore
make sure you have it installed. If your development machine is Windows,
Docker must be running in Linux container mode.
The previous command installs a release candidate (RC) version of the Dapr
CLI. Please refer to https://docs.dapr.io/getting-started/install-dapr-cli/ for
more details.
.NET
To install .NET 5, please refer to https://dotnet.microsoft.com/download for the
link to the latest binaries.
On a development machine, it makes sense to install the full SDK, which in-
cludes the runtime. Once the install is complete, open a new Command
Prompt and run the dotnet --info command. You should see the following
output:
Version: 5.0.202
Commit: db7cc87d51
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19042
OS Platform: Windows
RID: win10-x64
Version: 5.0.5
Commit: 2f740adc14
This proves .NET has been recognized and the framework is working fine.
VS Code
VS Code is a great multiplatform source code editor by Microsoft. You can in-
stall it for free by following the instructions at
https://code.visualstudio.com/docs/setup/windows.
Windows Terminal
I really love the new Windows Terminal (https://aka.ms/terminal) for its ease
of use and configurability. In the following chapters, we will often have to run
multiple commands and tools in parallel. Therefore, the tabs feature of
Windows Terminal is just one of the reasons why I suggest you adopt it too.
dapr init
In a local development environment, it might happen that the ports Dapr in-
tends to use for Redis (for example) are already in use. In this case, you
should identify which processes or containers are using the ports and change
them accordingly.
Once you launch the init command, this is the output you should expect:
dapr init -k
Alternatively, you can install Dapr on Kubernetes with a Helm v3 chart. You
can find more details at https://docs.dapr.io/getting-started/install-dapr-
kubernetes/#install-with-helm-advanced.
Important note
Reinstalling Dapr...
Creating c:dapr directory
Downloading https://api.github.com/repos/dapr/cli/releases/assets/34341976
...
Extracting c:daprdapr_windows_amd64.zip...
Clean up c:daprdapr_windows_amd64.zip...
Ensure that Docker Desktop is set to Linux containers mode when you run
Dapr in self hosted mode.
The process to update the Dapr runtime is similar to the initialization, but first,
we will need to uninstall Dapr from your machine, as follows:
Removing directory: C…
After we execute dapr init, checking the Dapr version, we can see it has now
moved forward from 1.0 to 1.1 for both the CLI and the runtime, as illustrated
in the following code snippet:
Our Dapr test environment is up and running: we are now ready to try it with
our first sample.
2. Then, we add the reference to the Dapr SDK for ASP.NET. The
current version is 1.1.0. You can look for the package versions on
NuGet at https://www.nuget.org/packages/Dapr.Actors.AspNetCore
with the dotnet add package command, as illustrated in the following
code snippet:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace dapr.microservice.webapi
{
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use //this method to
add services to the container.
{
services.AddControllers().AddDapr();
}
// This method gets called by the runtime. Use //this method to
configure the HTTP request pipeline.
IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapSubscribeHandler();
endpoints.MapControllers();
});
}
}
using Dapr;
using Microsoft.AspNetCore.Mvc;
using System;
namespace dapr.microservice.webapi.Controllers
[ApiController]
{
[HttpGet("hello")]
{
Console.WriteLine("Hello, World.");
}
}
I left the ASP.NET default port as 5000 but I changed the Dapr HTTP
port to 5010. The following command line launches the Dapr
application:
PS C:Reposdapr-sampleschapter01dapr.microservice.webapi>
dapr run --app-id hello-world --app-port 5000 --dapr-http-port
5010 dotnet run
The initial message informs you that Dapr is going to use port 5010 for
HTTP as specified, while for gRPC it is going to auto-select an avail-
able port.
The log from Dapr is full of information. To confirm your application is
running correctly in the context of the Dapr runtime, you can look for
this part:
You're up and running! Both Dapr and your app logs will appear
here.
PS C:Reposdapr-sampleschapter01> curl
http://localhost:5010/v1.0/invoke/hello-world/method/hello
Hello, World
This exciting response has been returned by Dapr, which passed our
(the client's) initial request to the ASP.NET Web API framework. You
should also see it logged as Console.WriteLine sends its output to
the Dapr window, as follows:
7. From another window, let's verify our Dapr service details: instead of
using the dapr list command, let's open the Dapr dashboard, like this:
In this case, the Dapr dashboard shows only this sample application we are
running on the development machine. In a Kubernetes environment, it would
show all the microservices running, along with the other components.
The Dapr dashboard also displays the configured components in the hosting
environment, as we can see here:
In Figure 1.4, the Dapr dashboard shows us that the local installation of Redis
is configured as state store and pub/sub components, in addition to the de-
ployment of Zipkin.
This ends our introductory section, where we were able to build our first Dapr
sample.
Summary
In this chapter, you have learned about the Dapr project, with its components,
building blocks, and the sidecar approach. All of these concepts will be ex-
plored individually in further depth in the following chapters. You are now able
to set up Dapr on your local development machine and prepare all the neces-
sary tools to make this experience easier.
You have also learned how to create a simple ASP.NET project and how to
configure and check Dapr, and we have had a glimpse of the Dapr dashboard
where we can gain a complete and immediate view of the Dapr environment.
In the next chapter, we will use the newly created environment to learn how to
debug Dapr.