eShopOnContainers 看微服务④:Catalog Service

服务简介


Catalog service(目录服务)维护着所有产品信息,包括库存、价格。所以该微服务的核心业务为:


  1. 产品信息的维护

  2. 库存的更新

  3. 价格的维护


架构模式


先看代码结构(下图)。


eShopOnContainers 看微服务④:Catalog Service_第1张图片


主要依赖:


1、HealthCheck 健康检查


2、WebHost


3、Entity Framework


4、Autofac


5、BuildingBlocks文件夹下的EventBus,RabbitMq


其中前四项在Identity Service里面都已经用到了。事件总线EventBus是第一次用到,我们后面会详细讲到。 


这个服务采用简单的数据驱动的CRUD微服务架构,来执行产品信息的创建、读取、更新和删除(CRUD)操作。


eShopOnContainers 看微服务④:Catalog Service_第2张图片


 这种类型的服务在单个 ASP.NET Core Web API 项目中即可实现所有功能,该项目包括数据模型类、业务逻辑类及其数据访问类。 


启动流程 


 我们还是从程序启动处开始看,跟identit.API差别不大。


Program.cs


Main函数,用到两个dbcontext。IntegrationEventLogContext负责记录事件日志,CatalogContext负责产品。最终数据库如下:


eShopOnContainers 看微服务④:Catalog Service_第3张图片


BuildWebHost函数:

eShopOnContainers 看微服务④:Catalog Service_第4张图片

这里有一个UseWebRoot,用来设置web根: webroot


默认情况下如果不指定,是 (Content Root Path)\wwwroot,前提是该路径存在。如果这个路径不存在,则使用一个没有文件操作的提供器。


startup.cs

 

eShopOnContainers 看微服务④:Catalog Service_第5张图片

eShopOnContainers 看微服务④:Catalog Service_第6张图片

这里有个app.UseCors("CorsPolicy"),实际上services.AddCors是写在AddCustomMVC扩展函数里面的。 

eShopOnContainers 看微服务④:Catalog Service_第7张图片

需要注意的是UseCors必须放在 UseMvc 之前,且策略名称(CorsPolicy)必须是已经定义的


业务实体


 


该服务的主要实体是商品CatalogItem,其中包含两个辅助类CatalogBrand,CatalogType:


我们在看CatalogItem.cs的时候会发现两个函数AddStock,RemoveStock 


对于实体这一块:


  1. 进行数据库字段映射时,主键都使用了ForSqlServerUseSequenceHiLo指定使用HI-LO高低位序列进行主键生成。

  2. 使用NoTracking提升查询速度
    CatalogController的构造方法中,明确指定以下代码来进行查询优化,这一点也是我们值得学习的地方。((DbContext)context).ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

  3. 在进行种子数据的预置时,使用了Polly开启了Retry机制。

eShopOnContainers 看微服务④:Catalog Service_第8张图片

业务处理


运行起来后,我们浏览器输入 http://localhost:5101 


eShopOnContainers 看微服务④:Catalog Service_第9张图片


展开catalog


eShopOnContainers 看微服务④:Catalog Service_第10张图片


对应CatalogController.cs代码

eShopOnContainers 看微服务④:Catalog Service_第11张图片

通过构造函数注入了3个对象


context,settings,catalogIntegrationEventService
他们分别在startup类的AddCustomDbContext,AddCustomOptions,AddIntegrationServices中被注册到了DI容器。


再看具体的action


eShopOnContainers 看微服务④:Catalog Service_第12张图片


通过ProducesResponseType描述HttpStatusCode的返回状态,200,404


eShopOnContainers 看微服务④:Catalog Service_第13张图片


 UpdateProduct函数

eShopOnContainers 看微服务④:Catalog Service_第14张图片

这里通过EventBus发布了一个事件,通过这个事件,修改产品价格时,同步更新购物车中保存的产品信息的价格。我们这里暂时不做详细讨论。 


我们先看看eshop如何实现多个context之间的原子性的 _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent)的实现代码:

eShopOnContainers 看微服务④:Catalog Service_第15张图片

然后看ResilientTransaction.cs

eShopOnContainers 看微服务④:Catalog Service_第16张图片

相关文章:

  • eShopOnContainers 看微服务 ①:总体概览

  • eShopOnContainers 看微服务 ②:配置 启动

  • eShopOnContainers 看微服务③:Identity Service

  • eShopOnContainers 知多少[1]:总体概览

  • eShopOnContainers 知多少[2]:Run起来

  • eShopOnContainers 知多少[3]:Identity Microservice

  • eShopOnContainers 知多少[4]:Catalog microservice

  • Catalog Service - 解析微软微服务架构eShopOnContainers(三)

  • eShopOnContainers 知多少[5]:EventBus With RabbitMQ

  • EventBus In eShop -- 解析微软微服务架构eShopOnContainers(四)

  • eShopOnContainers 是一个基于微服务的.NET Core示例框架

 
   

原文地址:https://www.cnblogs.com/tianyamoon/p/10141221.html


.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com

640?wx_fmt=jpeg

你可能感兴趣的:(eShopOnContainers 看微服务④:Catalog Service)