eShopOnContainers 知多少[7]:Basket microservice

引言
Basket microservice(購物車微服務)主要用于處理購物車的業務邏輯,包括:
- 購物車商品的CRUD
- 訂閱商品價格更新事件,進行購物車商品同步處理
- 購物車結算事件發布
- 訂閱訂單成功創建事件,進行購物車的清空操作
架構模式

如上圖所示,本微服務采用數據驅動的CRUD微服務架構,來執行購物車商品的維護操作。并使用Redis數據庫進行持久化。
這種類型的服務在單個 ASP.NET Core Web API 項目中即可實現所有功能,該項目包括數據模型類、業務邏輯類及其數據訪問類。其項目結構如下:

核心技術選型:
- ASP.NET Core Web API
- Entity Framework Core
- Redis
- Swashbuckle(可選)
- Autofac
- Eventbus
- Newtonsoft.Json
實體建模和持久化
該微服務的核心領域實體是購物車,其類圖如下:
其中CustomerBasket與BasketItem為一對多關系,使用倉儲模式進行持(chi)久(jiu)化。
- 通過對
CustomerBasket對象進行json格式的序列化和反序列化來完成在redis中的持久化和讀取。 - 以單例模式注入redis連接
ConnectionMultiplexer,該對象最終通過構造函數注入到RedisBasketRepository中。
services.AddSingleton<ConnectionMultiplexer>(sp =>
{
var settings = sp.GetRequiredService<IOptions<BasketSettings>>().Value;
var configuration = ConfigurationOptions.Parse(settings.ConnectionString, true);
configuration.ResolveDns = true;
return ConnectionMultiplexer.Connect(configuration);
});
事件的注冊和消費
在本服務中主(zhu)要需要處理(li)以(yi)下(xia)事(shi)件(jian)的發布和(he)消費:
- 事件發布:當用戶點擊購物車結算時,發布用戶結算事件。
- 事件消費:訂單創建成功后,進行購物車的清空
- 事件消費:商品價格更新后,進行購物車相關商品的價格同步
private void ConfigureEventBus(IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>();
}
以上都是基于事件總線來達成。
認證和授權
購物車管理界面是需要認證和授權。那自然需要與上游的Identity Microservice進行(xing)銜接。在啟(qi)動類進行(xing)認證中間件的配置。
private void ConfigureAuthService(IServiceCollection services)
{
// prevent from mapping "sub" claim to nameidentifier.
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
options.Audience = "basket";
});
}
protected virtual void ConfigureAuth(IApplicationBuilder app)
{
if (Configuration.GetValue<bool>("UseLoadTest"))
{
app.UseMiddleware<ByPassAuthMiddleware>();
}
app.UseAuthentication();
}
手動啟用斷路器
在該微服務中,定義了一個中斷中間件:FailingMiddleware,通過訪問//localhost:5103/failing獲取該中間件的啟用狀態,通過請求參數指定:即通過//localhost:5103/failing?enable和//localhost:5103/failing?disable來手動中斷和恢復服務,來模擬斷路,以便用于測試斷路器模式。
開啟斷路后,當訪問購物車頁面時,Polly在重試指定次數依然無法訪問服務時,就會拋出BrokenCircuitException異常,通過(guo)捕捉該異常告知用戶(hu)稍后再試(shi)。
public class CartController : Controller
{
//…
public async Task<IActionResult> Index()
{
try
{
var user = _appUserParser.Parse(HttpContext.User);
//Http requests using the Typed Client (Service Agent)
var vm = await _basketSvc.GetBasket(user);
return View(vm);
}
catch (BrokenCircuitException)
{
// Catches error when Basket.api is in circuit-opened mode
HandleBrokenCircuitException();
}
return View();
}
private void HandleBrokenCircuitException()
{
TempData["BasketInoperativeMsg"] = "Basket Service is inoperative, please try later on. (Business message due to Circuit-Breaker)";
}
}

注入過濾器
在配置MVC服務(wu)時指定了(le)兩個(ge)過(guo)濾器:全局異常過(guo)濾器和(he)模型(xing)驗證過(guo)濾器。
// Add framework services.
services.AddMvc(options =>
{
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
options.Filters.Add(typeof(ValidateModelStateFilter));
}).AddControllersAsServices();
- 全局異常過濾器是通過定義
BasketDomainException異常和HttpGlobalExceptionFilter過濾器來實現的。 - 模型驗證過濾器是通過繼承
ActionFilterAttribute特性實現的ValidateModelStateFilter來獲取模型狀態中的錯誤。
public class ValidateModelStateFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.ModelState.IsValid)
{
return;
}
var validationErrors = context.ModelState
.Keys
.SelectMany(k => context.ModelState[k].Errors)
.Select(e => e.ErrorMessage)
.ToArray();
var json = new JsonErrorResponse
{
Messages = validationErrors
};
context.Result = new BadRequestObjectResult(json);
}
}
SwaggerUI認證授權集成
因為(wei)默認(ren)啟用了(le)安全認(ren)證,所以為(wei)了(le)方便在(zai)SwaggerUI界面進行(xing)測試,那(nei)么我們就必須為(wei)其集成(cheng)認(ren)證授權。代碼如(ru)下:
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new Info
{
Title = "Basket HTTP API",
Version = "v1",
Description = "The Basket Service HTTP API",
TermsOfService = "Terms Of Service"
});
options.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Type = "oauth2",
Flow = "implicit",
AuthorizationUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize",
TokenUrl = $"{Configuration.GetValue<string>("IdentityUrlExternal")}/connect/token",
Scopes = new Dictionary<string, string>()
{
{ "basket", "Basket API" }
}
});
options.OperationFilter<AuthorizeCheckOperationFilter>();
});
其(qi)中有主要做了三件事(shi):
- 配置授權Url
- 配置TokenUrl
- 指定授權范圍
- 注入授權檢查過濾器
AuthorizeCheckOperationFilter用于攔截需要授權的請求
public class AuthorizeCheckOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
// Check for authorize attribute
var hasAuthorize = context.ApiDescription.ControllerAttributes().OfType<AuthorizeAttribute>().Any() ||
context.ApiDescription.ActionAttributes().OfType<AuthorizeAttribute>().Any();
if (hasAuthorize)
{
operation.Responses.Add("401", new Response { Description = "Unauthorized" });
operation.Responses.Add("403", new Response { Description = "Forbidden" });
operation.Security = new List<IDictionary<string, IEnumerable<string>>>();
operation.Security.Add(new Dictionary<string, IEnumerable<string>>
{
{ "oauth2", new [] { "basketapi" } }
});
}
}
}
最后
本服務較之前講的Catalog microservice 而言,主要是多了一個認證(zheng)和redis存儲。
??面向.NET開發者的AI Agent 開發課程【.NET+AI | 智能體開發進階】已上線,歡迎掃碼加入學習。??
關注我的公眾號『向 AI 而行』,我們微信不見不散。
閱罷此文,如果您覺得本文不錯并有所收獲,請【打賞】或【推薦】,也可【評論】留下您的問題或建議與我交流。 你的支持是我不斷創作和分享的不竭動力!
