Skip to content

Commit 88c3b04

Browse files
iluwatar#297 Create API Gateway pattern
1 parent 1a55f3a commit 88c3b04

File tree

8 files changed

+170
-0
lines changed

8 files changed

+170
-0
lines changed

api-gateway/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-design-patterns</artifactId>
7+
<groupId>com.iluwatar</groupId>
8+
<version>1.10.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>api-gateway</artifactId>
12+
<dependencies>
13+
<dependency>
14+
<groupId>junit</groupId>
15+
<artifactId>junit</artifactId>
16+
<scope>test</scope>
17+
</dependency>
18+
</dependencies>
19+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.iluwatar.api.gateway;
2+
3+
/**
4+
* The ApiGateway aggregates calls to microservices based on the needs of the individual clients.
5+
*/
6+
public class ApiGateway {
7+
8+
private ImageService imageService = new ImageService();
9+
private PriceService priceService = new PriceService();
10+
11+
/**
12+
* Retrieves product information that desktop clients need
13+
* @return Product information for clients on a desktop
14+
*/
15+
public DesktopProduct getProductDesktop() {
16+
DesktopProduct desktopProduct = new DesktopProduct();
17+
desktopProduct.setImagePath(imageService.getImagePath());
18+
desktopProduct.setPrice(priceService.getPrice());
19+
return desktopProduct;
20+
}
21+
22+
/**
23+
* Retrieves product information that mobile clients need
24+
* @return Product information for clients on a mobile device
25+
*/
26+
public MobileProduct getProductMobile() {
27+
MobileProduct mobileProduct = new MobileProduct();
28+
mobileProduct.setPrice(priceService.getPrice());
29+
return mobileProduct;
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.iluwatar.api.gateway;
2+
3+
/**
4+
* With the Microservices pattern, a client may need data from multiple different microservices.
5+
* If the client called each microservice directly, that could contribute to longer load times,
6+
* since the client would have to make a network request for each microservice called. Moreover,
7+
* having the client call each microservice directly ties the client to that microservice - if the
8+
* internal implementations of the microservices change (for example, if two microservices are
9+
* combined sometime in the future) or if the location (host and port) of a microservice changes,
10+
* then every client that makes use of those microservices must be updated.
11+
*
12+
* <p>
13+
* The intent of the API Gateway pattern is to alleviate some of these issues. In the API Gateway
14+
* pattern, an additional entity (the API Gateway) is placed between the client and the
15+
* microservices. The job of the API Gateway is to aggregate the calls to the microservices.
16+
* Rather than the client calling each microservice individually, the client calls the API Gateway
17+
* a single time. The API Gateway then calls each of the microservices that the client needs.
18+
*
19+
* <p>
20+
* This implementation shows what the API Gateway pattern could look like for an e-commerce site.
21+
* In this case, the (@link ImageService) and (@link PriceService) represent our microservices.
22+
* Customers viewing the site on a desktop device can see both price information and an image of
23+
* a product, so the (@link ApiGateway) calls both of the microservices and aggregates the data in
24+
* the (@link DesktopProduct) model. However, mobile users only see price information; they do not
25+
* see a product image. For mobile users, the (@link ApiGateway) only retrieves price information,
26+
* which it uses to populate the (@link MobileProduct).
27+
*/
28+
public class App {
29+
30+
/**
31+
* Program entry point
32+
*
33+
* @param args
34+
* command line args
35+
*/
36+
public static void main(String[] args) {
37+
ApiGateway apiGateway = new ApiGateway();
38+
39+
DesktopProduct desktopProduct = apiGateway.getProductDesktop();
40+
System.out.println(String.format("Desktop Product \nPrice: %s\nImage path: %s",
41+
desktopProduct.getPrice(), desktopProduct.getImagePath()));
42+
43+
MobileProduct mobileProduct = apiGateway.getProductMobile();
44+
System.out.println(String.format("Mobile Product \nPrice: %s", mobileProduct.getPrice()));
45+
}
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.iluwatar.api.gateway;
2+
3+
/**
4+
* Encapsulates all of the information that a desktop client needs to display a product.
5+
*/
6+
public class DesktopProduct {
7+
8+
private String price;
9+
private String imagePath;
10+
11+
public String getPrice() {
12+
return price;
13+
}
14+
15+
public void setPrice(String price) {
16+
this.price = price;
17+
}
18+
19+
public String getImagePath() {
20+
return imagePath;
21+
}
22+
23+
public void setImagePath(String imagePath) {
24+
this.imagePath = imagePath;
25+
}
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar.api.gateway;
2+
3+
/**
4+
* Represents a microservice used to retrieve image data.
5+
*/
6+
public class ImageService {
7+
8+
/**
9+
* Gets the image path
10+
* @return the image path
11+
*/
12+
public String getImagePath() {
13+
return "/product-image.png";
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iluwatar.api.gateway;
2+
3+
/**
4+
* Encapsulates all of the information that mobile client needs to display a product.
5+
*/
6+
public class MobileProduct {
7+
8+
private String price;
9+
10+
public String getPrice() {
11+
return price;
12+
}
13+
14+
public void setPrice(String price) {
15+
this.price = price;
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar.api.gateway;
2+
3+
/**
4+
* Represents a microservice used to retrieve price data.
5+
*/
6+
public class PriceService {
7+
8+
/**
9+
* Gets the price
10+
* @return the price
11+
*/
12+
public String getPrice() {
13+
return "20";
14+
}
15+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<module>publish-subscribe</module>
9494
<module>delegation</module>
9595
<module>event-driven-architecture</module>
96+
<module>api-gateway</module>
9697
</modules>
9798

9899
<dependencyManagement>

0 commit comments

Comments
 (0)