Use the Dapr API
You will now run the sidecar and call the API directly (simulating what an application would do).
One of the most useful Dapr CLI commands is dapr run
. This command launches an application together with a sidecar. For the purpose of this tutorial you’ll run the sidecar without an application.
Run the following command to launch a Dapr sidecar that will listen on port 3500 for a blank application named myapp:
With this command, no custom component folder was defined, so Dapr uses the default component definitions that were created during the init flow (these can be found under $HOME/.dapr/components
on Linux or MacOS and under %USERPROFILE%\.dapr\components
on Windows). These tell Dapr to use the local Redis Docker container as a state store and message broker.
[
{
"key": "name",
]
Notice, the object contained in the state has a key
assigned with the value name
. You will use the key in the next step.
Run the command shown below to store the new state.
curl -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "Bruce Wayne"}]' http://localhost:3500/v1.0/state/statestore
Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '[{ "key": "name", "value": "Bruce Wayne"}]' -Uri 'http://localhost:3500/v1.0/state/statestore'
Now get the object you just stored in the state by using the state management API with the key name
:
With the same Dapr instance running from above run:
Invoke-RestMethod -Uri 'http://localhost:3500/v1.0/state/statestore/name'
You can look in the Redis container and verify Dapr is using it as a state store. Run the following to use the Redis CLI:
List the redis keys to see how Dapr created a key value pair (with the app-id you provided to dapr run
as a prefix to the key):
keys *
View the state value by running:
hgetall "myapp||name"
1) "data"
2) "\"Bruce Wayne\""
3) "version"
Exit the redis-cli with: