Update readme

This commit is contained in:
Ilkka Seppälä 2020-08-10 16:25:06 +03:00
parent 10815b6469
commit 04a2be0c99

View File

@ -12,49 +12,53 @@ tags:
## Intent ## Intent
Aggregate calls to microservices in a single location: the API Gateway. The user makes a single call to the API Gateway, Aggregate calls to microservices in a single location, the API Gateway. The user makes a single call
and the API Gateway then calls each relevant microservice. to the API Gateway, and the API Gateway then calls each relevant microservice.
## Explanation ## Explanation
With the Microservices pattern, a client may need data from multiple different microservices. If the client called each With the Microservices pattern, a client may need data from multiple different microservices. If the
microservice directly, that could contribute to longer load times, since the client would have to make a network request client called each microservice directly, that could contribute to longer load times, since the
for each microservice called. Moreover, having the client call each microservice directly ties the client to that client would have to make a network request for each microservice called. Moreover, having the
microservice - if the internal implementations of the microservices change (for example, if two microservices are client call each microservice directly ties the client to that microservice - if the internal
combined sometime in the future) or if the location (host and port) of a microservice changes, then every client that 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. makes use of those microservices must be updated.
The intent of the API Gateway pattern is to alleviate some of these issues. In the API Gateway pattern, an additional The intent of the API Gateway pattern is to alleviate some of these issues. In the API Gateway
entity (the API Gateway) is placed between the client and the microservices. The job of the API Gateway is to aggregate pattern, an additional entity (the API Gateway) is placed between the client and the microservices.
the calls to the microservices. Rather than the client calling each microservice individually, the client calls the The job of the API Gateway is to aggregate the calls to the microservices. Rather than the client
API Gateway a single time. The API Gateway then calls each of the microservices that the client needs. 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.
Real world example Real world example
> We are implementing microservices and API Gateway pattern for an e-commerce site. In this system the API Gateway makes > We are implementing microservices and API Gateway pattern for an e-commerce site. In this system
calls to the Image and Price microservices. > the API Gateway makes calls to the Image and Price microservices.
In plain words In plain words
> For a system implemented using microservices architecture, API Gateway is the single entry point that aggregates the > For a system implemented using microservices architecture, API Gateway is the single entry point
calls to the individual microservices. > that aggregates the calls to the individual microservices.
Wikipedia says Wikipedia says
> API Gateway is a server that acts as an API front-end, receives API requests, enforces throttling and security > API Gateway is a server that acts as an API front-end, receives API requests, enforces throttling
policies, passes requests to the back-end service and then passes the response back to the requester. A gateway often > and security policies, passes requests to the back-end service and then passes the response back
includes a transformation engine to orchestrate and modify the requests and responses on the fly. A gateway can also > to the requester. A gateway often includes a transformation engine to orchestrate and modify the
provide functionality such as collecting analytics data and providing caching. The gateway can provide functionality to > requests and responses on the fly. A gateway can also provide functionality such as collecting
support authentication, authorization, security, audit and regulatory compliance. > analytics data and providing caching. The gateway can provide functionality to support
> authentication, authorization, security, audit and regulatory compliance.
**Programmatic Example** **Programmatic Example**
This implementation shows what the API Gateway pattern could look like for an e-commerce site. The `ApiGateway` makes This implementation shows what the API Gateway pattern could look like for an e-commerce site. The
calls to the Image and Price microservices using the `ImageClientImpl` and `PriceClientImpl` respectively. Customers `ApiGateway` makes calls to the Image and Price microservices using the `ImageClientImpl` and
viewing the site on a desktop device can see both price information and an image of a product, so the `ApiGateway` calls `PriceClientImpl` respectively. Customers viewing the site on a desktop device can see both price
both of the microservices and aggregates the data in the `DesktopProduct` model. However, mobile users only see price information and an image of a product, so the `ApiGateway` calls both of the microservices and
information; they do not see a product image. For mobile users, the `ApiGateway` only retrieves price information, which aggregates the data in the `DesktopProduct` model. However, mobile users only see price information;
it uses to populate the `MobileProduct`. they do not see a product image. For mobile users, the `ApiGateway` only retrieves price
information, which it uses to populate the `MobileProduct`.
Here's the Image microservice implementation. Here's the Image microservice implementation.
@ -64,7 +68,6 @@ public interface ImageClient {
} }
public class ImageClientImpl implements ImageClient { public class ImageClientImpl implements ImageClient {
@Override @Override
public String getImagePath() { public String getImagePath() {
var httpClient = HttpClient.newHttpClient(); var httpClient = HttpClient.newHttpClient();
@ -114,7 +117,7 @@ public class PriceClientImpl implements PriceClient {
} }
``` ```
And here we can see how API Gateway maps the requests to the microservices. Here we can see how API Gateway maps the requests to the microservices.
```java ```java
public class ApiGateway { public class ApiGateway {