mercredi 4 septembre 2019

Aggregate data from multiple micro services in dot net core 2.2

My goal is to develop an application with microservice based architecture.In this application gateway aggregation pattern has to be implemented which means responses from multiple microservices have to be combined into a single response to be sent to UI. I am using Ocelot API Gateway for this. I have found two ways to aggregate the data from multiple microservices.
1. Using the Ocelot configuration.json file.

"Aggregates": [
    {
        "ReRouteKeys": [
            "Tom",
            "Laura"
        ],
        "UpstreamPathTemplate": "/",
        "Aggregator": "FakeDefinedAggregator"
}

  1. Using a controller in api gateway that acts a aggregator which internally calls two or more micro services and then aggregate the data into a response.

     var basket = await _basket.GetByIdAsync(data.BuyerId) ?? new BasketData(data.BuyerId);
    
        var catalogItems = await _catalog.GetCatalogItemsAsync(data.Items.Select(x => x.ProductId));
    
        foreach (var bitem in data.Items)
        {
            var catalogItem = catalogItems.SingleOrDefault(ci => ci.Id == bitem.ProductId);
            if (catalogItem == null)
            {
                return BadRequest($"Basket refers to a non-existing catalog item ({bitem.ProductId})");
            }
    
            basket.Items.Add(new BasketDataItem()
            {
                Id = bitem.Id,
                ProductId = catalogItem.Id.ToString(),
                ProductName = catalogItem.Name,
                PictureUrl = catalogItem.PictureUri,
                UnitPrice = catalogItem.Price,
                Quantity = bitem.Quantity
            });
        }
    
    

Q-1. Which one of the above ways is recommended to use?

Q-2. Is it recommended to use Controllers in the api gateway that act as an aggregator ?

Aucun commentaire:

Enregistrer un commentaire