目录:
面向.NET开发人员的Dapr——前言
面向.NET开发人员的Dapr——分布式世界
Dapr at 20,000 feet
俯瞰Dapr
In chapter 1, we discussed the appeal of distributed microservice applications. But, we also pointed out that they dramatically increase architectural and operational complexity. With that in mind, the question becomes, how can you "have your cake" and "eat it too?". That is, how can you take advantage of the agility of distributed architecture, and minimize its complexity?
第1章介绍了分布式微服务应用程序的吸引力。但我们也指出,它们大幅增加了体系结构和运营的复杂性。考虑到这一点,问题变成了,您如何 "拥有蛋糕"并“怎么食用”。也就是说,如何充分利用分布式体系结构的灵活性,并将其复杂性降到最低?
Dapr, or Distributed Application Runtime, is a new way to build modern distributed applications.
Dapr 或分布式应用程序运行时 是一种用于构建现代分布式应用程序的新途径。
What started as a prototype has evolved into a highly successful open-source project. Its sponsor, Microsoft, has closely partnered with customers and the open-source community to design and build Dapr. The Dapr project brings together developers from all backgrounds to solve some of the toughest challenges of developing distributed applications.
最初作为原型的项目已经发展成为一个非常成功的开源项目。 赞助商Microsoft与客户和开源社区紧密合作,设计和构建 Dapr。Dapr 项目汇集了来自各个背景的开发人员,以解决开发分布式应用程序的一些棘手挑战。
This book looks at Dapr from the viewpoint of a .NET developer. In this chapter, you'll build a conceptual understanding of Dapr and how it works. Later on, we present practical, hands-on instruction on how you can use Dapr in your applications.
本书从 .NET 开发人员的角度探讨了 Dapr。在本章中,您将建立对 Dapr 及其工作原理的概念理解。稍后,我们将提供有关如何在应用程序中使用 Dapr 的实践指导。
Imagine flying in a jet at 20,000 feet. You look out the window and see the landscape below from a wide perspective. Let's do the same for Dapr. Visualize yourself flying over Dapr at 20,000 feet. What would you see?
想象一下在20000英尺的高度上飞行。你向窗外望去,以宽广的视眼看下面的风景。让我们俯瞰Dapr。想象自己在20,000英尺高空飞越Dapr。你会看到什么?
Dapr and the problem it solves
Dapr 以及它解决的问题
Dapr addresses a large challenge inherent in modern distributed applications: Complexity.
Dapr 解决了现代分布式应用程序固有的巨大挑战: 复杂性。
Through an architecture of pluggable components, Dapr greatly simplifies the plumbing behind distributed applications. It provides a dynamic glue that binds your application with infrastructure capabilities from the Dapr runtime.
通过可插入组件的体系结构,Dapr 大大简化了分布式应用程序背后的管道。它提供了一 个动态胶水,用于将应用程序与 Dapr 运行时中的基础设施功能绑定在一起。
Consider a requirement to make one of your services stateful? What would be your design. You could write custom code that targets a state store such as Redis Cache. However, Dapr provides state management capabilities out-of-the-box. Your service invokes the Dapr state management building block that dynamically binds to a state store component via a Dapr component configuration yaml file. Dapr ships with several pre-built state store components, including Redis. With this model, your service delegates state management to the Dapr runtime. Your service has no SDK, library, or direct reference to the underlying component. You can even change state stores, say, from Redis to MySQL or Cassandra, with no code changes.
怎样让一个服务拥有状态?你将怎么设计。可以编写面向状态存储(如 Redis 缓存)的自定义代码。但是,Dapr 提供开箱即用的状态管理功能。服务调用 Dapr 状态管理构建块,该构建块通过Dapr组件配置文件(配置文件是一个yaml文件)动态绑定到状态存储组件。Dapr 附带了多个预构建的状态存储组件,包括 Redis。借助此模式,服务将状态管理委托给 Dapr 运行时。服务没有 SDK、类库或对基础组件的直接引用。甚至可以将状态存储从 Redis 替换为 MySQL 或 Cassandra,无需更改代码。
Figure 2-1 shows Dapr from 20,000 feet.
图 2-1 俯瞰Dapr。
Figure 2-1. Dapr at 20,000 feet.
图 2-1 俯瞰Dapr。
In the top row of the figure, note how Dapr provides language-specific SDKs for popular development platforms. Dapr v1.0 includes support for Go, Node.js, Python, .NET, Java, and JavaScript. This book focuses on the Dapr .NET SDK, which also provides direct support for ASP.NET Core integration.
在图的首行中,请注意 Dapr 如何为常用开发平台提供特定于语言的 SDK。Dapr v1.0 包括对 Go、Node.js、Python、.NET、Java 和 JavaScript 的支持。本书重点介绍 Dapr .NET SDK,它还直接支持与ASP.NET Core 集成。
While language-specific SDKs enhance the developer experience, Dapr is platform agnostic. Under the hood, Dapr's programming model exposes capabilities through standard HTTP/gRPC communication protocols. Any programming platform can call Dapr via its native HTTP and gRPC APIs.
虽然特定于语言的 SDK 增强了开发人员体验,但 Dapr 与平台无关。在底层,Dapr 的编程模型通过标准 HTTP/gRPC 通信协议公开功能。任何编程平台都可以通过其本机 HTTP 和 gRPC API 调用 Dapr。
The blue boxes across the center of the figure represent the Dapr building blocks. Each exposes a distributed application capability that your application can consume.
图中间的蓝色框表示 Dapr 构建块。每个构建块都公开一个分布式应用程序功能,以供你的应用使用。
The bottom row highlights the portability of Dapr and the diverse environments across which it can run.
底行重点介绍了 Dapr 的可移植性,以及Dapr可在其上运行的不同环境。
Dapr architecture
Dapr 体系结构
At this point, the jet turns around and flies back over Dapr, descending in altitude, giving you a closer look at how Dapr works.
此时,飞机转身飞回Dapr,下降高度,使您更深入地了解 Dapr 的工作原理。
Building blocks
构建块
From the new perspective, you see a more detailed view of the Dapr building blocks.
从新的视角,您可以看到 Dapr 构建块的更详细视图。
A building block encapsulates a distributed infrastructure capability. You can access the functionality through the HTTP or gRPC APIs. Figure 2-2 shows the available blocks for Dapr v 1.0.
构建块封装分布式基础设施功能。可以通过 HTTP 或 gRPC Api 访问该功能。图2-2 显示了Dapr v1.0的可用构建块。
Figure 2-2. Dapr building blocks.
图 2-2。Dapr 构建块。
The following table describes the infrastructure services provided by each block.
下表描述了每个构建块提供的基础设施服务。
Building block | Description |
---|---|
State management | Support contextual information for long running stateful services. |
Service invocation | Invoke direct, secure service-to-service calls using platform agnostic protocols and well-known endpoints. |
Publish and subscribe | Implement secure, scalable pub/sub messaging between services. |
Bindings | Trigger code from events raised by external resources with bi-directional communication. |
Observability | Monitor and measure message calls across networked services. |
Secrets | Securely access external secret stores. |
Actors | Encapsulate logic and data in reusable actor objects. |
构建基块 | 说明 |
---|---|
状态管理 | 支持长时间运行有状态服务的上下文信息。 |
服务调用 | 使用平台无关协议和众所周知的终结点,进行直接安全的服务间调用。 |
发布和订阅 | 在服务之间实现安全的可伸缩的发布/订阅消息传送。 |
绑定 | 通过双向通信,外部资源触发事件时执行相关代码(或从服务内部反方向通知外部资源) |
可观察性 | 监视和度量跨网络服务的消息调用。 |
机密 | 安全访问外部机密存储。 |
执行组件(参与者) | 在可重用的执行组件(参与者)对象中封装逻辑和数据。 |
Building blocks abstract the implementation of distributed application capabilities from your services. Figure 2-3 shows this interaction.
构建块从服务中抽象化分布式应用程序功能的实现。图 2-3 显示了这种交互。
Figure 2-3. Dapr building block integration.
图 2-3。Dapr 构建块集成。
Building blocks invoke Dapr components that provide the concrete implementation for each resource. The code for your service is only aware of the building block. It takes no dependencies on external SDKs or libraries - Dapr handles the plumbing for you. Each building block is independent. You can use one, some, or all of them in your application. As a value-add, Dapr building blocks bake in industry best practices including comprehensive observability.
构建块调用 Dapr 组件,这些组件提供每个资源的具体实现。服务的代码仅知道构建块。它不需要依赖于外部 SDK 或库 - Dapr 会处理管道。每个构建块都是独立的。可以在应用程序中使用其中一个、一部分或全部构建块。作为一种附加值,Dapr 构建块产生于行业最佳实践中(包括全面的可观测性)。
We provide detailed explanation and code samples for each Dapr building block in the upcoming chapters. At this point, the jet descends even more. From the new perspective, you now have a closer look at the Dapr components layer.
在即将发布的章节中,我们将提供每个 Dapr 构建块的详细说明和代码示例。此时,飞机下降了更多。从新视角,现在更深入地了解了 Dapr 组件层。
Components
组件
While building blocks expose an API to invoke distributed application capabilities, Dapr components provide the concrete implementation to make it happen.
虽然构建块公开 API 以调用分布式应用程序功能,但 Dapr 组件提供了具体实现来支持。
Consider, the Dapr state store component. It provides a uniform way to manage state for CRUD operations. Without any change to your service code, you could switch between any of the following Dapr state components:
请考虑 Dapr 状态存储 组件。它提供了一种统一的方式来管理 CRUD 操作的状态。无需更改服务代码,即可在下列任何 Dapr 状态组件之间切换:
AWS DynamoDB
Aerospike
Azure Blob Storage
Azure CosmosDB
Azure Table Storage
Cassandra
Cloud Firestore (Datastore mode)
CloudState
Couchbase
Etcd
HashiCorp Consul
Hazelcast
Memcached
MongoDB
PostgreSQL
Redis
RethinkDB
SQL Server
Zookeeper
Each component provides the necessary implementation through a common state management interface:
每个组件都通过通用状态管理接口提供必要的实现:
type Store interface {Init(metadata Metadata) errorDelete(req *DeleteRequest) errorBulkDelete(req []DeleteRequest) errorGet(req *GetRequest) (*GetResponse, error)Set(req *SetRequest) errorBulkSet(req []SetRequest) error
}
Tip
The Dapr runtime as well as all of the Dapr components have been written in the Golang, or Go, language. Go is a popular language across the open source community and attests to cross-platform commitment of Dapr.
提示
Dapr 运行时以及所有 Dapr 组件都使用 Go语言编写。Go是一种在开源社区中流行的语言,并证实 Dapr 的跨平台承诺。
Perhaps you start with Azure Redis Cache as your state store. You specify it with the following configuration:
或许你将 Azure Redis 缓存用作状态存储作为开始。使用以下配置指定它:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:name: statestorenamespace: default
spec:type: state.redisversion: v1metadata:- name: redisHostvalue: <HOST>- name: redisPasswordvalue: <PASSWORD>- name: enableTLSvalue: <bool> # Optional. Allowed: true, false.- name: failovervalue: <bool> # Optional. Allowed: true, false.
In the spec p, you configure Dapr to use the Redis Cache for state management. The p also contains component-specific metadata. In this case, you can use it to configure additional Redis settings.
在spec 节中,将 Dapr 配置为使用 Redis 缓存进行状态管理。spec节还包含组件特定的元数据。在这种情况下,可以使用它来配置其他 Redis 设置。
At a later time, the application is ready to go to production. For the production environment, you may want to change your state management to Azure Table Storage. Azure Table Storage provides state management capabilities that are affordable and highly durable.
稍后,应用程序已准备就绪,可以开始生产。对于生产环境,可能需要将状态管理替换为Azure Table Storage。Azure Table Storage提供经济实惠且经久耐用的状态管理功能。
At the time of this writing, the following component types are provided by Dapr:
撰写本文时,Dapr 提供以下组件类型:
Component | Description |
---|---|
Service discovery | Used by the service invocation building block to integrate with the hosting environment to provide service-to-service discovery. |
State | Provides a uniform interface to interact with a wide variety of state store implementations. |
Pub/sub | Provides a uniform interface to interact with a wide variety of message bus implementations. |
Bindings | Provides a uniform interface to trigger application events from external systems and invoke external systems with optional data payloads. |
Middleware | Allows custom middleware to plug into the request processing pipeline and invoke additional actions on a request or response. |
Secret stores | Provides a uniform interface to interact with external secret stores, including cloud, edge, commercial, open-source services. |
组件 | 说明 |
---|---|
服务发现 | 由服务调用构建块使用,用于与宿主环境集成以提供服务到服务发现。 |
状态 | 提供统一的接口以与各种状态存储实现交互。 |
发布/订阅 | 提供一个统一接口,用于与各种消息总线实现进行交互。 |
绑定 | 提供一个统一接口,用于从外部系统触发应用程序事件,并调用外部系统(随附可选数据负载)。 |
中间件 | 允许自定义中间件插入请求处理管道,并针对请求或响应调用其他操作。 |
机密存储 | 提供一个统一接口,用于与外部机密存储(包括云、边缘、商业、开源服务)交互。 |
As the jet completes its fly over of Dapr, you look back once more and can see how it connects together.
当飞机完成在 Dapr 的飞行时,可以再次回顾一下,并查看它如何与我们实现的服务连接在一起。
Sidecar architecture
Sidecar 体系结构
Dapr exposes its building blocks and components through a sidecar architecture. A sidecar enables Dapr to run in a separate memory process or separate container alongside your service. Sidecars provide isolation and encapsulation as they aren't part of the service, but connected to it. This separation enables each to have its own runtime environment and be built upon different programming platforms. Figure 2-4 shows a sidecar pattern.
Dapr 通过边车架构公开其 构建块和组件。边车使Dapr能够在单独的进程或单独容器(独立于我们实现的服务)中运行。边车提供隔离和封装,它们不是服务的一部分,但与服务连接。这种分离允许每部分都可以拥有自己的运行时环境,并基于不同的编程平台进行构建。图 2-4 显示了边车模式。
Figure 2-4. Sidecar architecture.
图 2-4。边车架构。
This pattern is named Sidecar because it resembles a sidecar attached to a motorcycle. In the previous figure, note how the Dapr sidecar is attached to your service to provide distributed application capabilities.
此模式之所以称作“边车“(Sidecar),是因为它类似于三轮摩托车上的挎斗。在上图中,注意 Dapr 边车如何附加到服务以提供分布式应用功能。
Hosting environments
托管环境
Dapr has cross-platform support and can run in many different environments. These environments include Kubernetes, a group of VMs, or edge environments such as Azure IoT Edge.
Dapr 具有跨平台支持,可在许多不同的环境中运行。这些环境包括 Kubernetes、一组 VM 或边缘环境(如 Azure IoT Edge)。
For local development, the easiest way to get started is with self-hosted mode. In self-hosted mode, the microservices and Dapr sidecars run in separate local processes without a container orchestrator such as Kubernetes. For more information, see download and install the Dapr CLI.
对于本地开发,最简单的入门方法是使用 自承载模式。在自承载模式下,微服务和 Dapr 边车在的单独本地进程中运行(没有容器编排工具(如 Kubernetes))。有关详细信息,请参阅 下载并安装 DAPR CLI。
Figure 2-5 shows an application and Dapr hosted in two separate memory processes communicating via HTTP or gRPC.
图2-5 显示了一个应用程序和 Dapr,它托管在两个独立的内存进程中,通过 HTTP 或 gRPC 进行通信。
Figure 2-5. Self-hosted Dapr sidecar.
图 2-5。自承载的 Dapr 边车。
By default, Dapr installs Docker containers for Redis and Zipkin to provide default state management and observability. If you don't want to install Docker on your local machine, you can even run Dapr in self-hosted mode without any Docker containers. However, you must install default components such as Redis for state management and pub/sub manually.
默认情况下,Dapr 安装用于 Redis 和 Zipkin 的 Docker 容器,提供默认状态管理和可观察性。如果不想在本地计算机上安装 Docker,甚至可以 在没有任何 Docker 容器的自承载模式下运行 Dapr。但是,必须手动安装默认组件如用于状态管理和发布/订阅的Redis。
Dapr also runs in containerized environments, such as Kubernetes. Figure 2-6 shows Dapr running in a separate side-car container along with the application container in the same Kubernetes pod.
Dapr 也可以在 容器化环境(如 Kubernetes)中运行。图2-6 显示了在单独的边车容器中运行的 Dapr,以及在同一 Kubernetes pod 中的应用程序容器。
Figure 2-6. Kubernetes-hosted Dapr sidecar.
图 2-6。Kubernetes 托管的 Dapr 边车。
Dapr performance considerations
Dapr 性能注意事项
As you've seen, Dapr exposes a sidecar architecture to decouple your application from distributed application capabilities. Invoking a Dapr operation requires at least one out-of-process network call. Figure 2-7 presents an example of a Dapr traffic pattern.
如您所见,Dapr 公开了边车体系结构,以便将应用程序与分布式应用能力分离。调用 Dapr 操作需要至少一次进程外的网络调用。图2-7 显示了 Dapr 通信模式的示例。
Figure 2-7. Dapr traffic patterns.
图 2-7。Dapr 通信模式。
Looking at the previous figure, one might question the latency and overhead incurred for each call.
查看上图,每次调用的延迟和开销可能是个问题。
The Dapr team has invested heavily in performance. A tremendous amount of engineering effort has gone into making Dapr efficient. Calls between Dapr sidecars are always made with gRPC, which delivers high performance and small binary payloads. In most cases, the additional overhead should be sub-millisecond.
Dapr 团队在性能方面投入了大量精力。进行大量工程工作使 Dapr 高效。Dapr 边车之间的调用始终使用 gRPC 进行,可提供高性能和小的二进制负载。在大多数情况下,额外的开销应为亚毫秒级。
To increase performance, developers can call the Dapr building blocks with gRPC.
若要提高性能,开发人员可以使用 gRPC 调用 Dapr 构建块。
gRPC is a modern, high-performance framework that evolves the age-old remote procedure call (RPC) protocol. gRPC uses HTTP/2 for its transport protocol, which provides significant performance enhancements over HTTP RESTFul service, including:
Multiplexing support for sending multiple parallel requests over the same connection - HTTP 1.1 limits processing to one request/response message at a time.
Bidirectional full-duplex communication for sending both client requests and server responses simultaneously.
Built-in streaming enabling requests and responses to asynchronously stream large data sets.
gRPC 是一种现代的高性能框架,它改进了 RPC (远程过程调用)协议 。gRPC 使用 HTTP/2 作为传输协议,该协议比HTTP RESTFul 服务提供显著的性能增强,包括:
对同一连接发送多个并行请求的多路复用支持 - HTTP 1.1 将处理限制为一次处理一个请求/响应消息。
双向全双工通信,用于同时发送客户端请求和服务器响应。
内置流式处理,支持对大型数据集进行异步流式处理的请求和响应。
To learn more, check out the gRPC overview from the Architecting Cloud-Native .NET Apps for Azure eBook.
若要了解有关详细信息,请查看Architecting Cloud-Native .NET Apps for Azure电子书中的gRPC概述。
Dapr and service meshes
Dapr 和服务网格
Service mesh is another rapidly evolving technology for distributed applications.
服务网格是分布式应用程序的另一种快速发展的技术。
A service mesh is a configurable infrastructure layer with built-in capabilities to handle service-to-service communication, resiliency, load balancing, and telemetry capture. It moves the responsibility for these concerns out of the services and into the service mesh layer. Like Dapr, a service mesh also follows a sidecar architecture.
服务网格是一个可配置的基础设施层,具有处理服务到服务通信、复原能力、负载均衡和遥测捕获的内置功能。它将这些问题的职责从服务移入服务网格层。与 Dapr 一样,服务网格也遵循边车体系结构。
Figure 2-8 shows an application that implements service mesh technology.
图 2-8 显示了实施服务网格技术的应用程序。
Figure 2-8. Service mesh with a side car.
图 2-8。 带有边车的服务网格。
The previous figure shows how messages are intercepted by a sidecar proxy that runs alongside each service. Each proxy can be configured with traffic rules specific to the service. It understands messages and can route them across your services and the outside world.
上图显示了通过与每个服务一起运行的边车代理来截获消息的方式。每个代理都可以配置特定于该服务的通信规则。它了解消息,并可以将消息路由到您的服务和外部世界。
So the question becomes, "Is Dapr a service mesh?".
那么问题来了, " Dapr 是一种服务网格吗?"。
While both use a sidecar architecture, each technology has a different purpose. Dapr provides distributed application features. A service mesh provides a dedicated network infrastructure layer.
尽管这两种模式都使用边车体系结构,但每种技术都有不同的用途。Dapr 提供分布式应用程序功能。服务网格提供专用的网络基础实施层。
As each works at a different level, both can work together in the same application. For example, a service mesh could provide networking communication between services. Dapr could provide application services such as state management or actor services.
它们在不同层面工作,可以在同一应用程序中协同工作。例如,服务网格可以提供服务之间的网络通信。Dapr 可以提供应用程序服务,例如状态管理或执行组件(参与者)服务。
Figure 2-9 shows an application that implements both Dapr and service mesh technology.
图2-9 显示了实施 Dapr 和服务网格技术的应用程序。
Figure 2-9. Dapr and service mesh together.
图 2-9。将 Dapr 和 服务网格组合在一起。
The Dapr online documentation cover Dapr and service mesh integration.
Dapr 联机文档介绍 Dapr 和服务网格集成。
Summary
总结
This chapter introduced you to Dapr, a Distributed Application Runtime.
本章介绍了 Dapr 分布式应用程序运行时。
Dapr is an open-source project sponsored by Microsoft with close collaboration from customers and the open-source community.
Dapr 是由 Microsoft 赞助的开源项目,与客户和开源社区密切合作。
At its core, Dapr helps reduce the inherent complexity of distributed microservice applications. It's built upon a concept of building block APIs. Dapr building blocks expose common distributed application capabilities, such as state management, service-to-service invocation, and pub/sub messaging. Dapr components lie beneath the building blocks and provide the concrete implementation for each capability. Applications bind to various components through configuration files.
Dapr 的核心有助于降低分布式微服务应用程序的固有复杂性。它以构建块 Api 的概念为基础构建。Dapr 构建块公开通用的分布式应用程序能力,例如状态管理、服务到服务调用和订阅/发布消息系统。Dapr 组件位于构建块下面,并为每项功能提供具体实现。应用程序通过配置文件绑定到各种组件。
In the next chapters, we present practical, hands-on instruction on how to use Dapr in your applications.
下一章将介绍有关如何在应用程序中使用 Dapr 的实践说明。
目录:
面向.NET开发人员的Dapr——前言
面向.NET开发人员的Dapr——分布式世界