Skip to content

dnyanesh-genpact/Aggregator-Design-Pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Aggregator-Design-Pattern

The Aggregator Design Pattern for Microservices Architecture

Problem that Aggregator Design Pattern solves

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.

Solution for this problem

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.

Prerequisites

  • Java Development Kit (JDK 8 or above)
  • Maven
  • IDE like STS(Spring Tool Suite) or Eclipse

Architecture Overview

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.

Steps :

  1. Create Microservices : Create three separate Spring Boot projects (either via Spring Initializr or your preferred method).

  2. Add Dependencies : For Report Service, add just web dependency : image

    For Account and Fund Transfer service, add following dependencies : image

  3. Business Logic :

    1. Account Service - Create a REST API to get an account by account number.
    2. Fund Transfer Service - Create a REST API to get transaction details for specific account.
    3. Report Service(Aggregator Service) - Create a REST API to call above services and aggregate the data.
  4. Aggregation Logic :

    image

  5. Run the services : Start all 3 services i.e. Account Service, Fund Transfer Service and Aggregator Service.

  6. 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.

  7. 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

  8. Test Aggregator Test by calling http://localhost:5055/banking/report/getTxnDetails/976435618

Microservices Configuration

Customize the behavior of services by editing the respective application.properties file. Adjust settings such as port, logging, and error handling.

image

image

image

Sample Code for Controllers and Models

Account Controller :

image

Account Model :

image

FundTransfer Controller :

image

FundTransfer model :

image

Report Controller (Aggregator Controller) :

image

Report Model :

image

Contributing

Contributions are welcome!

Contact

For questions or feedback, please email at [email protected] OR [email protected].

About

The Aggregator Design Pattern for Microservices Architecture

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages