Joke Collection Website - Talk about mood - "Spring Cloud" (39) uses distributed locks to control repeated requests of microservices.

"Spring Cloud" (39) uses distributed locks to control repeated requests of microservices.

Usually, we can solve the problem of repeated submission of requests in a short time through the front-end anti-shake and throttling. If network problems, Nginx retry mechanism, micro-service pretend retry mechanism or users intentionally bypass the front-end anti-shake and throttling settings and directly make frequent requests, the system's anti-duplication request will fail, and even lead to multiple duplicate records in the background. At this time, we need to consider adding anti-duplication settings in the background.

Considering the distributed scene of micro-service, this paper adopts Redisson distributed lock+custom comment +AOP to realize the function of preventing repeated requests in the background. The basic realization idea is: add a custom anti-copy note on the interface that needs anti-copy, set anti-copy parameters, intercept the request parameters through AOP, generate the key of distributed lock according to the note configuration, and set the effective time. Every time you request access, you try to acquire the lock. If you get it, you will carry it out. If not, it means that the request is within the set repeated request interval, and the prompt message "Do not request frequently" will be returned.

Related references:

1. Anti-duplication configuration items and corresponding parameters obtained through SpringExpression:/p/p/77895a82237

2.Redisson distributed locks and related tools: /wsh _ Quiet/Article/Details/115326052.

GitEgg: GitEgg is an open source and free enterprise-level micro-service application development framework, which aims to integrate the current mainstream and stable open source technology framework, integrate the best common project solutions, and realize the rapid development framework of micro-services that can be directly used.

GitHub-WMZ1930/GitEgg: GitEgg is an open source and free enterprise-level micro-service application development framework, which aims to integrate the current mainstream and stable open source technology framework, the commonly used best project solutions, and realize the rapid development framework of micro-services that can be directly used.