中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

eShopOnContainers 知多少[7]:Basket microservice

購物車界面

引言

Basket microservice(購物車微服務)主要用于處理購物車的業務邏輯,包括:

  1. 購物車商品的CRUD
  2. 訂閱商品價格更新事件,進行購物車商品同步處理
  3. 購物車結算事件發布
  4. 訂閱訂單成功創建事件,進行購物車的清空操作

架構模式

數據驅動/CRUD 微服務設計

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

核心技術選型:

  1. ASP.NET Core Web API
  2. Entity Framework Core
  3. Redis
  4. Swashbuckle(可選)
  5. Autofac
  6. Eventbus
  7. Newtonsoft.Json

實體建模和持久化

該微服務的核心領域實體是購物車,其類圖如下:

其中CustomerBasketBasketItem為一對多關系,使用倉儲模式進行持(chi)久(jiu)化。

  1. 通過對CustomerBasket對象進行json格式的序列化和反序列化來完成在redis中的持久化和讀取。
  2. 以單例模式注入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)消費:

  1. 事件發布:當用戶點擊購物車結算時,發布用戶結算事件。
  2. 事件消費:訂單創建成功后,進行購物車的清空
  3. 事件消費:商品價格更新后,進行購物車相關商品的價格同步
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();
  1. 全局異常過濾器是通過定義BasketDomainException異常和HttpGlobalExceptionFilter過濾器來實現的。
  2. 模型驗證過濾器是通過繼承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):

  1. 配置授權Url
  2. 配置TokenUrl
  3. 指定授權范圍
  4. 注入授權檢查過濾器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存儲。

posted @ 2019-01-04 11:21  「圣杰」  閱讀(2762)  評論(0)    收藏  舉報