Distributed system API gateway principle and selection

原文转载于:https://www.cnblogs.com/ttaall/p/13712855.html

What is a gateway?

A bridge for communication between two independent LANs/or it can be understood that all external requests will be sent to the gateway, and the gateway handles request distribution routing, etc., hiding various API interfaces of internal services

Gateway role and function

1. Dynamic routing Routing to the corresponding service according to the request, if the service is not available, there will be a retry mechanism

2. Load balancing Multiple servers provide the same service, the gateway will pull the registration information of each service from the registration center, and then load balance the request

3. Flow control to limit the flow to avoid impact on the internal system

4. Unified authentication The gateway verifies the relevant permissions (the requester's access permissions, and can also realize SSO single sign-on)

5. Fuse downgrade When the service is unavailable or the traffic is too large, the gateway can downgrade the request to another server or do other processing, prompting the user to be temporarily unavailable

6. Grayscale release only upgrades a small number of servers. A small number of services are sent to the upgraded server through the gateway to test the upgraded server. A large number of requests are still sent to the old version server

7. Log service Service access report, request throughput, concurrent number, daily alarm

Common gateway framework

Gateway corresponding to Zuul StringCloud  

Kong Nginx + Lua Lua application running on Nginx

Gateway involves principle

Protocol conversion: Each system uses different protocols (http, grpc, dubbo protocol) to coordinate and unify through the gateway

Chain processing: The gateway processes requests similar to the chain of responsibility mode and processes requests through various filters

 

Asynchronous request: The number of threads in the gateway is limited. Each thread takes a request to access the service and waits for the service access result to return. This process is a waste of time and cannot be completed under high concurrency  

The asynchronous request worker thread can process the next request without waiting for the service to return the result. The Reactor thread of the gateway is responsible for select polling the Socket access result. Once the result is found to be returned, the worker thread is notified to process the returned result.

The NMS implements dynamic routing:

Dynamic routing (it is not necessary to reconfigure the gateway to restart every time the service address changes)

principle:

A table can be built in the gateway database to store configuration information corresponding to different services. Then configure a front-end entry, and modify the configuration information dynamically on the page each time to complete the database modification.

The backend gateway route reads this table to complete the configuration

Current limiting concept

Three elements of high concurrency architecture: cache + degradation + current limiting

Types of throttling:  

1. Concurrent flow limit, such as limit 100 requests per second, limit and request frequency within a certain time range

2. Request frequency limit, such as a request from a certain ip/a certain user, limit the request to no more than 5 times per second

3. Upload and download speed, such as limiting a user’s download speed to a maximum of 100KB/S, which is more commonly used in download scenarios. For example, some resource download sites will limit the download speed of ordinary users, and only purchasing members can increase the speed. The method is actually similar to request frequency limiting, except that one limits the amount of requests, and the other limits the size of request data packets.

Handling after throttling: how to respond to throttling requests

1. Abandon, directly throw an exception or directly jump to a friendly page

2. Queuing, use the message queue to play the role of peak elimination and current limiting

3. Downgrade, directly perform downgrade processing. For example, the product details page only returns the most basic product data (basic data) that is easy to find, and complex data is returned directly without query. 

Current limiting method:

1. Access layer current limiting, such as Nginx, the entrance of the gateway implements request routing + current limiting. If it is a gateway cluster, it also needs to use distributed middleware to share information such as the number of requests

2. Pooling technology current limiting, such as using Hystrix's thread pool, thread pool + waiting queue to achieve current limiting

Current limiting algorithm:

1. Fixed window algorithm,

2. Sliding window algorithm,

3. Leaky bucket algorithm,

4. Token Bucket Algorithm

 

Guess you like

Origin blog.csdn.net/qq_28581269/article/details/121664834