This a translation of article ( http://microservices.io/patterns/apigateway.html) originally written and Copyrighted by Chris Richardson ( http://twitter.com/crichardson).
Mode: API GatewayBackground
Let's say you create an online store using MicroServices mode and are implementing the product detail page. You need to develop multiple versions of the Product Details user interface:
for third parties public item details.
Product details can show a lot of information about a product . For example, Amazon.com on the POJOs in Action detail page shows:
Basic information about a book, such as title, author, price, etc.
Purchase History of Books
Is there any goods
Purchase parameters
Goods purchased at the same time as this book
What else did the user who bought the book buy?
User reviews
Rating of Sellers
...
Now that the online store uses the MicroServices model, the product details data is expanded through the service. Such as:
Commodity Information Services (Product info Service)-Basic product information such as title, author
Price Services (Pricing Service)-Product price
Order service-Product purchase history
Inventory Services (Inventory service)-goods are in stock
Commenting services (Review service)-User reviews ...
Therefore, the code that displays the product details needs to obtain information from all of these services.
Problem
How do clients of MicroServices-based applications access these standalone services?
Driving force
The API granularity provided by MicroServices is usually different from the client's requirements. MicroServices generally provide fine-grained APIs, which means that clients need to interact with multiple services. As mentioned above, the client that needs the commodity details pulls the data from a large number of services.
Different clients require different data. For example, the desktop version of the product detail page is usually more detailed than the mobile version.
Different types of clients have different network performance. For example, mobile networks are generally slower and higher latency than non-mobile networks. Of course, a wide area network (WAN) is much slower than a local area network (LAN). This means that the network used by the mobile local client differs greatly from the performance characteristics of the server-side Web application's LAN. A server-side web app can send a large number of requests to the back-end service without impacting the user experience, but the mobile client can only send a small number of requests.
The partitioning of the service may vary over time, so it needs to be hidden from the client.
Solution Solutions
Implement an API gateway as the only entry for all clients. The API Gateway has two ways of handling requests. Some requests are simply proxied/routed to the appropriate service, and other requests are forwarded to a set of services.
Compared to providing a pervasive API,API gateway, different APIs are opened according to different clients. For example, the Netflix API gateway runs client-specific adapter code that provides the client with the most appropriate API for its needs.
The API gateway can also implement security, such as verifying that a client is authorized to make a request.
Example
Netflix API Gateway
Results
Using API gateways has the following benefits:
Hides to the client how the app is partitioned into microservices
Provide optimal API to each client
The logic of invoking a large number of services is transferred to the API Gateway, thus simplifying the client
Reduced number of requests/round trips. For example, the API enables clients to pull data from multiple services in a single request. Fewer requests, less overhead, and therefore improved user experience. API gateways are essential for mobile applications.
The API Gateway pattern also has some drawbacks:
Increased complexity-the API gateway itself is also a part that needs to be developed, deployed, and managed.
Increased response time because the API gateway is a network hop-but, for the vast majority of applications, the overhead of a single round trip is not obvious.
Problem:
How do I implement an API gateway? The event-driven/reactive (reactive) method is best if it is to scale to handle high loads. On the JVM, NIO-based libraries such as Netty, Spring reactor are also available. Nodejs is another option.
Related patterns
The MicroServices model provides the need for this pattern.
Known users
Change
Undecided
First: Integrated architecture (Monolithic Architecture)
Second: micro-service Architecture (MicroServices architecure)
Third:API gateways (API Gateway)
Translation API gateways (API Gateway)