@@ -5,14 +5,142 @@ folder: api-gateway
55permalink : /patterns/api-gateway/
66categories : Architectural
77tags :
8- - Cloud distributed
9- - Decoupling
8+ - Cloud distributed
9+ - Decoupling
10+ - Microservices
1011---
1112
1213## Intent
1314
14- Aggregate calls to microservices in a single location: the API Gateway. The user makes a single
15- call to the API Gateway, and the API Gateway then calls each relevant microservice.
15+ Aggregate calls to microservices in a single location: the API Gateway. The user makes a single call to the API Gateway,
16+ and the API Gateway then calls each relevant microservice.
17+
18+ ## Explanation
19+
20+ With the Microservices pattern, a client may need data from multiple different microservices. If the client called each
21+ microservice directly, that could contribute to longer load times, since the client would have to make a network request
22+ for each microservice called. Moreover, having the client call each microservice directly ties the client to that
23+ microservice - if the internal implementations of the microservices change (for example, if two microservices are
24+ combined sometime in the future) or if the location (host and port) of a microservice changes, then every client that
25+ makes use of those microservices must be updated.
26+
27+ The intent of the API Gateway pattern is to alleviate some of these issues. In the API Gateway pattern, an additional
28+ entity (the API Gateway) is placed between the client and the microservices. The job of the API Gateway is to aggregate
29+ the calls to the microservices. Rather than the client calling each microservice individually, the client calls the
30+ API Gateway a single time. The API Gateway then calls each of the microservices that the client needs.
31+
32+ Real world example
33+
34+ > We are implementing microservices and API Gateway pattern for an e-commerce site. In this system the API Gateway makes
35+ calls to the Image and Price microservices.
36+
37+ In plain words
38+
39+ > For a system implemented using microservices architecture, API Gateway is the single entry point that aggregates the
40+ calls to the individual microservices.
41+
42+ Wikipedia says
43+
44+ > API Gateway is a server that acts as an API front-end, receives API requests, enforces throttling and security
45+ policies, passes requests to the back-end service and then passes the response back to the requester. A gateway often
46+ includes a transformation engine to orchestrate and modify the requests and responses on the fly. A gateway can also
47+ provide functionality such as collecting analytics data and providing caching. The gateway can provide functionality to
48+ support authentication, authorization, security, audit and regulatory compliance.
49+
50+ ** Programmatic Example**
51+
52+ This implementation shows what the API Gateway pattern could look like for an e-commerce site. The ` ApiGateway ` makes
53+ calls to the Image and Price microservices using the ` ImageClientImpl ` and ` PriceClientImpl ` respectively. Customers
54+ viewing the site on a desktop device can see both price information and an image of a product, so the ` ApiGateway ` calls
55+ both of the microservices and aggregates the data in the ` DesktopProduct ` model. However, mobile users only see price
56+ information; they do not see a product image. For mobile users, the ` ApiGateway ` only retrieves price information, which
57+ it uses to populate the ` MobileProduct ` .
58+
59+ Here's the Image microservice implementation.
60+
61+ ``` java
62+ public interface ImageClient {
63+ String getImagePath ();
64+ }
65+
66+ public class ImageClientImpl implements ImageClient {
67+
68+ @Override
69+ public String getImagePath () {
70+ var httpClient = HttpClient . newHttpClient();
71+ var httpGet = HttpRequest . newBuilder()
72+ . GET ()
73+ .uri(URI . create(" http://localhost:50005/image-path" ))
74+ .build();
75+
76+ try {
77+ var httpResponse = httpClient. send(httpGet, BodyHandlers . ofString());
78+ return httpResponse. body();
79+ } catch (IOException | InterruptedException e) {
80+ e. printStackTrace();
81+ }
82+
83+ return null ;
84+ }
85+ }
86+ ```
87+
88+ Here's the Price microservice implementation.
89+
90+ ``` java
91+ public interface PriceClient {
92+ String getPrice ();
93+ }
94+
95+ public class PriceClientImpl implements PriceClient {
96+
97+ @Override
98+ public String getPrice () {
99+ var httpClient = HttpClient . newHttpClient();
100+ var httpGet = HttpRequest . newBuilder()
101+ . GET ()
102+ .uri(URI . create(" http://localhost:50006/price" ))
103+ .build();
104+
105+ try {
106+ var httpResponse = httpClient. send(httpGet, BodyHandlers . ofString());
107+ return httpResponse. body();
108+ } catch (IOException | InterruptedException e) {
109+ e. printStackTrace();
110+ }
111+
112+ return null ;
113+ }
114+ }
115+ ```
116+
117+ And here we can see how API Gateway maps the requests to the microservices.
118+
119+ ``` java
120+ public class ApiGateway {
121+
122+ @Resource
123+ private ImageClient imageClient;
124+
125+ @Resource
126+ private PriceClient priceClient;
127+
128+ @RequestMapping (path = " /desktop" , method = RequestMethod . GET )
129+ public DesktopProduct getProductDesktop () {
130+ var desktopProduct = new DesktopProduct ();
131+ desktopProduct. setImagePath(imageClient. getImagePath());
132+ desktopProduct. setPrice(priceClient. getPrice());
133+ return desktopProduct;
134+ }
135+
136+ @RequestMapping (path = " /mobile" , method = RequestMethod . GET )
137+ public MobileProduct getProductMobile () {
138+ var mobileProduct = new MobileProduct ();
139+ mobileProduct. setPrice(priceClient. getPrice());
140+ return mobileProduct;
141+ }
142+ }
143+ ```
16144
17145## Class diagram
18146![ alt text] ( ./etc/api-gateway.png " API Gateway ")
@@ -21,10 +149,11 @@ call to the API Gateway, and the API Gateway then calls each relevant microservi
21149
22150Use the API Gateway pattern when
23151
24- * you're also using the Microservices pattern and need a single point of aggregation for your
25- microservice calls
152+ * You're using microservices architecture and need a single point of aggregation for your microservice calls.
26153
27154## Credits
28155
29156* [ microservices.io - API Gateway] ( http://microservices.io/patterns/apigateway.html )
30157* [ NGINX - Building Microservices: Using an API Gateway] ( https://www.nginx.com/blog/building-microservices-using-an-api-gateway/ )
158+ * [ 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=ac7b6a57f866ac006a309d9086e8cfbd )
159+ * [ Building Microservices: Designing Fine-Grained Systems] ( https://www.amazon.com/gp/product/1491950358/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1491950358&linkId=4c95ca9831e05e3f0dadb08841d77bf1 )
0 commit comments