Practical Microservices with Dapr and .NET

Book description

Use the new, enticing, and highly portable event-driven runtime to simplify building resilient and scalable microservices for cloud and edge applications

Key Features

  • Build resilient, stateless, and stateful microservice applications that run on the cloud and edge
  • Solve common distributed systems such as low latency and scaling using any language and framework
  • Use real-time and proactive monitoring tools to support a reliable and highly available system

Book Description

Over the last decade, there has been a huge shift from heavily coded monolithic applications to finer, self-contained microservices. Dapr is a new, open source project by Microsoft that provides proven techniques and best practices for developing modern applications. It offers platform-agnostic features for running your applications on public cloud, on-premises, and even on edge devices.

This book will help you get to grips with microservice architectures and how to manage application complexities with Dapr in no time. You'll understand how Dapr offers ease of implementation while allowing you to work with multiple languages and platforms. You'll also understand how Dapr's runtime, services, building blocks, and software development kits (SDKs) help you to simplify the creation of resilient and portable microservices. Dapr provides an event-driven runtime that supports the essential features you need to build microservices, including service invocation, state management, and publish/subscribe messaging. You'll explore all of those in addition to various other advanced features with this practical guide to learning Dapr.

By the end of this book, you'll be able to write microservices easily using your choice of language or framework by implementing industry best practices to solve problems related to distributed systems.

What you will learn

  • Use Dapr to create services, invoking them directly and via pub/sub
  • Discover best practices for working with microservice architectures
  • Leverage the actor model to orchestrate data and behavior
  • Use Azure Kubernetes Service to deploy a sample application
  • Monitor Dapr applications using Zipkin, Prometheus, and Grafana
  • Scale and load test Dapr applications on Kubernetes

Who this book is for

This book is for developers looking to explore microservices architectures and implement them in Dapr applications using examples on Microsoft .NET Core. Whether you are new to microservices or have knowledge of this architectural approach and want to get hands-on experience in using Dapr, you’ll find this book useful. Familiarity with .NET Core will help you to understand the C# samples and code snippets used in the book.

Table of contents

  1. Practical Microservices with Dapr and .NET
  2. Why subscribe?
  3. Foreword
  4. Contributors
  5. About the author
  6. About the reviewer
  7. Packt is searching for authors like you
  8. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  9. Section 1: Introduction to Dapr
  10. Chapter 1: Introducing Dapr
    1. Technical requirements
    2. An overview of Dapr
      1. What Dapr is not
    3. The architecture of Dapr
    4. Setting up Dapr
      1. Docker
      2. The Dapr CLI
      3. .NET
      4. VS Code
      5. Windows Terminal
      6. Installing self-hosted Dapr
      7. Installing Dapr on Kubernetes
      8. Updating the Dapr version
    5. Building our first Dapr sample
    6. Summary
  11. Chapter 2: Debugging Dapr Solutions
    1. Technical requirements
    2. Configuring Dapr debug in VS Code
      1. Attaching the debugger
      2. Examining the debug configuration
    3. Debugging a Dapr multi-project solution
      1. Creating .NET solutions
      2. Launching the configuration
      3. Tasks
      4. Launching debug sessions individually
      5. Launching compound debug sessions
    4. Using Tye with Dapr
      1. Installing Tye
      2. Using Tye
    5. Summary
  12. Section 2: Building Microservices with Dapr
  13. Chapter 3: Service-to-Service Invocation
    1. Technical requirements
    2. How services work in Dapr
      1. Introducing our sample architecture
      2. Introducing service-to-service invocation
      3. Name resolution
    3. Service invocation with the .NET SDK
      1. Creating a project for the Order service
      2. Configuring Dapr in ASP.NET
      3. Implementing Dapr with an ASP.NET controller
      4. Creating a project for the Reservation service
      5. Preparing the debugging configuration
      6. Implementing Dapr with ASP.NET routing
      7. Recap
    4. HTTP and gRPC for Dapr services
      1. gRPC in ASP.NET
      2. The autonomy of a microservice
      3. Winning latency with gRPC
    5. Summary
  14. Chapter 4: Introducing State Management
    1. Technical requirements
    2. Managing state in Dapr
      1. State, stateless, and stateful
      2. State stores in Dapr
      3. Transactions
      4. Concurrency
      5. Consistency
      6. Interaction with state stores
    3. Stateful services in an e-commerce ordering system
      1. Stateful reservation-service
      2. Handling the Dapr state in ASP.NET controllers
    4. Azure Cosmos DB as a state store
      1. Setting up Azure Cosmos DB
      2. Configuring the state store
      3. Testing the state store
      4. Partitioning with Cosmos DB
      5. Wrapping up
    5. Summary
  15. Chapter 5: Publish and Subscribe
    1. Technical requirements
    2. Using the pub/sub pattern in Dapr
    3. Using Azure Service Bus in Dapr
      1. Subscribing to a topic
      2. Configuring a pub/sub component
      3. Publishing to a topic
      4. Inspecting the messages
    4. Implementing a saga pattern
      1. Publishing messages to Dapr
      2. Subscribing to a Dapr topic
      3. Testing the saga pattern
    5. Summary
  16. Chapter 6: Resource Bindings
    1. Technical requirements
    2. Learning how to use Dapr bindings
      1. Configuring a cron input binding
      2. Testing the cron binding
    3. Using Twilio output bindings in Dapr
      1. Signing up for a Twilio trial
      2. Configuring a Twilio output binding
      3. Signaling via the output binding
      4. Verifying the notification
    4. Ingesting data in C# with the Azure Event Hubs input binding
      1. Creating an Azure Event Hubs binding
      2. Configuring the input binding
      3. Implementing an Azure Event Hubs input binding
      4. Producing events
    5. Summary
  17. Chapter 7: Using Actors
    1. Technical requirements
    2. Using actors in Dapr
      1. Introduction to the actor pattern
      2. Configuring a new state store
      3. Verifying the configuration
    3. Actor lifetime, concurrency, and consistency
      1. Placement service
      2. Concurrency and consistency
      3. Lifetime
    4. Implementing actors in an e-commerce reservation system
      1. Preparing the Actor's projects
      2. Implementing the actor model
      3. Accessing actors from other Dapr applications
      4. Inspecting the actor state
    5. Summary
  18. Section 3: Deploying and Scaling Dapr Solutions
  19. Chapter 8: Deploying to Kubernetes
    1. Technical requirements
    2. Setting up Kubernetes
      1. Creating an Azure resource group
      2. Creating an AKS cluster
      3. Connecting to the AKS cluster
    3. Setting up Dapr on Kubernetes
    4. Deploying a Dapr application to Kubernetes
      1. Building Docker images
      2. Pushing Docker images
      3. Managing secrets in Kubernetes
      4. Deploying applications
    5. Exposing Dapr applications to external clients
    6. Summary
  20. Chapter 9: Tracing Dapr Applications
    1. Technical requirements
    2. Observing applications in Dapr
    3. Tracing with Zipkin
      1. Setting up Zipkin
      2. Configuring tracing with Zipkin
      3. Enabling tracing in Dapr
      4. Investigating with Zipkin
    4. Analyzing metrics with Prometheus and Grafana
      1. Installing Prometheus
      2. Installing Grafana
      3. Importing dashboards
    5. Summary
  21. Chapter 10: Load Testing and Scaling Dapr
    1. Technical requirements
      1. Bash
      2. Python
      3. Locust
      4. Kubernetes configuration
    2. Scaling Dapr on Kubernetes
      1. Replicas
      2. Autoscale
      3. Resource requests and limits
    3. Load testing with Locust
    4. Load testing Dapr
      1. Preparing the data via port-forwarding
      2. Testing Locust locally
      3. Locust on Azure Container Instances
      4. Configuring the Horizontal Pod Autoscaler
    5. Autoscaling with KEDA
    6. Summary
  22. Appendix – Microservices Architecture with Dapr
    1. Discovering microservices
      1. Service
      2. Autonomy
      3. Automated deployment
      4. Bounded context
      5. Loose coupling
      6. Event-driven architecture
      7. Observability
      8. Sustainability
    2. Adopting microservices patterns
    3. Building an e-commerce architecture
      1. Bounded contexts
      2. An example – sales microservices
    4. Building microservices with Dapr
      1. Loosely coupled microservices
      2. Autonomous microservices
      3. Observable microservices
      4. Scalable microservices
      5. Event-driven microservices
      6. Stateless microservices
    5. Summary
  23. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Practical Microservices with Dapr and .NET
  • Author(s): Davide Bedin
  • Release date: December 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781800568372