A list,

Traffic limiting of the Soul gateway adopts redis token bucket algorithm to implement traffic limiting at the interface level.

Redis is implemented in three ways:

  • Redis-based setnx operations: Setnx instruction, at the time of the operation of the CAS, at the same time to the specified key is set the expiration time, we are in the main purpose of the current limit is to in unit time, the one and only N number of requests to be able to access my program, relying on setnx can easily do this aspect of the function, such as we need to limit in 10 seconds 20 request, So we can set the expiration time of setNx to 10, when the number of setNx requests reaches 20, the effect of limiting traffic will be achieved;

  • Redis based data structure zSET: requests are made into a Zset array, value is unique when each request comes in, and score can be represented by the current timestamp, because score can be used to calculate the number of requests within the current timestamp. The zset data structure also provides the range method so that we can easily get the number of requests in the two timestamps;

  • Token bucket algorithm based on Redis: every time a request is accessed, a token can be retrieved from Redis. If a token is retrieved, it indicates that the limit is not exceeded, and if not, it indicates that the limit is exceeded.

The Soul gateway adopts redis token bucket algorithm to limit traffic, and the processing flow is as follows:

2. Setup and use of plug-in

1. Start the Redis

The Soul gateway uses the Redis token bucket algorithm to limit traffic, so you need to start a Redis service first

2. Soul-admin configuration Set it to on in soul-admin – > Plug-in Management – > rate_limiter. In the plug-in, configure Redis. Redis currently supports standalone, Sentinel, and cluster modes. If it is sentinel, cluster, etc. multi-node, in the URL configuration, please use for each column; Segmentation, such as 192.168.1.1:6379; 192.168.1.2 instead: 6379. If the user does not need to use it, disable the plugin in the admin background.

  • Create a new selector in the plugin list

Rate: Is how many requests per second you allow the user to make and discard any requests. This is the filling rate of the token bucket;

Capacity: The maximum number of requests that a user can execute in one second. This is the number of tokens the token bucket can hold;

Add rateLimiter support to the gateway’s pop.xml file as follows:

<! -- soul ratelimiter plugin start--> <dependency> <groupId>org.dromara</groupId> <artifactId>soul-spring-boot-starter-plugin-ratelimiter</artifactId> <version>${last.version}</version> </dependency> <! -- soul ratelimiter plugin end-->Copy the code

Third, summary

One day you learned how to use the rateLimiter plug-in, and the next day you analyze the principles and code behind it.