I am working on microservice architecture and I want to aggregate data from two microservices.
For example, Frontend calls the API Gateway and API Gateway calls two microservices Customer and Order microservices. Customer microservice returns customer details and Order microservice returns all ordered products by customer.
This is the format returned by API Gateway after aggregation from two microservice using Ocelot or Azure API Management.
Format 1
{
"Customers":[
{
"customerId":1001,
"customerName":"Tom"
},
{
"customerId":1002,
"customerName":"Jerry"
}
],
"Orders":[
{
"CustomerId":1001,
"Orders":[
{
"ProductId":"PRO1",
"ProductName":"Books"
},
{
"ProductId":"PRO2",
"ProductName":"Pens"
}
]
},
{
"CustomerId":1002,
"Orders":[
{
"ProductId":"PRO3",
"ProductName":"Pencils"
},
{
"ProductId":"PRO4",
"ProductName":"Toys"
}
]
}
]
}
The Format that I want is format 2.
Format 2
{
"OrderDetails":[
{
"customerId":1001,
"customerName":"Tom",
"Orders":[
{
"ProductId":"PRO1",
"ProductName":"Books"
},
{
"ProductId":"PRO2",
"ProductName":"Pens"
}
]
},
{
"customerId":1002,
"customerName":"Jerry",
"Orders":[
{
"ProductId":"PRO3",
"ProductName":"Pencils"
},
{
"ProductId":"PRO4",
"ProductName":"Toys"
}
]
}
]
}
The second format is achievable using Ocelot but the merging of data is based on the ids on Gateway and it requires some processing.
Is it a good practice to aggregate data on Gateway using business logic. If not what practices should be followed for this kind of aggregation?
It would be nice if you can provide some references for achieving this aggregation using the Azure API Management.