Dapr Samples

repository·master·Indexed 19 days ago

https://github.com/dapr/samples

A collection of real-world Dapr implementation samples demonstrating building blocks, middleware, and cloud integrations. Featured samples include a Batch File Processing solution using Node.js, Azure Blob Storage, Cosmos DB, and Redis, as well as a proof-of-concept integration between Dapr Bindings, Knative Serving, and Knative Eventing using Kafka.

Tokens
66.5K
Snippets
220
Records
277
Agent score
65%

What's inside dapr-samples

  1. Overview of Dapr & Azure API Management Integration

    master

    This demo demonstrates the integration between Dapr and Azure API Management (APIM) using a self-hosted gateway running on Kubernetes. It illustrates how APIM can act as a gateway to interact with Dapr services through three specific use-cases:

    1. Service Invocation: Calling a specific Dapr service method.
    2. Pub/Sub: Publishing content to a Dapr Pub/Sub topic.
    3. Bindings: Invoking a Dapr binding with request content transformation.

    The demo also covers using APIM tracing to debug the configuration.

  2. Overview of the dapr-distributed-calendar sample

    master

    The dapr-distributed-calendar is a proof-of-concept application demonstrating Dapr's language-agnostic capabilities. It uses a Model-View-Controller-Service (MVCS) architecture to showcase several Dapr building blocks:

    • State Store: Used by the Go service to persist event data (using Redis).
    • Pub/Sub: Used by the Javascript controller to publish messages to a topic.
    • Output Bindings: Used by the Python service to send emails via SendGrid.
    • Service Invocation: Used by the Javascript controller to communicate with the Go service.
  3. Overview of the Batch File Processing Sample

    master

    This end-to-end sample demonstrates how to process batches of related text files (CSV) using microservices and Dapr. It simulates a real-world enterprise scenario where files in a batch (e.g., OrderHeaderDetails.csv, OrderLineItems.csv, and ProductInformation.csv) arrive asynchronously and must be combined before processing.

    Key Dapr capabilities demonstrated:

    • State Management: Used to track the arrival of individual files within a batch.
    • Bindings: Used to interact with Azure Blob Storage and Cosmos DB.
    • Pub/Sub: Used to decouple the file reception from the heavy processing logic and to provide load leveling.
    • End-to-end Tracing: Integrated with Azure App Insights to monitor the flow of requests across microservices.

    Technical Specifications:

    • Dapr runtime version: v0.10
    • Language: JavaScript (Node.js)
    • Environment: Kubernetes (AKS)
  4. Overview of Dapr Bindings + Knative Serving and Eventing Sample

    master

    This sample demonstrates a proof-of-concept integration between Dapr and Knative. It uses Knative Serving (with Kourier) to host React Form and backend applications, Dapr Bindings to push events into Kafka topics, and Knative Eventing to read from those Kafka topics and distribute them to backend applications.

    Technical Specifications:

    • Dapr runtime version: v1.5.0
    • Knative Serving version: v1.0
    • Languages supported: Javascript, Python, Go, C#
    • Environment: Kubernetes > v1.20
  5. Consume Kafka messages without CloudEvents

    master

    This sample demonstrates how to consume messages from Kafka that are not wrapped in the CloudEvents format. This is useful when integrating with external producers (like a Confluent Kafka SDK producer) that publish raw payloads directly to a Kafka topic.

    To enable this behavior in a Dapr subscriber, you must set the isRawPayload metadata property to true in your subscription configuration.

  6. Explore Dapr sample scenarios

    master

    This repository provides samples for a wide range of Dapr building blocks and integration patterns. Key categories include:

    • Core Building Blocks: State management, Pub/Sub, Service Invocation, Bindings, and Workflow.
    • Middleware: OAuth 2.0 (Google and Microsoft/AAD), WebAssembly, and request transformation.
    • Cloud & Infrastructure Integrations: Azure APIM, AWS EKS Pod Identity, Azure AKS Workload Identity, Kubernetes Events, and Argo CD (GitOps).
    • Advanced Patterns: Outbox transactions (with Redis/MySQL), Kafka integration without CloudEvents, and Reactive applications with Drasi.
    • Polyglot & Multi-App: Workflows orchestrating across Go, Java, and Python services.
  7. Build a Dapr Pluggable Component in .NET

    master

    This template project provides a starting point for building Dapr pluggable components (such as State Stores, Pub/Sub, Input Bindings, or Output Bindings) using .NET. It uses gRPC to communicate between the Dapr runtime and your custom component implementation.

    Core Concepts

    • Pluggable Components: Instead of writing a Dapr component in Go, you can write it in .NET by implementing specific gRPC service interfaces defined in Dapr's proto files.
    • Service Implementation: You implement classes that inherit from the generated base classes (e.g., StateStore.StateStoreBase) found in the ./Services/Services.cs file.
    • Registration: Once implemented, the services must be registered in the ASP.NET Core gRPC pipeline in Program.cs using app.MapGrpcService<T>().
  8. Find Dapr samples and quickstarts

    master

    The dapr/samples repository contains complex, multi-component, and end-to-end demonstrations of Dapr capabilities across various languages and scenarios.

    Important Note: If you are a newcomer looking for simple, high-level tutorials to get started quickly, you should use the dapr/quickstarts repository instead. The quickstarts are designed for rapid onboarding, whereas this repository focuses on specific usage patterns and complex distributed application scenarios.

  9. What is Drasi and how does it work with Dapr?

    master

    Drasi is a Data Change Processing platform designed to detect and react to complex data changes across multiple distributed sources using declarative graph queries.

    In a Dapr environment, Drasi provides Zero-Impact Change Detection. Instead of polling databases or calling service APIs (which adds load), Drasi subscribes to database change logs or replication logs (e.g., PostgreSQL logical replication). This allows it to maintain a real-time view of data across multiple microservices without requiring any changes to your existing Dapr services or their state stores.

    Key components include:

    • Sources: Connectors to data sources like PostgreSQL, MySQL, Cosmos Gremlin, and Kubernetes.
    • Continuous Queries: Graph queries that define complex conditions spanning multiple sources.
    • Reactions: Actions triggered when query results change (e.g., SignalR, Azure Event Grid, StorageQueue, or Dapr Pub/Sub).
  10. How Drasi Sources and Continuous Queries Work

    master

    Drasi provides zero-impact change detection by monitoring the PostgreSQL Write-Ahead Log (WAL) rather than querying service APIs.

    1. PostgreSQL WAL Configuration

    To enable Drasi monitoring, databases must be configured with:

    • wal_level=logical
    • max_replication_slots=5
    • max_wal_senders=10

    2. Zero-Impact Monitoring

    Drasi creates a replication slot and subscribes to the WAL stream. It receives real-time notifications for INSERT, UPDATE, and DELETE operations. It maintains its own copy of relevant data for query processing, ensuring it never calls your service APIs or puts additional query load on your production database.

    3. Continuous Queries (Cypher)

    Queries are written in Cypher and run perpetually to detect business conditions.

    Example: product-catalogue query

    MATCH (p:Product)
    OPTIONAL MATCH (r:Review)-[:REVIEWS]->(p)
    WITH p, r
    RETURN p.Id as ProductId, 
           p.Name as Name, 
           p.Price as Price,
           p.Stock as Stock,
           avg(r.Rating) as AverageRating,
           count(r) as ReviewCount
  11. Implement the transactional outbox pattern with Dapr

    master

    The transactional outbox pattern allows an application to save data to a state store and transactionally send pub/sub messages to a listener. This ensures that state updates and message publishing happen atomically.

    In this sample, the order-processor application creates, saves, and deletes orders in a state store, which then triggers notification messages to the order-notification application via a message broker. This sample uses the Dapr .NET SDK.