In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the advantages and disadvantages of the API gateway". In the daily operation, I believe many people have doubts about the advantages and disadvantages of the API gateway. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what are the advantages and disadvantages of the API gateway?" Next, please follow the editor to study!
Background
In theory, the client can send requests directly to the microservice, and each microservice has an exposed URL that maps to the microservice's load balancer, which is responsible for distributing requests between available instances. But this approach has the following drawbacks:
1. Mismatch between client requirements and fine-grained API exposed by micro-service
There is often a business that invokes many services, and if the client sends many requests, this may be inefficient on the public network and make the client code more complex.
two。 The protocol used by the service is not Web-friendly
Some services may use binary RPC (such as thrift), while others may use the AMQP messaging protocol. Neither protocol is browser-friendly or firewall-friendly and is best used internally. Outside the firewall, applications should use protocols such as HTTP and WebSocket.
3. Difficult to reconstruct
You may want to change the way the system is divided into services over time. For example, merge two services or split a service into two or more services. If the client communicates directly with the microservice, it is difficult to perform this kind of refactoring.
Because of the above problems, it is rarely reasonable for clients to communicate directly with micro-services, and a better way is to use API gateways, with API gateways as the only entrance to the back-end service system. It encapsulates the internal architecture of the system and provides a customized API for each client. It is responsible for service request routing, composition and protocol conversion. Some API gateways have other responsibilities, such as authentication, monitoring, load balancing, and caching.
Overall architecture
A complete service gateway should include three parts: API gateway, gateway console, metric data collection and analysis. The actual shapes are different and can be built on demand, but there must be an API gateway. The functions and responsibilities of the gateway console may be placed in places such as service registration without separate extraction. As for metrics data collection, a general metrics data collection application may be stored in the entire micro-service architecture to monitor all types of applications.
Advantages and disadvantages of API gateway 1. Advantages
Encapsulates the internal structure of the application. The client only needs to interact with the gateway and does not have to invoke a specific service. The API gateway provides a specific API for each type of client, which reduces the number of interactions between the client and the application and simplifies the processing of the client code.
two。 Shortcoming
Added a highly available component that must be developed, deployed, and maintained. Another risk is that the API gateway becomes a development bottleneck. In order to expose each microservice, the developer must update the API gateway. The update process of the API gateway should be as simple as possible, otherwise developers will have to wait in line to update the gateway. However, despite these shortcomings, the use of API gateways makes sense for most real-world applications.
Realized technology
1. Development language
For most applications, the performance and scalability of API gateways are usually very important. Therefore, the API gateway will be built on a platform that supports asynchronism and non-blocking. The Java family can use a NIO-based framework, such as Netty, Vertx, Spring Reactor, as well as Node.js and NGINX Plus.
two。 Responsive programming
The API gateway handles some requests by simply routing the requests to the appropriate back-end services, while other requests are processed by invoking multiple back-end services and merging the results. For requests that do not have dependencies, the API gateway should execute concurrently to minimize response time. Writing API combinatorial code using the traditional asynchronous callback method will fall into callback hell. The code can become messy, difficult to understand, and error-prone. You can use responsive programming to write code in a declarative style. Examples include Future in Scala, CompletableFuture in Java 8, and Promise in JavaScript, and Reactive Extensions (RX), which was originally developed by Microsoft for the .NET platform. Netflix created RxJava for JVM specifically for their API gateway.
3. Process communication model
The application of microservice must be a distributed system, so the communication mechanism between processes must be used. There are two types of interprocess communication mechanisms to choose from. One is to use an asynchronous, message-based mechanism. Some implementations use message agents such as JMS or AMQP, while others (such as Zeromq) have no agents and communicate directly between services. The other is a synchronization mechanism such as HTTP or Thrift. Typically, a system uses both asynchronous and synchronous types. It may even use multiple implementations of the same type. In short, API gateways need to support multiple communication mechanisms.
4. Service discovery
The API gateway needs to know the location (IP address and port) of each microservice it communicates with. The location of the application service is dynamically assigned. Also, a set of instances of a single service changes dynamically as it automatically expands or upgrades. The API gateway needs to use the system's service discovery mechanism, which can be server-side discovery or client-side discovery. If the system uses client discovery, the API gateway must be able to query the service registry, which is a database of all microservice instances and their locations.
Spring cloud provides service registration and discovery functions. If you need to implement it yourself, you can consider using Zookeeper as the registry and Curator on the client.
5. Partial failure
In the implementation of API gateway, there is another problem to deal with, that is, the problem of local failure. This problem occurs in all distributed systems whenever one service invokes another service that is slow or unavailable. API gateways can never be blocked by waiting for downstream services indefinitely. However, how you handle failures depends on the specific scenario and which service fails. If cached data is available, the API gateway can also return cached data. Data can be cached by the API gateway itself or stored in an external cache such as Redis or Memcached. By returning default data or caching data, the API gateway ensures that the system failure does not affect the user's experience.
Netflix Hystrix is an extremely useful library when it comes to writing code to invoke remote services. Hystrix times out calls that exceed the set threshold. It implements a "circuit breaker" mode that prevents clients from waiting unnecessarily for unresponsive services. If the error rate of the service exceeds the set threshold, the Hystrix will cut off the circuit breaker and all requests will fail immediately within a specified time frame. Hystrix allows the user to define a backup operation after a failed request, such as reading data from the cache or returning a default value. If you are using JVM, you should definitely consider using Hystrix. If you are using a non-JVM environment, you should use an equivalent library.
6. Reference implementation scheme
The above lists the points to consider when using DIY as an API gateway, as well as the technical implementation for reference. Here are several popular technical solutions for building API gateways for reference. Examples of these schemes will be given in the following articles.
1) Nginx + Lua implements load balancing, current limiting, service discovery and other functions
2) use spring cloud technology stack, where zuul is used as API gateway
3) Mashape's open source API gateway Kong
7. Gateway console
Provide domain management, application management, service authorization, service monitoring, statistics and measurement data display, view the global view of services and other functions. Both service consumers and service providers register applications in the gateway console, which assigns application id (appId unique) and application key (appSecret) to each application. The information that needs to be provided when registering: application name, application description, information about the person in charge of the application.
At this point, the study on "what are the advantages and disadvantages of API gateway" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.