1
votes

According to article https://dzone.com/articles/building-microservices-using there is a statement:

The API Gateway is responsible for request routing, composition, and protocol translation. All requests from clients first go through the API Gateway. It then routes requests to the appropriate microservice. The API Gateway will often handle a request by invoking multiple microservices and aggregating the results.

I'm wondering, based on Zuul example, how the API Gateway achiving this?

Let's imagine we have a 2 microservices, one which retrives all available product names and second one which returns descriptions of products. In monolithic architecture we would have only one request to get all needed data. In microservice architecture API gateway should combine responses (from both microservices) and return one response.

How this funcionality should be implemented? Are there any guidelines or articles on that?

1

1 Answers

0
votes

Not all API gateway supports aggregation. This is an example of aggregating response of two service for a single client call using nginx.

The side effect of this being it introduces coupling at the API gateway layer.

Another possible solution is to use aggregation microservice. The main responsibility of this service is to provide a client side api. It can make multiple calls to other microservices to formulate the response for the client.See sample below

  @RequestMapping(path = "/product", method = RequestMethod.GET)
  public Product getProduct() {

    var product = new Product();
    String productTitle = informationClient.getProductTitle();
    Integer productInventory = inventoryClient.getProductInventories();

    if (productTitle != null) {
      product.setTitle(productTitle);
    } else {
      product.setTitle("Error: Fetching Product Title Failed"); //Fallback to error message
    }

    if (productInventory != null) {
      product.setProductInventories(productInventory);
    } else {
      product.setProductInventories(-1); //Fallback to default error inventory
    }

    return product;
  }

Full example here