* Changed database implementation. Removed static objects. * Fix Logs * Fix 40 errors from checkstyle plugin run. 139 left)) * Fix CacheStore errors from checkstyle plugin 107 left * Fix last errors in checkstyle. * Fix sonar issues * Fix issues in VALIDATE phase * Fix Bug with mongo connection. Used "Try with resources" * Add test * Added docker-compose for mongo db. MongoDb db work fixed. * Provided missing tests * Comments to start Application with mongo. * Fix some broken links * Remove extra space * Update filename * Fix some links in localization folders * Fix link * Update frontmatters * Work on patterns index page * Work on index page * Fixes according PR comments. Mainly Readme edits. * fix frontmatter * add missing png * Update pattern index.md * Add index.md for Chinese translation * update image paths * update circuit breaker image paths * Update image paths for localizations * add generated puml * Add missing image * Update img file extensions * Update the rest of the EN and ZH patterns to conform with the new website Co-authored-by: Victor Zalevskii <zvictormail@gmail.com>
title, category, language, tags
| title | category | language | tags | |||
|---|---|---|---|---|---|---|
| Aggregator Microservices | Architectural | en |
|
Intent
The user makes a single call to the aggregator service, and the aggregator then calls each relevant microservice.
Explanation
Real world example
Our web marketplace needs information about products and their current inventory. It makes a call to an aggregator service which in turn calls the product information microservice and product inventory microservice returning the combined information.
In plain words
Aggregator Microservice collects pieces of data from various microservices and returns an aggregate for processing.
Stack Overflow says
Aggregator Microservice invokes multiple services to achieve the functionality required by the application.
Programmatic Example
Let's start from the data model. Here's our Product.
public class Product {
private String title;
private int productInventories;
// getters and setters ->
...
}
Next we can introduce our Aggregator microservice. It contains clients ProductInformationClient and
ProductInventoryClient for calling respective microservices.
@RestController
public class Aggregator {
@Resource
private ProductInformationClient informationClient;
@Resource
private ProductInventoryClient inventoryClient;
@RequestMapping(path = "/product", method = RequestMethod.GET)
public Product getProduct() {
var product = new Product();
var productTitle = informationClient.getProductTitle();
var productInventory = inventoryClient.getProductInventories();
//Fallback to error message
product.setTitle(requireNonNullElse(productTitle, "Error: Fetching Product Title Failed"));
//Fallback to default error inventory
product.setProductInventories(requireNonNullElse(productInventory, -1));
return product;
}
}
Here's the essence of information microservice implementation. Inventory microservice is similar, it just returns inventory counts.
@RestController
public class InformationController {
@RequestMapping(value = "/information", method = RequestMethod.GET)
public String getProductTitle() {
return "The Product Title.";
}
}
Now calling our Aggregator REST API returns the product information.
curl http://localhost:50004/product
{"title":"The Product Title.","productInventories":5}
Class diagram
Applicability
Use the Aggregator Microservices pattern when you need a unified API for various microservices, regardless the client device.
