Data Aggregation in API Gateway - Zuul
Asked Answered
P

2

7

I'm looking for a solution that would provide kind of data Aggregation in API Gateway. I am using spring cloud netflix zuul for API gateway. I created 3 micro services using spring boot -

Catalog - All products 
DeviceInfo - a particular product detail 
Inventory - product stock

Here is Zuul configuration -

zuul.routes.deviceInfo.path=/device/deviceInfo/**
zuul.routes.deviceInfo.url=http://localhost:9002/getDeviceInfo

zuul.routes.catalog.path=/device/all/**
zuul.routes.catalog.url=http://localhost:9001/getProductCatalog

zuul.routes.inventory.path=/device/stock/**
zuul.routes.inventory.url=http://localhost:9003/getInventory

ribbon.eureka.enabled=false

server.port=8080

In product detail page I need to make two calls -

http://localhost:8080/device/deviceInfo/ - for product details
http://localhost:8080/device/stock/ - for stock details

Is there any way to make a single call to API gateway which will combine the results of above two calls? Both calls are giving JSON in response.

Pontificate answered 17/8, 2016 at 10:5 Comment(4)
but why would you do that you are creating individual services to get response individually.Audra
May be I have wrong concept of API Gateway. So, is it fine to make two calls? Yes, both services have individual responsibilities. Just thinking a scenario if we have to aggregate response of two or more services based on some business logic. What would we do then? Or, this situation should not occurred in micro service development?Pontificate
I am not sure if zuul is actually suppose to do that it is mostly used for routing. Maybe you can try and read on zuul filters if that can help you in any way to consolidate the responses. But i strongly think zuul is not the place to consolidate responses. You have the services separated because there responsibilities are different and each serve there own purpose.In case there is a need to combine the two responses then its better to create a new one that does that or look into some other solution out there.Audra
Did you find out how to aggregate them into a single call. ?Poultryman
M
2

You may use aggregator-microservices pattern but not in ZUUL. Keep Zuul as stateless gateway. Aggregator microservice should have client code like this

public String getProductTitle() {
    String response = null;
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
      HttpGet httpGet = new HttpGet("http://localhost:51515/information");
      try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
        response = EntityUtils.toString(httpResponse.getEntity());
      }
    } catch (IOException e) {
      LOGGER.error("Exception caught.", e);
    }
    return response;
  }
}

Please have look at https://github.com/iluwatar/java-design-patterns/tree/master/aggregator-microservices

Mancunian answered 13/3, 2018 at 21:40 Comment(0)
G
0

I think the original question is about API Composition pattern as documented here. API composition is a commonly used pattern in a microservices ecosystem where a single call from a Client could result in calls to many microservices and return the consolidated result. Doing this in the API Gateway is a fairly common practice and is also documented here.

I myself looked for this solution with zuul some time ago but didn't find it. Maybe things have changed since then.

Geniegenii answered 22/1, 2021 at 22:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.