Git Product home page Git Product logo

ismainfinitecloud / aggregator-microservices Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 49 KB

This repository showcases the Aggregator Microservices Design Pattern. As architectures lean towards decentralization, mastering patterns like this becomes key. The Aggregator streamlines multiple microservices' responses into one, easing client interactions with intricate microservices-driven backends

Java 100.00%

aggregator-microservices's Introduction

title category language tag
Aggregator Microservices
Architectural
en
Cloud distributed
Decoupling
Microservices

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

alt text

Applicability

Use the Aggregator Microservices pattern when you need a unified API for various microservices, regardless the client device.

Credits

aggregator-microservices

aggregator-microservices's People

Contributors

ismainfinitecloud avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.