Skip to content

a sample project using Dapr with dotnet core 5.0 and redis cache as a store.

Notifications You must be signed in to change notification settings

egrish/sample-dotnet-dapr-redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DAPR C# DEMO

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.

TOOLS

INSTALL DAPR ON KUBERNETES

I am going to use minukube to run Kubernetes locally.

  1. Start minikube
minikube start
  1. Install DAPR locally on your kubernetes cluster
dapr init -k
  1. 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)
  1. 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.

  1. 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
  1. Mappings
  • redis-pubsub.yaml --> kind: Component (1st) --> name: statestore equals SampleController.cs --> public const string StoreName = "statestore";
  • redis-pubsub.yaml --> kind: Component (2nd) --> name: pubsub equals SampleController.cs --> [Topic("pubsub", ...)]
  1. 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>

  1. Get client from the store
GET http://localhost:5000/getClient/1
Content-Type: application/json

{
    "id": "1",
    "firstName": "Evgeny",
    "lastName": "Grishchenko"
}
  1. Delete client from the store
DELETE http://localhost:5000/deleteClient/1

Who do I talk to?

About

a sample project using Dapr with dotnet core 5.0 and redis cache as a store.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages