Post

Dapr - CMDSheet

Commands Cheat Sheet

Dapr - CMDSheet

Introduction

Dapr (Distributed Application Runtime) is an open-source project that simplifies the development of microservices-based applications by providing building blocks such as state management, pub/sub messaging, and service invocation. This blog explores essential Dapr commands that every developer should know.

Installation

dapr --versionTo verify the installation

Initializing Dapr

dapr initInitialize it locally
dapr init --kubernetesTo initialize Dapr in Kubernetes mode
dapr uninstallTo uninstall Dapr
dapr uninstall --kubernetesTo uninstall from Kubernetes

Managing Dapr Applications

  • Running a Dapr Application
1
dapr run --app-id myapp --app-port 5000 --dapr-http-port 3500 -- python app.py
  • --app-id specifies a unique identifier for the app.
  • --app-port defines the port where the app runs.
  • --dapr-http-port assigns a port for Dapr sidecar HTTP communication.

  • Stopping a Running Dapr App
1
dapr stop --app-id myapp

Viewing Dapr Application Status

dapr listTo list running Dapr instances
dapr logs --app-id myappTo check the logs of a running Dapr application

State Management Commands

dapr state get statestore mykeyTo get the state from a state store
dapr state save statestore mykey "myvalue"To save a state
dapr state delete statestore mykeyTo delete a state

Service Invocation

  • Dapr enables service-to-service communication. To invoke a method on another service:
1
curl -X POST http://localhost:3500/v1.0/invoke/myservice/method/mymethod -d '{"data":"hello"}' -H "Content-Type: application/json"
  • Or using the Dapr CLI:
1
dapr invoke --app-id myservice --method mymethod --post --data '{"data":"hello"}'

Pub/Sub Messaging

  • To publish an event:
1
dapr publish --pubsub pubsub --topic mytopic --data '{"message": "hello world"}'
  • To subscribe to a topic, define a subscriber service that listens for events.

Observability & Debugging

dapr components -kChecking Component Status
dapr config -kChecking Configuration

Enabling Tracing

  • Enable tracing with the following:
1
dapr run --app-id myapp --app-port 5000 --enable-metrics --enable-tracing -- python app.py

Dapr Dashboard

dapr dashboardTo start the Dapr dashboard
dapr dashboard -kIf running in Kubernetes

Conclusion

Dapr CLI commands make it easy to develop, debug, and manage microservices. Whether you’re running applications locally or in Kubernetes, these commands will help you interact with Dapr’s features efficiently. Happy coding!

This post is licensed under CC BY 4.0 by the author.