烂翻译系列之面向.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 应用程序。 现在,是时候探索一个端到端微服务应用,来演示怎样使用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:

  1. Three different front-end clients.
  2. An application gateway to abstract the back end from the front end.
  3. Several back-end core microservices.
  4. An event bus component that enables asynchronous pub/sub messaging.

如您所见,eShopOnContainers 包含许多活动部件:

  1. 三个不同的前端客户端。
  2. 应用程序网关,用于从前端的角度抽象后端。
  3. 多个后端核心微服务。
  4. 事件总线组件,用于异步发布/订阅消息。

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 上,因此对原始应用做了更新。 该架构包括:

  1. Single Page Application front end written in the popular Angular SPA framework. It sends user requests to an API gateway microservice.      用流行的Angular SPA 框架编写的 单页面应用程序 前端。 它将用户请求发送到 API 网关微服务。

  2. 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 操作 (例如,从目录获取品牌列表,) 并通过直接调用后端微服务来处理。

  3. 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 实现了一个 聚合器微服务 ,用于在完成操作所需的微服务间协调工作流。

  4. 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:        一组核心后端微服务实现电子商店所需的功能。 每个微服务都是自包含的,并且独立于其他微服务。 以下是广泛接受的领域分解模式,每个微服务都分离出特定的 业务功能

    • 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.      支付服务负责客户的付款业务。

    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 等)来构建服务。

  5. 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.

  1. 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.
  2. The ordering service sends order confirmation e-mails using the bindings building block.
  3. The back-end services communicate asynchronously using the publish & subscribe building block.
  4. Secret management is done by the secrets building block.
  5. The API gateway and web shopping aggregator services use the service invocation building block to invoke methods on the back-end services.
  6. The basket service uses the state management building block to store the state of the customer's shopping basket.

在上图中,可以看到哪些服务使用了哪些 Dapr 构建块。

  1. 原始 eShopOnContainers 应用演示了订单服务中的 DDD 概念和模式。 在更新后的 eShopOnDapr 中,订单服务使用参与者构建块(actor)作为替代实现。 参与者(actor)的基于回合的访问模型可以轻松实现有状态的订单处理,支持取消订单。
  2. 订单服务使用 绑定构建块发送订单确认电子邮件。
  3. 后端服务使用 publish & 订阅构建块进行异步通信。
  4. 机密管理由 机密构建块完成。
  5. API 网关和 web 购物聚合器服务使用 服务调用构建块 来调用后端服务方法。
  6. 购物篮服务使用 状态管理构建块 来存储客户购物篮的状态。

Benefits of applying Dapr to eShop

将 Dapr 应用于 eShop 的好处

In general, the use of Dapr building blocks add observability and flexibility to the application:

  1. 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.
  2. Flexibility: You can now swap out infrastructure simply by changing a component configuration file. No code changes are necessary.

通常,使用 Dapr 构建块可向应用添加可观察性和灵活性:

  1. 可观察性:通过使用 Dapr 构建块,可在服务间互相调用时和使用Dapr 组件时获取丰富的分布式跟踪,而无需编写任何代码。 在 eShopOnContainers 中,需使用大量的自定义日志记录来提供可观察性。
  2. 灵活性:你现在可以通过更改组件配置文件来仅 替换 基础设施。 不需要更改代码。

Here are some more examples of benefits offered by specific building blocks:      下面是特定构建块提供的优点的一些更多示例:

  • 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。

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 构建块和组件替换大量自定义功能,大大简化了构建微服务应用程序的复杂性。

posted @ 2021-05-25 12:25  菜鸟吊思  阅读(355)  评论(0编辑  收藏  举报