面向.NET开发人员的Dapr——参考应用程序
目錄:
面向.NET開發人員的Dapr——前言
面向.NET開發人員的Dapr——分布式世界
面向.NET開發人員的Dapr——俯瞰Dapr
面向.NET開發人員的Dapr——入門
Dapr reference application
Dapr 參考應用
Earlier in the book, you've learned about the foundational benefits of Dapr. You saw how Dapr can help your team construct distributed applications while reducing architectural and operational complexity. Along the way, you've had the opportunity to build some small Dapr apps. Now, it's time to explore an end-to-end microservice application that demonstrates Dapr building blocks and components.
本書前面介紹了 Dapr 的基礎知識。你了解了 Dapr 如何幫助你的團隊構造分布式應用程序,同時降低體系結構和運營的復雜性。在此過程中,您有機會構建一些小規模的 Dapr 應用程序?,F在,是時候探索一個端到端微服務應用,來演示怎樣使用Dapr 構建基塊和組件。
But, first a little history.
但首先要回顧一些歷史。
eShop on containers
容器化的eShop
Several years ago, Microsoft, in partnership with leading community experts, released a popular guidance book, entitled?.NET Microservices for Containerized .NET Applications. Figure 3-1 shows the book:
幾年前,Microsoft (與領先社區專家合作)發布了一個熱門的指南書籍, .NET容器化微服務應用。圖3-1 顯示了本書:
Figure 3-1. .NET Microservices: Architecture for Containerized .NET Applications.
圖 3-1。.NET 微服務:容器化 .NET 應用架構。
The book dove deep into the principles, patterns, and best practices for building distributed applications. It included a full-featured microservice reference application that showcased the architectural concepts. Entitled,?eShopOnContainers, the application shows an e-Commerce storefront that sells various .NET items, including clothing and coffee mugs. Built in .NET Core, the application is cross-platform and can run in either Linux or Windows containers. Figure 3-2 shows the original eShop architecture.
本書深入闡述了有關構建分布式應用的原則、模式和最佳實踐。它包含一個功能齊全的微服務參考應用,來展現架構概念。應用名為EShopOnContainers,該應用為一個電商平臺,其銷售各種 .net 商品,包括衣服和咖啡杯子。此應用基于跨平臺的 .NET Core開發,可以在 Linux 或 Windows 容器中運行。圖3-2 顯示了 eShop on containers 的應用架構。
Figure 3-2. Original?ShopOnContainers?reference application.
圖 3-2。原始?ShopOnContainers?參考應用。
As you can see, eShopOnContainers includes many moving parts:
Three different front-end clients.
An application gateway to abstract the back end from the front end.
Several back-end core microservices.
An event bus component that enables asynchronous pub/sub messaging.
如您所見,eShopOnContainers 包含許多活動部件:
三個不同的前端客戶端。
應用程序網關,用于從前端的角度抽象后端。
多個后端核心微服務。
事件總線組件,用于異步發布/訂閱消息。
The eShopOnContainers reference application has been widely accepted across the .NET community and used to model many large commercial microservice applications.
EShopOnContainers 參考應用已被 .NET 社區中廣泛接受,并用于對許多大型商業微服務應用進行建模。
eShop on Dapr
使用Dapr的eShop
An alternative version of the eShop application accompanies this book. It's called?eShopOnDapr. The updated version evolves the earlier eShopOnContainers application by integrating Dapr building blocks and components. Figure 3-3 shows the new streamlined solution architecture:
本書隨附了Dapr版的 eShop 應用程序。稱為 eShopOnDapr。更新后的版本通過集成 Dapr 構建基塊和組件來演進早期的 eShopOnContainers 應用程序。圖3-3 顯示了新的精簡解決方案架構:
Figure 3-3. eShopOnDapr reference application architecture.
圖 3-3。eShopOnDapr 參考應用架構。
As focus of the eShopOnDapr reference application is on Dapr, the original application has been updated. The architecture consists of:
由于 eShopOnDapr 參考應用的關注點在 Dapr 上,因此對原始應用做了更新。該架構包括:
A Single Page Application front end written in the popular Angular SPA framework. It sends user requests to an API gateway microservice. ?? ??用流行的Angular SPA 框架編寫的?單頁面應用程序?前端。它將用戶請求發送到 API 網關微服務。
The API gateway abstracts the back-end core microservices from the front-end client. It's implemented using Envoy, a high performant, open-source service proxy. Envoy routes incoming requests to various back-end microservices. Most requests are simple CRUD operations (for example, get the list of brands from the catalog) and handled by a direct call to a back-end microservice. ?? ? API 網關從前端客戶端抽象后端核心微服務。它使用 Envoy(一種高性能的開源服務代理)來實現。Envoy 將傳入請求路由到各種后端微服務。大多數請求是簡單的 CRUD 操作 (例如,從目錄獲取品牌列表,) 并通過直接調用后端微服務來處理。
Other requests are logically more complex and require multiple microservices to work together. For these cases, eShopOnDapr implements an aggregator microservice that orchestrates a workflow across the microservices needed to complete the operation. ?? ?其他請求邏輯上比較復雜,并且需要多個微服務一起協同工作。在這些情況下,eShopOnDapr 實現了一個?聚合器微服務?,用于在完成操作所需的微服務間協調工作流。
The set of core back-end microservices includes functionality required for an e-Commerce store. Each is self-contained and independent of the others. Following widely accepted domain decomposing patterns, each microservice isolates a specific?business capability:? ? ? ??一組核心后端微服務實現電子商店所需的功能。每個微服務都是自包含的,并且獨立于其他微服務。以下是廣泛接受的領域分解模式,每個微服務都分離出特定的?業務功能:
Each service has its own persistent storage. Adhering to microservice best practices, there's not a shared datastore with which all services interact. ?? ??每個服務都有自己的持久存儲。秉承微服務?最佳實踐,不在所有交互的服務間共享數據存儲。
The design of each microservice is based on its individual requirements. The simple services use basic CRUD operations to access to their underlying data stores. Advanced services, like Ordering, use a Domain-Driven Design approach to manage business complexity. If necessary, services could be built across different technology stacks, such as .NET Core, Java, Go, NodeJS, and more. ?? ??每個微服務的設計都基于其各自的要求。簡單服務使用基本的 CRUD 操作來訪問其底層數據存儲。高級服務(如訂單服務)使用領域驅動設計方法來管理業務復雜性。如有必要,可以跨不同的技術堆棧(如 .NET Core、Java、Go、NodeJS 等)來構建服務。
The basket service manages the customer's shopping basket experience. ?? ??購物籃服務管理客戶的購物籃體驗。
The catalog service manages product items available for sale. ?? ??目錄服務管理可用于銷售的產品項。
The identity service manages authentication and identity. ?? ??標識服務管理身份驗證和標識。
The ordering service handles all aspects of placing and managing orders. ?? ??訂購服務處理下單和管理訂單的所有方面。
The payment service transacts the customer's payment. ?? ??支付服務負責客戶的付款業務。
Finally, the event bus wraps the Dapr publish/subscribe components. It enables asynchronous publish/subscribe messaging across microservices. Developers can plug in any Dapr-supported message broker. ?? ??最后,事件總線包裝 Dapr 發布/訂閱組件。它賦予了在微服務間異步發布/訂閱消息傳送的能力。開發人員可以插入任何 Dapr 支持的消息代理(引擎)。
Application of Dapr building blocks
Dapr 構建塊的應用
The eShopOnDapr codebase is more streamlined than the eShopOnContainers codebase. Dapr building blocks replace a large amount of error-prone plumbing code.
EShopOnDapr 基礎代碼比 eShopOnContainers 基礎代碼更簡單。Dapr 構建塊替換大量易出錯的管道代碼。
Figure 3-4 shows the Dapr integration in the eShop reference application.
圖3-4 顯示了 eShop 參考應用中的 Dapr 集成。
Figure 3-4. Dapr integration in eShopOnDapr.
圖 3-4。EShopOnDapr 中的 Dapr 集成。
In the previous figure, you can see which services use which Dapr building blocks.
The original eShopOnContainers application demonstrates DDD concepts and patterns in the ordering service. In the updated eShopOnDapr, the ordering service uses the?actor building block?as an alternative implementation. The turn-based access model of actors makes it easy to implement a stateful ordering process with support for cancellation.
The ordering service sends order confirmation e-mails using the?bindings building block.
The back-end services communicate asynchronously using the?publish & subscribe building block.
Secret management is done by the?secrets building block.
The API gateway and web shopping aggregator services use the?service invocation building block?to invoke methods on the back-end services.
The basket service uses the?state management building block?to store the state of the customer's shopping basket.
原始 eShopOnContainers 應用演示了訂單服務中的 DDD 概念和模式。在更新后的 eShopOnDapr 中,訂單服務使用參與者構建塊(actor)作為替代實現。參與者(actor)的基于回合的訪問模型可以輕松實現有狀態的訂單處理,支持取消訂單。
訂單服務使用?綁定構建塊發送訂單確認電子郵件。
后端服務使用 publish & 訂閱構建塊進行異步通信。
機密管理由?機密構建塊完成。
API 網關和 web 購物聚合器服務使用?服務調用構建塊?來調用后端服務方法。
購物籃服務使用?狀態管理構建塊?來存儲客戶購物籃的狀態。
Observability: By using the Dapr building blocks, you gain rich distributed tracing for both calls between services and to Dapr components without having to write any code. In eShopOnContainers, a large amount of custom logging is used to provide insight.
Flexibility: You can now?swap out?infrastructure simply by changing a component configuration file. No code changes are necessary.
可觀察性:通過使用 Dapr 構建塊,可在服務間互相調用時和使用Dapr 組件時獲取豐富的分布式跟蹤,而無需編寫任何代碼。在 eShopOnContainers 中,需使用大量的自定義日志記錄來提供可觀察性。
靈活性:你現在可以通過更改組件配置文件來僅?替換?基礎設施。不需要更改代碼。
Service Invocation? ? ??服務調用
With Dapr's support for mTLS, services now communicate through encrypted channels. ?? ??借助 Dapr 對 mTLS的支持,服務現在通過加密通道進行通信。
When transient errors occur, service calls are automatically retried. ?? ??發生暫時性錯誤時,服務調用會自動重試。
Automatic service discovery reduces the amount of configuration needed for services to find each other. ?? ??自動服務發現可減少服務彼此查找所需的配置量。
Publish/Subscribe? ? ??發布/訂閱
eShopOnContainer included a large amount of custom code to support both Azure Service Bus and RabbitMQ. Developers used Azure Service Bus for production and RabbitMQ for local development and testing. An?IEventBus?abstraction layer was created to enable swapping between these message brokers. This layer consisted of approximately?700 lines of error-prone code. The updated implementation with Dapr requires only?35 lines of code. That's 5% of the original lines of code! More importantly, the implementation is straightforward and easy to understand. ?? ? eShopOnContainer 包含大量自定義代碼來支持 Azure 服務總線和 RabbitMQ。開發人員使用 Azure Service Bus 進行生產和 使用RabbitMQ來本地開發和測試。定義了一個?IEventBus?作為抽象層,方便由具體的消息代理替換實現。此層包括約?700 行易出錯的代碼。使用 Dapr 的更新后的實現僅需?35 行代碼。這是原始代碼行的 5%?!更重要的是,實現既簡單又易于理解。
eShopOnDapr uses Dapr's rich ASP.NET Core integration to use pub/sub. You add?Topic?attributes to ASP.NET Core controller methods to subscribe to messages. Therefore, there's no need to write a separate message handler loop for each message broker. ?? ? eShopOnDapr 使用 Dapr 的豐富 ASP.NET Core 集成來使用發布/訂閱。將?Topic?特性添加到 ASP.NET Core 控制器方法來訂閱消息。因此,無需為每個消息代理編寫單獨的消息處理器。
Messages routed to the service as HTTP calls enable the use of ASP.NET Core middleware to add functionality, without introducing new concepts or SDKs to learn. ?? ??通過 HTTP 調用將消息路由到服務,以利用 ASP.NET Core 中間件來添加功能,而無需學習新概念或 Sdk。
Bindings? ? ??綁定
The eShopOnContainers solution contained a?to-do?item for e-mailing an order confirmation to the customer. The thought was to eventually implement a third-party email API such as SendGrid. With Dapr, implementing email notification was as easy as configuring a resource binding. There wasn't any need to learn external APIs or SDKs.? ? ??EShopOnContainers 解決方案包含一個待辦事項,?用于向客戶?發送訂單確認電子郵件。其思想是最終實現第三方電子郵件 API,如 SendGrid。利用 Dapr,實現電子郵件通知就像配置資源綁定一樣簡單。無需學習外部 Api 或 Sdk。
面向.NET開發人員的Dapr——前言
面向.NET開發人員的Dapr——分布式世界
面向.NET開發人員的Dapr——俯瞰Dapr
面向.NET開發人員的Dapr——入門
在上圖中,可以看到哪些服務使用了哪些 Dapr 構建塊。
Benefits of applying Dapr to eShop
將 Dapr 應用于 eShop 的好處
In general, the use of Dapr building blocks add observability and flexibility to the application:
通常,使用 Dapr 構建塊可向應用添加可觀察性和靈活性:
Here are some more examples of benefits offered by specific building blocks:? ? ??下面是特定構建塊提供的優點的一些更多示例:
Note
注意
The Actors building block isn't covered in the first version of this book. An extensive chapter on the Actor building block and its integration with eShopOnDapr will be included in the 1.1 update.
本書的第一版中未介紹參與者(Actors)構建塊。1.1 更新中將包含有關參與者(Actors)構建塊及其與 eShopOnDapr 的集成的詳盡章節。
Summary
總結
In this chapter, you're introduced to the eShopOnDapr reference application. It's an evolution of the widely popular eShopOnContainers microservice reference application. eShopOnDapr replaces a large amount of custom functionality with Dapr building blocks and components, dramatically simplifying the complexities required to build a microservices application.
本章介紹了 eShopOnDapr 參考應用。這是廣泛流行的 eShopOnContainers 微服務 參考應用的變體。eShopOnDapr 使用 Dapr 構建塊和組件替換大量自定義功能,大大簡化了構建微服務應用程序的復雜性。
目錄:
總結
以上是生活随笔為你收集整理的面向.NET开发人员的Dapr——参考应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谈谈MySQL面试的45个常见问题
- 下一篇: 面向.NET开发人员的Dapr——总结和