Demonstrates a distributed calculator application that uses Dapr services to power a React web app. Highlights polyglot (multi-language) programming, service invocation and state management.
Demonstrates the use of Dapr Secrets API to access secret stores.
1 - Define a component
Create a component definition file to interact with the secrets building block
When building an app, you’d most likely create your own component file definitions, depending on the building block and specific component that you’d like to use.
In this tutorial, you will create a component definition file to interact with the secrets building block API:
Create a local JSON secret store.
Register the secret store with Dapr using a component definition file.
Obtain the secret using the Dapr HTTP API.
Step 1: Create a JSON secret store
Create a new directory named my-components to hold the new secret and component file:
mkdir my-components
Navigate into this directory.
cd my-components
Dapr supports many types of secret stores, but for this tutorial, create a local JSON file named mysecrets.json with the following secret:
{"my-secret":"I'm Batman"}
Step 2: Create a secret store Dapr component
Create a new file localSecretStore.yaml with the following contents:
type: secretstores.local.file tells Dapr to use the local file component as a secret store.
The metadata fields provide component-specific information needed to work with this component. In this case, the secret store JSON path is relative to where you call dapr run.
Step 3: Run the Dapr sidecar
Launch a Dapr sidecar that will listen on port 3500 for a blank application named myapp:
PowerShell environment:
dapr run --app-id myapp --dapr-http-port 3500 --resources-path ../
non-PowerShell environment:
dapr run --app-id myapp --dapr-http-port 3500 --resources-path .
Tip
If an error message occurs, stating the app-id is already in use, you may need to stop any currently running Dapr sidecars. Stop the sidecar before running the next dapr run command by either:
For this tutorial, we describe how to get up and running with Redis.
Step 1: Create a Redis store
Dapr can use any Redis instance, either:
Containerized on your local dev machine, or
A managed cloud service.
If you already have a Redis store, move on to the configuration section.
Redis is automatically installed in self-hosted environments by the Dapr CLI as part of the initialization process. You are all set! Skip ahead to the next steps.
You can use Helm to create a Redis instance in our Kubernetes cluster. Before beginning, install Helm v3.
For Dapr’s Pub/sub functionality, you’ll need at least Redis version 5. For state store, you can use a lower version.
Note that adding --set architecture=standalone to the install command creates a single replica Redis setup, which can save memory and resources if you are working in a local environment.
Run kubectl get pods to see the Redis containers now running in your cluster:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master-0 1/1 Running 0 69s
redis-replicas-0 1/1 Running 0 69s
redis-replicas-1 1/1 Running 0 22s
For Kubernetes:
The hostname is redis-master.default.svc.cluster.local:6379
The secret, redis, is created automatically.
Verify you have an Azure subscription.
Open and log into the Azure portal to start the Azure Redis Cache creation flow.
Fill out the necessary information.
Dapr Pub/sub uses Redis streams introduced by Redis 5.0. To use Azure Redis Cache for Pub/sub, set the version to (PREVIEW) 6.
Click Create to kickoff deployment of your Redis instance.
Make note of the Redis instance hostname from the Overview page in Azure portal for later.
It should look like xxxxxx.redis.cache.windows.net:6380.
Once your instance is created, grab your access key:
Navigate to Access Keys under Settings.
Create a Kubernetes secret to store your Redis password:
Dapr defines resources to use for building block functionality with components. These steps go through how to connect the resources you created above to Dapr for state and pub/sub.
Locate your component files
In self-hosted mode, component files are automatically created under:
Windows: %USERPROFILE%\.dapr\components\
Linux/MacOS: $HOME/.dapr/components
Since Kubernetes files are applied with kubectl, they can be created in any directory.
Create State store component
Create a file named redis-state.yaml, and paste the following:
apiVersion:dapr.io/v1alpha1kind:Componentmetadata:name:statestorenamespace:defaultspec:type:state.redisversion:v1metadata:- name:redisHostvalue:localhost:6379- name:redisPasswordsecretKeyRef:name:rediskey:redis-password# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)# - name: enableTLS# value: true
apiVersion:dapr.io/v1alpha1kind:Componentmetadata:name:statestorenamespace:defaultspec:type:state.redisversion:v1metadata:- name:redisHostvalue:<REPLACE WITH HOSTNAME FROM ABOVE - for Redis on Kubernetes it is redis-master.default.svc.cluster.local:6379>- name:redisPasswordsecretKeyRef:name:rediskey:redis-password# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)# - name: enableTLS# value: true
Note the above code example uses the Kubernetes secret you created earlier when setting up a cluster.
Other stores
If using a state store other than Redis, refer to the supported state stores for information on options to set.
Create Pub/sub message broker component
Create a file called redis-pubsub.yaml, and paste the following:
apiVersion:dapr.io/v1alpha1kind:Componentmetadata:name:pubsubnamespace:defaultspec:type:pubsub.redisversion:v1metadata:- name:redisHostvalue:localhost:6379- name:redisPasswordsecretKeyRef:name:rediskey:redis-password# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)# - name: enableTLS# value: true
apiVersion:dapr.io/v1alpha1kind:Componentmetadata:name:pubsubnamespace:defaultspec:type:pubsub.redisversion:v1metadata:- name:redisHostvalue:<REPLACE WITH HOSTNAME FROM ABOVE - for Redis on Kubernetes it is redis-master.default.svc.cluster.local:6379>- name:redisPasswordsecretKeyRef:name:rediskey:redis-password# uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)# - name: enableTLS# value: true
Note the above code example uses the Kubernetes secret you created earlier when setting up a cluster.
Other stores
If using a pub/sub message broker other than Redis, refer to the supported pub/sub message brokers for information on options to set.
Hard coded passwords (not recommended)
For development purposes only, you can skip creating Kubernetes secrets and place passwords directly into the Dapr component file:
apiVersion:dapr.io/v1alpha1kind:Componentmetadata:name:statestorenamespace:defaultspec:type:state.redisversion:v1metadata:- name:redisHostvalue:<HOST>- name:redisPasswordvalue:<PASSWORD># uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)# - name: enableTLS# value: true
apiVersion:dapr.io/v1alpha1kind:Componentmetadata:name:pubsubnamespace:defaultspec:type:pubsub.redisversion:v1metadata:- name:redisHostvalue:<HOST>- name:redisPasswordvalue:<PASSWORD># uncomment below for connecting to redis cache instances over TLS (ex - Azure Redis Cache)# - name: enableTLS# value: true
Step 3: Apply the configuration
When you run dapr init, Dapr creates a default redis pubsub.yaml on your local machine. Verify by opening your components directory:
On Windows, under %UserProfile%\.dapr\components\pubsub.yaml
On Linux/MacOS, under ~/.dapr/components/pubsub.yaml
For new component files:
Create a new components directory in your app folder containing the YAML files.
Provide the path to the dapr run command with the flag --resources-path
If you initialized Dapr in slim mode (without Docker), you need to manually create the default directory, or always specify a components directory using --resources-path.
Run kubectl apply -f <FILENAME> for both state and pubsub files: