Skip to content

Commit a7b4194

Browse files
committed
iluwatar#590 explanation for Aggregator Microservices
1 parent 9ff5b9e commit a7b4194

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

aggregator-microservices/README.md

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,91 @@ tags:
1010

1111
## Intent
1212

13-
The user makes a single call to the Aggregator, and the aggregator then calls each relevant microservice and collects
14-
the data, apply business logic to it, and further publish is as a REST Endpoint.
15-
More variations of the aggregator are:
16-
- Proxy Microservice Design Pattern: A different microservice is called upon the business need.
17-
- Chained Microservice Design Pattern: In this case each microservice is dependent/ chained to a series
18-
of other microservices.
13+
The user makes a single call to the aggregator service, and the aggregator then calls each relevant microservice.
14+
15+
## Explanation
16+
17+
Real world example
18+
19+
> Our web marketplace needs information about products and their current inventory. It makes a call to an aggregator
20+
> service that in turn calls the product information microservice and product inventory microservice returning the
21+
> combined information.
22+
23+
In plain words
24+
25+
> Aggregator Microservice collects pieces of data from various microservices and returns an aggregate for processing.
26+
27+
Stack Overflow says
28+
29+
> Aggregator Microservice invokes multiple services to achieve the functionality required by the application.
30+
31+
**Programmatic Example**
32+
33+
Let's start from the data model. Here's our `Product`.
34+
35+
```java
36+
public class Product {
37+
private String title;
38+
private int productInventories;
39+
// getters and setters ->
40+
...
41+
}
42+
```
43+
44+
Next we can introduct our `Aggregator` microservice. It contains clients `ProductInformationClient` and
45+
`ProductInventoryClient` for calling respective microservices.
46+
47+
```java
48+
@RestController
49+
public class Aggregator {
50+
51+
@Resource
52+
private ProductInformationClient informationClient;
53+
54+
@Resource
55+
private ProductInventoryClient inventoryClient;
56+
57+
@RequestMapping(path = "/product", method = RequestMethod.GET)
58+
public Product getProduct() {
59+
60+
var product = new Product();
61+
var productTitle = informationClient.getProductTitle();
62+
var productInventory = inventoryClient.getProductInventories();
63+
64+
//Fallback to error message
65+
product.setTitle(requireNonNullElse(productTitle, "Error: Fetching Product Title Failed"));
66+
67+
//Fallback to default error inventory
68+
product.setProductInventories(requireNonNullElse(productInventory, -1));
69+
70+
return product;
71+
}
72+
}
73+
```
74+
75+
Here's the essence of information microservice implementation. Inventory microservice is similar, it just returns
76+
inventory counts.
77+
78+
```java
79+
@RestController
80+
public class InformationController {
81+
@RequestMapping(value = "/information", method = RequestMethod.GET)
82+
public String getProductTitle() {
83+
return "The Product Title.";
84+
}
85+
}
86+
```
87+
88+
Now calling our `Aggregator` REST API returns the product information.
89+
90+
```bash
91+
curl http://localhost:50004/product
92+
{"title":"The Product Title.","productInventories":5}
93+
```
1994

2095
## Class diagram
2196

22-
![alt text](./etc/aggregator-microservice.png "Aggregator Microservice")
97+
![alt text](./etc/aggregator-service.png "Aggregator Microservice")
2398

2499
## Applicability
25100

@@ -28,3 +103,5 @@ Use the Aggregator Microservices pattern when you need a unified API for various
28103
## Credits
29104

30105
* [Microservice Design Patterns](http://web.archive.org/web/20190705163602/http://blog.arungupta.me/microservice-design-patterns/)
106+
* [Microservices Patterns: With examples in Java](https://www.amazon.com/gp/product/1617294543/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617294543&linkId=8b4e570267bc5fb8b8189917b461dc60)
107+
* [Architectural Patterns: Uncover essential patterns in the most indispensable realm of enterprise architecture](https://www.amazon.com/gp/product/B077T7V8RC/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=B077T7V8RC&linkId=c34d204bfe1b277914b420189f09c1a4)
Binary file not shown.

0 commit comments

Comments
 (0)