API Gateway

  • ...

Since the online store uses the microservices pattern, the product detail data is spread out through the service. Such as:

  • Product Info Service - basic product information such as title, author

  • Pricing Service - commodity prices

  • Order service - item purchase history

  • Inventory service - is the item in stock

  • Review service - User reviews...

Therefore, the code that displays the store listing needs to obtain information from all of these services.

 

question

How do clients of microservice-based applications access these independent services?

 

driving force

  • The API granularity provided by microservices often differs from the needs of the client. Microservices generally provide fine-grained APIs, which means that clients need to interact with multiple services. For example, as mentioned above, clients that need product details need to pull data from a large number of services.

  • Different clients require different data. For example, the desktop version of a product detail page is often more detailed than the mobile version.

  • Different types of clients have different network performance. For example, mobile networks are generally slower and have 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 performance characteristics of the network used by the local client of the mobile phone 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.

  • The division of services may change over time and therefore needs to be hidden from clients.

 

solution

Implement an API Gateway as the only entry point for all clients. API Gateway has two ways to handle requests. Some requests are simply proxied/routed to the appropriate service, others are forwarded to a set of services.

Compared with providing universal APIs, API gateways open different APIs according to different clients. For example, the Netflix API Gateway runs client-specific adapter code that provides clients with the API that best suits their needs.

API gateways can also implement security, such as verifying that the client is authorized to make a request.

 

Example

Netflix API Gateway

 

result

Using an API Gateway has the following benefits:

  • Hides from the client how the application is divided into microservices

  • Provide optimal API to each client

  • Simplifies the client side by moving the logic of calling a large number of services to the API gateway

  • Reduced number of requests/roundtrips. For example, APIs allow clients to pull data from multiple services in a single request. With fewer requests, there is less overhead, thus improving the user experience. API gateway is very necessary for mobile applications.

The API Gateway pattern also has some disadvantages:

  • Added complexity - the API gateway itself is a part that needs to be developed, deployed and managed.

  • The response time is increased due to the additional network hop of the API Gateway - however, for most applications, the overhead of one more round trip is not noticeable.

question:

  • How to implement API Gateway? If you want to scale to handle high loads, an event-driven/reactive approach is best. On the JVM, NIO based libraries like Netty, Spring Reactor are also available. NodeJS is another option.

http://my.oschina.net/douxingxiang/blog/358173

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326741290&siteId=291194637