Dapr (Distributed Application Runtime) is an open-source, portable, event-driven runtime designed to simplify the development of resilient, microservice applications across cloud and edge environments. It offers a set of building blocks, or APIs, that abstract away common complexities in distributed systems, empowering developers to focus on their application logic rather than infrastructure concerns.
1. **Service Invocation (Python):**
```python import requests
response = requests.post(
"http://localhost:3500/v1.0/invoke/my-service/method/my-method", json={"data": "Hello, world!"}) ```
2. **State Management (Node.js):**
```javascript const dapr = require(“@dapr/dapr”);
const client = new dapr.DaprClient(process.env.DAPR_HTTP_PORT ]] | dapr | 3500, process.env.DAPR_GRPC_PORT | dapr | [[ 50001);
await client.state.save(“my-state-store”, [
{ key: "my-key", value: "my-value", },]); ```
3. **Publish/Subscribe (Go):**
```go package main
import (
"context" "log"
dapr "github.com/dapr/go-sdk/client"
)
func main() {
// Create a Dapr client client, err := dapr.NewClient() if err != nil { log.Fatalf("failed to create Dapr client: %v", err) } defer client.Close()
// Publish an event if err := client.PublishEvent(context.Background(), "my-pub-sub", "my-topic", []byte("hello")); err != nil { log.Fatalf("failed to publish event: %v", err) }
} ```
These examples demonstrate how to use Dapr building blocks for service invocation, state management, and publish/subscribe messaging in different programming languages.