Original address : Http://microservices.io/patterns/apigateway.html, the following is a translation of the original text using Google Translate.
Let Let’s imagine that you are building an online store using the microservice model, and the product details page you are using. You need to develop multiple versions of the product detail interface:
l HTML-HTML5/JavaScript desktop and mobile browser user interfaces generated by server-side web applications.
l Native Android and iPhone clients-these clients interact through the REST API server.
In addition , The online store must use the product details disclosed by the third-party application REST API. A product detailed information interface can display a lot of information about the product. For example, the detailed information page of Amazon.com includes:
l Basic information about the book, such as title, author, price, etc.
l The purchase history of this book
l Availability
l Purchase options
l Customers who bought this book also bought those
l Customer reviews
l Seller Ranking
l …
Because the online store uses the microservice model, the product detail information data is distributed across multiple services. For example,
L Product information-related product, such as title, basic information of the author
l Pricing Service-Product Price
l Ordering Service-Purchase History Products
l Inventory Service-Product Supply
l Comment service-customer comment…
Therefore, the code to display product details needs to be used in all these services To get information.
< strong>Conundrum
How does the client of microservices access individual services?
< strong>Considerations
l The granularity of the API provided through microservices is often different from what the client needs. Microservices usually provide fine-grained APIs, which means that the client needs to interact with multiple services. For example, as described above, the details of the product required by the client need to obtain data from multiple services.
l Different clients need different data. For example, the desktop browser version of the product details page is usually more complicated than the mobile version.
l Network performance differs for different types of clients. For example, mobile networks are generally much slower and have much higher latency. Of course, any wide area network is much slower than a local area network. This means that the performance characteristics of the network used by the local mobile phone client and the LAN of the server web application are very different. The server-side web application can send a large number of requests to the back-end service without affecting the user experience, but the mobile client can only send a small number of requests.
l The number of service instances and their location (host + port) change dynamically.
l The service may change over time, so the details must be hidden from the client.
< strong>Solution
can implement an API gateway, which is the entrance of all clients. The API Gateway has two ways to process requests. Some requests are simply proxied/routed to the appropriate service, and other requests are forwarded to a set of services.
< img src="/wp-content/uploadshttp:/img.voidcn.com/vcimg/static/loading.png" alt="" style="border:none; max-width:900px" d="6064617" s= "13b_cdf" t="jpg">
The API gateway can provide different APIs for each client, instead of providing an API suitable for all situations. For example, Netflix’s API gateway runs client-specific adapter code and provides each client with the APIs they need.
API The gateway can also implement security, such as: verifying that the client is authorized to execute the request.
< strong> Conclusion
Using APIgateway has the following advantages:
l Decouple the service and the client.
l Decouple the client and service deployment environment.
l Provides the best API to each client.
l Reduced number of requests/roundtrips. For example, the API gateway can retrieve data for multiple services at once. Fewer requests also means less overhead, which improves the user experience. An API gateway is essential for mobile applications.
l Simplify the call of the client, because the API gateway can combine services and provide a combined façade interface.
API The gateway mode also has some disadvantages:
l Increased complexity, API Gateway is another application that must be developed, deployed, and managed.
l Increased response time because of the additional layer of network redirection through the API gateway. However, the cost of the extra round trip for most applications is negligible.
< strong>Problem
How to implement API gateway? The API gateway needs to support high concurrency and high load, and use event response mechanisms (two IO mechanisms: one is blocking, the other is event response, and the latest one is GO language micro-threading). the best method. On the JVM, NIO-based libraries such as Netty can be used, and NodeJS is another option.