Dapr is a runtime for every platform and every language. The focus of this book is on C# in .NET, used withVisual Studio Code (VS Code). The code snippets in the book can be appreciatedby developers from anybackground, 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 particularaction on Linux or a macOS developmentmachine, 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 learnmore about the Dapr roadmap athttps://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 directyou 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 tobe 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 canfind detailed instructions for running Docker at https://docs.docker.com/install/.
Intalling the Dapr CLI
We will immediatelystart working with Dapr; therefore, you need to installall the necessary tools. The Dapr runtime and its tools can be found athttps://github.com/dapr/cli.
On Windows, execute the following command to install the CLI in thec:/dapr
directory and add it to the userPATH
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 theInstalling Dapr in self-hosted mode section of this chapter.
Installing .NET
To install .NET 6, pleaserefer to https://dotnet.microsoft.com/download forthe link to the latest binaries.
.NET 6 isaLong-Term Support (LTS) version of .NET, which getsfree support and patches for3 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 thedotnet --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: ef49f6213aRuntime 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 hasbeen recognized and the framework is workingfine.
Installing VS Code
VS Code is agreat multiplatformsource code editor by Microsoft. You caninstall 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 environmentand eases the debuggingconfiguration—we highlyrecommend it. Please follow the instructions at https://docs.dapr.io/developing-applications/ides/vscode/.
Installing Windows Terminal
We really lovethe new Windows Terminal (https://aka.ms/terminal) for its ease of use and configurability. In thefollowing chapters, we will often have to runmultiple 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 beinitialized intwo modes:self-hosted (or standalone) andKubernetes.
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 theinit
command, the following is the output you should expect:
PS C:\Repos\practical-dapr\chapter01> dapr initMaking the jump to hyperspace...Installing runtime version 1.8.4Downloading 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 usedocker 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_placementopenzipkin/zipkin - 9410/tcp, 0.0.0.0:9411->9411/tcp, :::9411->9411/tcp - dapr_zipkinredis - 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp - dapr_redis
The outputshows the Dockercontainer for Dapr running on our machine.
Installing Dapr on Kubernetes
Dapr is specificallyintended to be executed on Kubernetes. From your development machine on which youhave 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 aContinuous Integration/Continuous Deployment (CI/CD) pipeline that takes care of the Dapr installation on the Kubernetes cluster too, this canalso work, although it is out of scope for the present setup.
To verify theinstallation was successfully completed, execute thefollowing command:
kubectl get pods --namespace dapr-system
The command should display the pods in thedapr-system
namespace.
Updating Dapr version
On a developmentWindows 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 executedapr 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 --versionCLI version: 1.8.1 Runtime version: 1.8.4
Our Dapr testenvironment is up and running. We are now ready to try itwith our first sample.