Original address: http://microservices.io/patterns/apigateway.html, the following is the use of Google translation of the original translation.
Let's imagine that you are building an online store that uses the MicroServices model that you use for the Product Details page. You need to develop multiple versions of the product detail interface:
L Html-html5/javascript Desktop and mobile browser user interface generated by server-side Web applications.
Native Android and iphone clients-these clients interact through the REST API server.
In addition, online stores must use product details that are exposed by third-party app rest APIs. A product's detailed information interface can display a lot of information about the product. For example, the Amazon.com details page includes:
L Basic information about this book, such as title, author, price, etc.
L This book purchase history
L Availability
L Purchase Options
L The customers who bought the book also buy those
L Customer Reviews
L Sellers Ranking
L ...
Because the online store uses the MicroServices model, product detail data is distributed across multiple services. For example
L Product Information-related products, such as title, author basic Information
L Pricing Services-product price
L Order Service-Purchase history products
L Stock Service-product supply
L Comment Service-customer reviews ...
Therefore, the code that displays the details of the product needs to obtain information in all of these services.
Problem
How do microservices clients access individual services?
Factors to consider
The granularity of APIs provided through MicroServices is often different from the needs of the client. MicroServices typically provide fine-grained APIs, which means that clients need to interact with multiple services. For example, as mentioned above, the client needs the details of the product needed to obtain data from multiple services.
L different clients require different data. For example, the Product Details page Desktop browser version is usually more complex than the mobile version.
L network performance is different for different types of clients. For example, mobile networks are often much slower and have much higher latencies. Of course, any WAN is much slower than a 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 number of service instances and their location (host + port) are dynamically changed.
The service may change over time, so hide the details from the client.
Solution Solutions
Can implement an API gateway, which is the portal 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.
The API gateway can provide different APIs for each client, rather than providing an API that is appropriate for all scenarios. For example, Netflix's API gateway runs client-specific adapter code, which provides each client with the APIs they need.
API gateways can also implement security, such as verifying that a client is authorized to execute a request.
Conclusion
Use API?? Gateways have the following advantages:
L DECOUPLE the service from the client.
L DECOUPLE the client and service deployment environment.
L provides the best API to each client.
L reduced number of requests/round trips. For example, an API gateway can retrieve data for multiple services at once. Fewer requests also mean less overhead and a better user experience. An API gateway is critical for mobile applications.
L simplifies client invocation because API gateways can combine services and provide a combined façade interface.
The API Gateway pattern also has some drawbacks:
L increased complexity, API gateways are another application that must be developed, deployed, and managed.
L Increased response time because of a layer of network jumps through the API gateway. However, the cost of extra round trips for most applications is negligible.
Problem
How do I implement an API gateway? API gateways need to support high concurrency, high load, using event response mechanisms (IO Two mechanisms: one is blocking, the other is event-responsive, and the latest is the go-language micro-threading approach) is the best approach. On the JVM, it is possible to base NIO-based libraries such as Netty, and Nodejs is another option.
Translation-MicroServices API Gateway