The Aggregator Design Pattern for Microservices Architecture
When breaking the business functionality into several smaller logical pieces of code, it becomes necessary to think about how to collaborate the data returned by each service. This responsibility cannot be left with the consumer, as then it might need to understand the internal implementation of the producer application.
The Aggregator pattern helps to address this. It talks about how we can aggregate the data from different services and then send the final response to the consumer. A composite aggregator microservice will make calls to all the required microservices, consolidate the data, and transform the data before sending back.
- Java Development Kit (JDK 8 or above)
- Maven
- IDE like STS(Spring Tool Suite) or Eclipse
We will create 3 microservices :
- Account Service -> Manages Bank Account creation and fetching
- Fund Transfer Service -> Manages trasnfer of funds for accounts
- Report Service -> This will be Aggregator Service that will aggregate data from Account Service and Fund Transfer Service to send a unified response back to client.
-
Create Microservices : Create three separate Spring Boot projects (either via Spring Initializr or your preferred method).
-
Add Dependencies : For Report Service, add just web dependency :
For Account and Fund Transfer service, add following dependencies :
-
Business Logic :
- Account Service - Create a REST API to get an account by account number.
- Fund Transfer Service - Create a REST API to get transaction details for specific account.
- Report Service(Aggregator Service) - Create a REST API to call above services and aggregate the data.
-
Aggregation Logic :
-
Run the services : Start all 3 services i.e. Account Service, Fund Transfer Service and Aggregator Service.
-
Test and connect h2 DB connection : Connect to Account Service DB using jdbc:h2:mem:AccountService_DB on h2 console. Connect to Account Service DB using jdbc:h2:mem:FundTransfer_DB on h2 console.
-
Make few post calls(So that we can have sample data in DB to fetch later) on both Account and Fund Transfer Services using following URLs : Account Service POST call - http://localhost:5051/banking/account/createAccount Fund Transfer Service POST call - http://localhost:5052/banking/fundTransfer/newFundTransferRequest
-
Test Aggregator Test by calling http://localhost:5055/banking/report/getTxnDetails/976435618
Customize the behavior of services by editing the respective application.properties file. Adjust settings such as port, logging, and error handling.
Account Controller :
Account Model :
FundTransfer Controller :
FundTransfer model :
Report Controller (Aggregator Controller) :
Report Model :
Contributions are welcome!
For questions or feedback, please email at [email protected] OR [email protected].