@@ -12,49 +12,53 @@ tags:
12
12
13
13
## Intent
14
14
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.
15
+ Aggregate calls to microservices in a single location, the API Gateway. The user makes a single call
16
+ to the API Gateway, and the API Gateway then calls each relevant microservice.
17
17
18
18
## Explanation
19
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
20
+ With the Microservices pattern, a client may need data from multiple different microservices. If the
21
+ client called each microservice directly, that could contribute to longer load times, since the
22
+ client would have to make a network request for each microservice called. Moreover, having the
23
+ client call each microservice directly ties the client to that microservice - if the internal
24
+ implementations of the microservices change (for example, if two microservices are combined sometime
25
+ in the future) or if the location (host and port) of a microservice changes, then every client that
25
26
makes use of those microservices must be updated.
26
27
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.
28
+ The intent of the API Gateway pattern is to alleviate some of these issues. In the API Gateway
29
+ pattern, an additional entity (the API Gateway) is placed between the client and the microservices.
30
+ The job of the API Gateway is to aggregate the calls to the microservices. Rather than the client
31
+ calling each microservice individually, the client calls the API Gateway a single time. The API
32
+ Gateway then calls each of the microservices that the client needs.
31
33
32
34
Real world example
33
35
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
+ > We are implementing microservices and API Gateway pattern for an e-commerce site. In this system
37
+ > the API Gateway makes calls to the Image and Price microservices.
36
38
37
39
In plain words
38
40
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
+ > For a system implemented using microservices architecture, API Gateway is the single entry point
42
+ > that aggregates the calls to the individual microservices.
41
43
42
44
Wikipedia says
43
45
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.
46
+ > API Gateway is a server that acts as an API front-end, receives API requests, enforces throttling
47
+ > and security policies, passes requests to the back-end service and then passes the response back
48
+ > to the requester. A gateway often includes a transformation engine to orchestrate and modify the
49
+ > requests and responses on the fly. A gateway can also provide functionality such as collecting
50
+ > analytics data and providing caching. The gateway can provide functionality to support
51
+ > authentication, authorization, security, audit and regulatory compliance.
49
52
50
53
** Programmatic Example**
51
54
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 ` .
55
+ This implementation shows what the API Gateway pattern could look like for an e-commerce site. The
56
+ ` ApiGateway ` makes calls to the Image and Price microservices using the ` ImageClientImpl ` and
57
+ ` PriceClientImpl ` respectively. Customers viewing the site on a desktop device can see both price
58
+ information and an image of a product, so the ` ApiGateway ` calls both of the microservices and
59
+ aggregates the data in the ` DesktopProduct ` model. However, mobile users only see price information;
60
+ they do not see a product image. For mobile users, the ` ApiGateway ` only retrieves price
61
+ information, which it uses to populate the ` MobileProduct ` .
58
62
59
63
Here's the Image microservice implementation.
60
64
@@ -64,7 +68,6 @@ public interface ImageClient {
64
68
}
65
69
66
70
public class ImageClientImpl implements ImageClient {
67
-
68
71
@Override
69
72
public String getImagePath () {
70
73
var httpClient = HttpClient . newHttpClient();
@@ -114,7 +117,7 @@ public class PriceClientImpl implements PriceClient {
114
117
}
115
118
```
116
119
117
- And here we can see how API Gateway maps the requests to the microservices.
120
+ Here we can see how API Gateway maps the requests to the microservices.
118
121
119
122
``` java
120
123
public class ApiGateway {
0 commit comments