面向.NET开发人员的Dapr——俯瞰Dapr
目錄:
面向.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.
下表描述了每個構建塊提供的基礎設施服務。
| 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 提供以下組件類型:
| 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——分布式世界
總結
以上是生活随笔為你收集整理的面向.NET开发人员的Dapr——俯瞰Dapr的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF 菜单栏滚动到顶部后固定的两种方法
- 下一篇: 无需羡慕,今后.NET开发想拿30k也可