The spike projects - through a token payment mechanism, control and queue spillway gates spike

Spike project for the treatment of high concurrency, we take a token payment mechanism, a token is generated based on the user's token, item id, id and a string of events commodity uuid stored in redis

While the introduction of spike gates, the purpose of flow control, such as the current activities of goods only 100, we paid 500 tokens issued before the spike will first token, token complete payment later put the user block at this level outside the control of the flow

Token will compare the user redis produced after obtaining a token, comparative success before they can buy goods

 

First, it is necessary to set the number of stocks gates, gates when we get set for the inventory of active listings 5 ​​times

 

 

According to the number of gates necessary to determine whether or not issued at the time of issuance of the token

 

 

 

Such an action would reduce the pressure on the enormous traffic

 

But nevertheless, we can imagine that if a site did spike activity, there is a commodity spike, each with 1000, it will have issued 50,000 tokens, so the number is still large, so we have taken to solve the flood queue this problem

 

What is queued flood it, like a lot of people at the same time into a door, the door is very small, a lot of people, can be lined up, if one can feed 20 people, 20 people on a wave, waves feed

Similarly, the queue of the spillway is to be divided into batches of users access to a huge amount to reduce the pressure caused by simultaneous access, generally divided into token bucket algorithm and the leaky bucket algorithm, we generally use the token bucket algorithm, then how to achieve it ?

We use single guava limiter is limiting, restricting the flow of a single interface

Cluster limiting: redis dependent or other middleware technologies unify counter, they tend to create a performance bottleneck

Stand-alone limiting: load balancing premise, limiting the effect of the single is better

 

Guide package:

<dependency> 
<the groupId> com.google.guava </ the groupId>
<the artifactId> Guava </ the artifactId>
<Version> 18.0 </ Version>
</ dependency>

added at class to use:

private RateLimiter orderCreateRateLimiter;
 
@PostConstruct
public void init() {
orderCreateRateLimiter = RateLimiter.create(180);//设置每秒处理300个请求
}

在下单之前校验

if(!orderCreateRateLimiter.tryAcquire()){

            throw new BusinessException(EmBusinessError.RATELIMIT);

}

 


  

Guess you like

Origin www.cnblogs.com/blogofbin/p/11704158.html