Book Image

Practical Microservices with Dapr and .NET - Second Edition

By : Davide Bedin
Book Image

Practical Microservices with Dapr and .NET - Second Edition

By: Davide Bedin

Overview of this book

This second edition will help you get to grips with microservice architectures and how to manage application complexities with Dapr in no time. You'll understand how Dapr simplifies development while allowing you to work with multiple languages and platforms. Following a C# sample, you'll understand how Dapr's runtime, building blocks, and software development kits (SDKs) help you to simplify the creation of resilient and portable microservices. Dapr provides an event-driven runtime that supports the essential features you need for building microservices, including service invocation, state management, and publish/subscribe messaging. You'll explore all of those in addition to various other advanced features with this practical guide to learning Dapr. With a focus on deploying the Dapr sample application to an Azure Kubernetes Service cluster and to the Azure Container Apps serverless platform, you’ll see how to expose the Dapr application with NGINX, YARP, and Azure API Management. By the end of this book, you'll be able to write microservices easily by implementing industry best practices to solve problems related to distributed systems.
Table of Contents (20 chapters)
1
Part 1: Introduction to Dapr
5
Part 2: Building Microservices with Dapr
11
Part 3: Deploying and Scaling Dapr Solutions

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 we use is Windows, 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, we encourage you to check the Dapr documentation at https://docs.dapr.io/.

Dapr roadmap

The Dapr runtime reached the v1.0 production-ready release in February 2021, as announced in the Dapr blog at https://blog.dapr.io/posts/2021/02/17/announcing-dapr-v1.0/, and five new minor versions have been released during 2021. You can learn more about the Dapr roadmap at https://docs.dapr.io/contributing/roadmap/.

The samples and scripts in this book have been updated and tested with v1.8 of Dapr.

In this book, we will also leverage several services on the Azure cloud platform (https://azure.microsoft.com/en-us/explore/), whether as a platform to execute Dapr applications, for exchanging messages via Azure, or for storing data.

Access to an Azure subscription is required. Each chapter will give you instructions and direct you to documentation for further information.

Next, we will accomplish the following steps:

  • Configuring Docker
  • Installing the Dapr CLI
  • Installing .NET
  • Installing VS Code
  • Installing Windows Terminal
  • Installing Dapr in self-hosted mode
  • Installing Dapr on Kubernetes

Let’s start with Docker.

Docker

Dapr requires Docker to be present locally in your development environment; therefore, make sure you have it installed. If your development machine is Windows, Docker must be running in Linux container mode.

You can find detailed instructions for running Docker at https://docs.docker.com/install/.

Intalling the Dapr CLI

We will immediately start working with Dapr; therefore, you need to install all the necessary tools. The Dapr runtime and its tools can be found at https://github.com/dapr/cli.

On Windows, execute the following command to install the CLI in the c:/dapr directory and add it to the user PATH environment variable so that the tools can be easily found from the command line:

powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"

For more details on the Dapr CLI, please refer to https://docs.dapr.io/getting-started/install-dapr-cli/.

We still need to initialize Dapr on the development machine, which we will do in the Installing Dapr in self-hosted mode section of this chapter.

Installing .NET

To install .NET 6, please refer to https://dotnet.microsoft.com/download for the link to the latest binaries.

.NET 6 is a Long-Term Support (LTS) version of .NET, which gets free support and patches for 3 years. Refer to https://dotnet.microsoft.com/en-us/platform/support/policy for more details on the .NET support policy.

On a development machine, it makes sense to install the full SDK, which includes the runtime. Once the install is complete, open a new command prompt and run the dotnet --info command. You should see the following output:

PS C:\Repos\dapr-samples\chapter01> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.101
 Commit:    ef49f6213a
Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.101\
Host (useful for support):
  Version: 6.0.1
  Commit:  3a25a7f1cc …

This proves that .NET has been recognized and the framework is working fine.

Installing VS Code

VS Code is a great multiplatform source code editor by Microsoft. You can install it for free by following the instructions at https://code.visualstudio.com/docs/setup/windows.

The Dapr extension

Dapr has an extension for VS Code that helps with navigating the Dapr local environment and eases the debugging configuration—we highly recommend it. Please follow the instructions at https://docs.dapr.io/developing-applications/ides/vscode/.

Installing Windows Terminal

We 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 we suggest you adopt it too.

Installing Dapr in self-hosted mode

Dapr can be initialized in two modes: self-hosted (or standalone) and Kubernetes.

As it is intended to be used for a development environment, the self-hosted mode locally installs Redis, the Dapr placement services, and Zipkin. The following command initializes Dapr in your local environment:

dapr init

The Dapr binaries and default components to leverage Redis are by default positioned in the %USERPROFILE%\.dapr\ folder.

In a local development environment, it might happen that the ports Dapr might intend 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, the following is the output you should expect:

PS C:\Repos\practical-dapr\chapter01> dapr init
Making the jump to hyperspace...
Installing runtime version 1.8.4
Downloading binaries and setting up components...
Downloaded binaries and completed components set up.
daprd binary has been installed to C:\Users\dabedin\.dapr\bin.
dapr_placement container is running.
dapr_redis container is running.
dapr_zipkin container is running.
Use `docker ps` to check running containers.
Success! Dapr is up and running. To get started, go here: https://aka.ms/dapr-getting-started

To check your newly initialized Dapr environment, you can use docker ps as follows:

PS C:\Repos\practical-dapr\chapter01> docker ps --format "{{.
Image}} - {{.Ports}} - {{.Names}}"
daprio/dapr:1.8.4 - 0.0.0.0:6050->50005/tcp, :::6050->50005/tcp
- dapr_placement
openzipkin/zipkin - 9410/tcp, 0.0.0.0:9411->9411/tcp, :::9411-
>9411/tcp - dapr_zipkin
redis - 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp - dapr_redis

The output shows the Docker container for Dapr running on our machine.

Installing Dapr on Kubernetes

Dapr is specifically intended to be executed on Kubernetes. From your development machine on which you have the Dapr CLI installed, you can set up Dapr on the Kubernetes cluster currently configured as follows:

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

If you intend to define a Continuous Integration/Continuous Deployment (CI/CD) pipeline that takes care of the Dapr installation on the Kubernetes cluster too, this can also work, although it is out of scope for the present setup.

To verify the installation was successfully completed, execute the following command:

kubectl get pods --namespace dapr-system

The command should display the pods in the dapr-system namespace.

Updating Dapr version

On a development Windows machine on which a previous version of Dapr was already present, the CLI can be updated by simply re-installing with the command we saw in a previous section.

As described in https://docs.dapr.io/operations/hosting/self-hosted/self-hosted-upgrade/, on a Windows development machine on which a previous version of Dapr was already present, you must uninstall Dapr first as follows:

PS C:\Repos\practical-dapr\chapter01> dapr uninstall --all

With the CLI updated and Dapr uninstalled, we can repeat the Dapr installation as follows:

PS C:\Repos\practical-dapr\chapter01> dapr init

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:

PS C:\Repos\practical-dapr\chapter01> dapr --version
CLI version:  1.8.1 
Runtime version: 1.8.4

Our Dapr test environment is up and running. We are now ready to try it with our first sample.