eShopOnContainers 看微服务④:Catalog Service
服务简介
Catalog service(目录服务)维护着所有产品信息,包括库存、价格。所以该微服务的核心业务为:
- 产品信息的维护
- 库存的更新
- 价格的维护
架构模式
先看代码结构(下图)。

主要依赖:
1、HealthCheck 健康检查
2、WebHost
3、Entity Framework
4、Autofac
5、BuildingBlocks文件夹下的EventBus,RabbitMq
其中前四项在Identity Service里面都已经用到了。事件总线EventBus是第一次用到,我们后面会详细讲到。
这个服务采用简单的数据驱动的CRUD微服务架构,来执行产品信息的创建、读取、更新和删除(CRUD)操作。

这种类型的服务在单个 ASP.NET Core Web API 项目中即可实现所有功能,该项目包括数据模型类、业务逻辑类及其数据访问类。
启动流程
我们还是从程序启动处开始看,跟identit.API差别不大。
Program.cs
Main函数,用到两个dbcontext。IntegrationEventLogContext负责记录事件日志,CatalogContext负责产品。最终数据库如下:

BuildWebHost函数:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()//使用startup类
.UseApplicationInsights()
.UseHealthChecks("/hc")//健康检查
.UseContentRoot(Directory.GetCurrentDirectory())
.UseWebRoot("Pics")//Web 根 string
... ... ... ... 此处忽略N行代码
.Build();
这里有一个UseWebRoot,用来设置web根: webroot。
默认情况下如果不指定,是 (Content Root Path)\wwwroot,前提是该路径存在。如果这个路径不存在,则使用一个没有文件操作的提供器。
startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddAppInsight(Configuration)
.AddCustomMVC(Configuration)//健康检查,跨域等
.AddCustomDbContext(Configuration)//两个dbcontext的连接字符串等属性
.AddCustomOptions(Configuration)
.AddIntegrationServices(Configuration)
.AddEventBus(Configuration)//添加事件总线
.AddSwagger(); var container = new ContainerBuilder();
container.Populate(services);
return new AutofacServiceProvider(container.Build()); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//Configure logs loggerFactory.AddAzureWebAppDiagnostics();
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace); var pathBase = Configuration["PATH_BASE"]; if (!string.IsNullOrEmpty(pathBase))
{
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
app.UsePathBase(pathBase);
} #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
app.Map("/liveness", lapp => lapp.Run(async ctx => ctx.Response.StatusCode = ));
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously app.UseCors("CorsPolicy");//跨域 app.UseMvcWithDefaultRoute();//路由 app.UseSwagger()//Swagger生成API文档
.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Catalog.API V1");
}); ConfigureEventBus(app);//配置事件总线
} protected virtual void ConfigureEventBus(IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<OrderStatusChangedToAwaitingValidationIntegrationEvent, OrderStatusChangedToAwaitingValidationIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent, OrderStatusChangedToPaidIntegrationEventHandler>();
}
}
这里有个app.UseCors("CorsPolicy"),实际上services.AddCors是写在AddCustomMVC扩展函数里面的。
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
需要注意的是UseCors必须放在 UseMvc 之前,且策略名称(CorsPolicy)必须是已经定义的。
业务实体
该服务的主要实体是商品CatalogItem,其中包含两个辅助类CatalogBrand,CatalogType:
我们在看CatalogItem.cs的时候会发现两个函数AddStock,RemoveStock
对于实体这一块:
- 进行数据库字段映射时,主键都使用了
ForSqlServerUseSequenceHiLo指定使用HI-LO高低位序列进行主键生成。 - 使用NoTracking提升查询速度
在CatalogController的构造方法中,明确指定以下代码来进行查询优化,这一点也是我们值得学习的地方。((DbContext)context).ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; - 在进行种子数据的预置时,使用了
Polly开启了Retry机制。
private Policy CreatePolicy( ILogger<CatalogContextSeed> logger, string prefix,int retries = )
{
return Policy.Handle<SqlException>().
WaitAndRetryAsync(
retryCount: retries,
sleepDurationProvider: retry => TimeSpan.FromSeconds(),
onRetry: (exception, timeSpan, retry, ctx) =>
{
logger.LogTrace($"[{prefix}] Exception {exception.GetType().Name} with message ${exception.Message} detected on attempt {retry} of {retries}");
}
);
}
public async Task SeedAsync(CatalogContext context,IHostingEnvironment env,IOptions<CatalogSettings> settings,ILogger<CatalogContextSeed> logger)
{
var policy = CreatePolicy(logger, nameof(CatalogContextSeed)); await policy.ExecuteAsync(async () =>
{
... ...
});
}
业务处理
运行起来后,我们浏览器输入 http://localhost:5101

展开catalog

对应CatalogController.cs代码
[Route("api/v1/[controller]")]//标记版本
[ApiController]
public class CatalogController : ControllerBase
{
private readonly CatalogContext _catalogContext;
private readonly CatalogSettings _settings;
private readonly ICatalogIntegrationEventService _catalogIntegrationEventService;
public CatalogController(CatalogContext context, IOptionsSnapshot<CatalogSettings> settings, ICatalogIntegrationEventService catalogIntegrationEventService)
{
_catalogContext = context ?? throw new ArgumentNullException(nameof(context));
_catalogIntegrationEventService = catalogIntegrationEventService ?? throw new ArgumentNullException(nameof(catalogIntegrationEventService));
_settings = settings.Value;
((DbContext)context).ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}
... .... ...
}
通过构造函数注入了3个对象
context,settings,catalogIntegrationEventService
他们分别在startup类的AddCustomDbContext,AddCustomOptions,AddIntegrationServices中被注册到了DI容器。
再看具体的action

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

UpdateProduct函数
[Route("items")]
[HttpPut]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType((int)HttpStatusCode.Created)]
public async Task<IActionResult> UpdateProduct([FromBody]CatalogItem productToUpdate)
{
var catalogItem = await _catalogContext.CatalogItems
.SingleOrDefaultAsync(i => i.Id == productToUpdate.Id);
if (catalogItem == null)
{
return NotFound(new { Message = $"Item with id {productToUpdate.Id} not found." });
}
var oldPrice = catalogItem.Price;
var raiseProductPriceChangedEvent = oldPrice != productToUpdate.Price;
// Update current product
catalogItem = productToUpdate;
_catalogContext.CatalogItems.Update(catalogItem);
if (raiseProductPriceChangedEvent) // 保存产品数据,如果价格发生变化,通过事件总线发布集成事件
{
//创建要通过事件总线发布的集成事件
var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);
// 通过本地事务实现原始目录数据库操作和IntegrationEventLog之间的原子性
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent);
// 通过事件总线发布,并将保存的事件标记为已发布
await _catalogIntegrationEventService.PublishThroughEventBusAsync(priceChangedEvent);
}
else //保存更新后的产品,因为产品价格没有变化。
{
await _catalogContext.SaveChangesAsync();
}
return CreatedAtAction(nameof(GetItemById), new { id = productToUpdate.Id }, null);
}
使用 PublishThroughEventBusAsync函数,通过事件总线发布事件
public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
{
try
{
await _eventLogService.MarkEventAsInProgressAsync(evt.Id);//标记事件,进行中
_eventBus.Publish(evt);
await _eventLogService.MarkEventAsPublishedAsync(evt.Id);//标记事件,发布
}
catch (Exception)
{
await _eventLogService.MarkEventAsFailedAsync(evt.Id);//标记事件,失败
}
}
通过这个事件,修改产品价格时,同步更新购物车中保存的产品信息的价格。
我们先看看eshop如何实现多个context之间的原子性的 _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent)的实现代码:
public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt)
{
//在显式BeginTransaction()中使用多个dbcontext时,使用EF核心弹性策略:
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
await ResilientTransaction.New(_catalogContext)
.ExecuteAsync(async () => {
// 通过本地事务实现原始目录数据库操作和IntegrationEventLog之间的原子性
await _catalogContext.SaveChangesAsync();
await _eventLogService.SaveEventAsync(evt, _catalogContext.Database.CurrentTransaction.GetDbTransaction());
});
}
然后ResilientTransaction.cs
public class ResilientTransaction
{
private DbContext _context;
private ResilientTransaction(DbContext context) =>
_context = context ?? throw new ArgumentNullException(nameof(context)); public static ResilientTransaction New (DbContext context) =>
new ResilientTransaction(context); public async Task ExecuteAsync(Func<Task> action)
{
//在显式BeginTransaction()中使用多个dbcontext时,使用EF核心弹性策略:
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
var strategy = _context.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(async () =>
{
using (var transaction = _context.Database.BeginTransaction())
{
await action();
transaction.Commit();
}
});
}
}
我们这样把Catalog service梳理了一遍,肯定有些地方还是不明不白的,我们后面会继续讨论。
eShopOnContainers 看微服务④:Catalog Service的更多相关文章
- eShopOnContainers 看微服务③:Identity Service
引言 通常,服务所公开的资源和 API 必须仅限受信任的特定用户和客户端访问.那进行 API 级别信任决策的第一步就是身份认证——确定用户身份是否可靠. 在微服务场景中,身份认证通常统一处理.一般有两 ...
- [转]eShopOnContainers 看微服务 ①:总体概览
本文转自:https://www.cnblogs.com/tianyamoon/p/10081177.html 一.简介 eShopOnContainers是一个简化版的基于.NET Core和Doc ...
- eShopOnContainers 看微服务⑤:消息通信
1.消息通信 传统的单体应用,组件间的调用都是使用代码级的方法函数.比如用户登录自动签到,增加积分.我们可以在登录函数调用积分模块的某个函数,为了解耦我们使用以来注入并放弃new Class()这种方 ...
- eShopOnContainers 看微服务 ①:总体概览
一.简介 eShopOnContainers是一个简化版的基于.NET Core和Docker等技术开发的面向微服务架构的参考应用. 该参考应用是一个简化版的在线商城/电子商务微服务参考示例应用. 其 ...
- eShopOnContainers 看微服务 ②:配置 启动
一.什么是docker Docker 是一个开源项目,通过把应用程序打包为可移植的.自给自足的容器(可以运行在云端或本地)的方式,实现应用程序的自动化部署. 使用 Docker 的时候,需要创建一个应 ...
- 浅谈服务治理、微服务与Service Mesh(三) Service Mesh与Serverless
作为本系列文章的第三篇(前两篇<浅谈服务治理.微服务与Service Mesh(一)Dubbo的前世今生>,<浅谈服务治理.微服务与Service Mesh(二) Spring Cl ...
- 微服务之Service Fabric 系列 (一):概览、环境安装
参考 微软官方文档 service fabric 百家号 大话微服务架构之微服务框架微软ServiceFabric正式开源 一.概述 1.概念 Azure Service Fabric 是一款分 ...
- 【Azure 微服务】Service Fabric中微服务在升级时,遇见Warning - System.Collections.Generic.KeyNotFoundException 服务无法正常运行
问题描述 使用.Net Framework 4.5.2为架构的Service Fabric微服务应用,在升级后发布到Azure Fabric中,服务无法运行.通过Service Fabric Expl ...
- 【Azure 微服务】Service Fabric, 使用ARM Template方式来更新SF集群的证书(Renew SF Certificate)
问题描述 因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)一文中,描述了因为证书过期而导致了SF集群不可用,并且通过命令dd-AzServiceFabricCluster ...
随机推荐
- Python练习一
#给一个字符串,统计其中的数字.字母和其他类型字符的个数r=raw_input("请输入一个字符串:")num=0str=0oth=0for i in r: if (i.isdig ...
- HTML5制作网页(2)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- 每个月总有那么几天!!!!XML解析
1. 介绍 1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许 ...
- 总结描述用户和组管理类命令的使用方法,系统用户相关信息,取出主机IP地址
1.列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可. [root@db146 ~]# who|cut -f1 -d' ' |sort -u root 2.取出最后 ...
- mininet的学习之三----------mininet中流表应用实战
我们看的同一个视频,工整的整理见以下网址 https://blog.csdn.net/weixin_36372879/article/details/86371583#commentBox
- java 运算符的优先级比较
口诀:淡云一笔安洛三福 单目>算数运算符>移位>比较>按位>逻辑>三目>赋值 单目运算符:+,-,++,-- 算数运算符:+,-,*,/,% 移位运算符:&l ...
- Spring Boot引入Oracle Jar
Oracle数据库的驱动依赖,maven仓库并不提供,需要手动添加 1. 下载jar文件 如ojdbc7.jar文件 将该文件放在D:\oracle\ojdbc7.jar路径下 2.然后在命令行输入 ...
- angular2 ngfor循环
angular2 在组件模板中可以循环数组集合等对象,语法非常简单,如: <ng-container *ngFor="let item of model.list"> ...
- 搭建一个microblaze的最小系统
搭建一个microblaze的最小系统 首先例化一个microblaze核 对microblaze核进行配置 勾选: 这个是使能外部存储BRAM作为程序运行缓冲区 第二个是需要勾选 这个是使能AXI数 ...
- 管理mysql数据的两条sql tips
当从B表数据更新到A表时: update A inner join B on A.aid=B.aid set A.user_name=B.username,A.phone=B.telwhere A.a ...