eShopOnContainers 知多少[11]:服务间通信之gRPC

最近翻看最新3.0 eShopOncontainers源码,发现其在架构选型中补充了 gRPC 进行服务间通信。那就索性也写一篇,作为系列的补充。

gRPC

老规矩,先来理一下gRPC的基本概念。gRPC是Google开源的RPC框架,比肩dubbo、thrift、brpc。其优势在于:
1. 基于proto buffer:二进制协议,具有高性能的序列化机制。相较于JSON(文本协议)而言,首先从数据包上就有60%-80%的减小,其次其解包速度仅需要简单的数学运算完成,无需复杂的词法语法分析,具有8倍以上的性能提升。
2. 支持数据流。
3. 基于proto 文件:可以更方便的在客户端和服务端之间进行交互。
4. gRPC语言无关性: 所有服务都是使用原型文件定义的。这些文件基于protobuffer语言,并定义服务的接口。基于原型文件,可以为每种语言生成用于创建服务端和客户端的代码。其中protoc编译工具就支持将其生成C #代码。从.NET Core 3 中,gRPC在工具和框架中深度集成,开发者会有更好的开发体验。

gRPC 在 eShopOncontainers 的应用

首先来理一下eShopOncontainers 中服务间同步通信的技术选型,主要还是是基于HTTP/REST,gRPC作为补充。

在eShopOncontainers中Ordering API、Catalog API、Basket API微服务通过gRPC端点暴露服务。其中Mobile Shopping、Web Shopping BFFs使用gRPC客户端访问服务。以下以Ordering API gRPC 服务举例说明。

订单微服务中定义了一个gRPC服务,用于从购物车创建订单。

服务端实现

proto文件定义如下:

syntax = "proto3"; option csharp_namespace = "GrpcOrdering"; package OrderingApi; service OrderingGrpc { rpc CreateOrderDraftFromBasketData(CreateOrderDraftCommand) returns (OrderDraftDTO) {} } message CreateOrderDraftCommand { string buyerId = 1; repeated BasketItem items = 2; } message BasketItem { string id = 1; int32 productId = 2; string productName = 3; double unitPrice = 4; double oldUnitPrice = 5; int32 quantity = 6; string pictureUrl = 7; } message OrderDraftDTO { double total = 1; repeated OrderItemDTO orderItems = 2; } message OrderItemDTO { int32 productId = 1; string productName = 2; double unitPrice = 3; double discount = 4; int32 units = 5; string pictureUrl = 6; }

服务实现,主要是借助Mediator充当CommandBus进行命令分发,具体实现如下:

namespace GrpcOrdering { public class OrderingService : OrderingGrpc.OrderingGrpcBase { private readonly IMediator _mediator; private readonly ILogger<OrderingService> _logger; public OrderingService(IMediator mediator, ILogger<OrderingService> logger) { _mediator = mediator; _logger = logger; } public override async Task<OrderDraftDTO> CreateOrderDraftFromBasketData(CreateOrderDraftCommand createOrderDraftCommand, ServerCallContext context) { _logger.LogInformation("Begin gRPC call from method {Method} for ordering get order draft {CreateOrderDraftCommand}", context.Method, createOrderDraftCommand); _logger.LogTrace( "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", createOrderDraftCommand.GetGenericTypeName(), nameof(createOrderDraftCommand.BuyerId), createOrderDraftCommand.BuyerId, createOrderDraftCommand); var command = new AppCommand.CreateOrderDraftCommand( createOrderDraftCommand.BuyerId, this.MapBasketItems(createOrderDraftCommand.Items)); var data = await _mediator.Send(command); if (data != null) { context.Status = new Status(StatusCode.OK, $" ordering get order draft {createOrderDraftCommand} do exist"); return this.MapResponse(data); } else { context.Status = new Status(StatusCode.NotFound, $" ordering get order draft {createOrderDraftCommand} do not exist"); } return new OrderDraftDTO(); } public OrderDraftDTO MapResponse(AppCommand.OrderDraftDTO order) { var result = new OrderDraftDTO() { Total = (double)order.Total, }; order.OrderItems.ToList().ForEach(i => result.OrderItems.Add(new OrderItemDTO() { Discount = (double)i.Discount, PictureUrl = i.PictureUrl, ProductId = i.ProductId, ProductName = i.ProductName, UnitPrice = (double)i.UnitPrice, Units = i.Units, })); return result; } public IEnumerable<ApiModels.BasketItem> MapBasketItems(RepeatedField<BasketItem> items) { return items.Select(x => new ApiModels.BasketItem() { Id = x.Id, ProductId = x.ProductId, ProductName = x.ProductName, UnitPrice = (decimal)x.UnitPrice, OldUnitPrice = (decimal)x.OldUnitPrice, Quantity = x.Quantity, PictureUrl = x.PictureUrl, }); } } }

同时,服务端还要注册gRPC的请求处理管道:

app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); endpoints.MapControllers(); endpoints.MapGrpcService<OrderingService>(); }); 客户端调用

接下来看下客户端[web.bff.shopping]怎么消费的:

public class OrderingService : IOrderingService { private readonly UrlsConfig _urls; private readonly ILogger<OrderingService> _logger; public readonly HttpClient _httpClient; public OrderingService(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger<OrderingService> logger) { _urls = config.Value; _httpClient = httpClient; _logger = logger; } public async Task<OrderData> GetOrderDraftAsync(BasketData basketData) { return await GrpcCallerService.CallService(_urls.GrpcOrdering, async channel => { var client = new OrderingGrpc.OrderingGrpcClient(channel); _logger.LogDebug(" gRPC client created, basketData={@basketData}", basketData); var command = MapToOrderDraftCommand(basketData); var response = await client.CreateOrderDraftFromBasketDataAsync(command); _logger.LogDebug(" gRPC response: {@response}", response); return MapToResponse(response, basketData); }); } private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData) { if (orderDraft == null) { return null; } var data = new OrderData { Buyer = basketData.BuyerId, Total = (decimal)orderDraft.Total, }; orderDraft.OrderItems.ToList().ForEach(o => data.OrderItems.Add(new OrderItemData { Discount = (decimal)o.Discount, PictureUrl = o.PictureUrl, ProductId = o.ProductId, ProductName = o.ProductName, UnitPrice = (decimal)o.UnitPrice, Units = o.Units, })); return data; } private CreateOrderDraftCommand MapToOrderDraftCommand(BasketData basketData) { var command = new CreateOrderDraftCommand { BuyerId = basketData.BuyerId, }; basketData.Items.ForEach(i => command.Items.Add(new BasketItem { Id = i.Id, OldUnitPrice = (double)i.OldUnitPrice, PictureUrl = i.PictureUrl, ProductId = i.ProductId, ProductName = i.ProductName, Quantity = i.Quantity, UnitPrice = (double)i.UnitPrice, })); return command; } }

其中,GrpcCallerService是对gRPC Client的一层封装,主要是为了解决未启用TLS无法使用gRPC的问题。

不启用TLS使用gRPC

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpszzj.html