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