This is a sample project using Dapr with dotnet core 5.0 and redis cache as a store. We have a sample api controller with several actions:
- create client
- get client
- delete client
Check src/DaprSamples/SampleStateStore_Redis
for a sample project.
I am going to use minukube
to run Kubernetes locally.
- Start minikube
minikube start
- Install DAPR locally on your kubernetes cluster
dapr init -k
- Install Redis into your cluster
dapr init -k
and check the status
kubectl get pods
, and you should see 3 nodes in RUNNING status.
To get the redis password, use
export REDIS_PASSWORD=$(kubectl get secret --namespace default redis -o jsonpath="{.data.redis-password}" | base64 --decode)
- Run DAPR API project locally
cd src/DaprSamples/SampleStateStore_Redis
dotnet build
dapr run --app-id controller --app-port 5000 -- dotnet run
, where app-id defines the name of the app for Dapr.
- Add DAPR components to the Kubernetes cluster
5.1. Create redis-statestore.yaml
and add
apiVersion: v1
kind: Secret
metadata:
name: redis
data:
redis-password: <REPLACE WITH BASE64 ENCODED PASSWORD>
---
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
namespace: default
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
secretKeyRef:
name: redis
key: redis-password
Or check redis-statestore.yaml
Here we create a state store component for persistence and restoration, using redis cache.
5.2. Deploy to the Kubernetes cluster
deploy -f redis-pubsub.yaml apply
- Mappings
redis-pubsub.yaml --> kind: Component (1st) --> name: statestore
equalsSampleController.cs --> public const string StoreName = "statestore";
redis-pubsub.yaml --> kind: Component (2nd) --> name: pubsub
equalsSampleController.cs --> [Topic("pubsub", ...)]
- Add client to the store
POST http://localhost:5000/addClient
Content-Type: application/json
{
"Id": "1",
"FirstName" : "Evgeny",
"LastName" : "Grishchenko"
}
To verify the redis (the store ):
redis-cli -h localhost -a <YOUR REDIS PASSWORD>
KEYS *
HGET <OUR KEY NAME>
Yes, the value is stored as HASH type.
In this case the name of the key would be controller||<client id>
- Get client from the store
GET http://localhost:5000/getClient/1
Content-Type: application/json
{
"id": "1",
"firstName": "Evgeny",
"lastName": "Grishchenko"
}
- Delete client from the store
DELETE http://localhost:5000/deleteClient/1
- Evgeny Grishchenko e.grisss@gmail.com