SpringBoot+custom annotation+Redis realizes distributed lock

image-20200708160944615

The verification code has been placed in git: https://github.com/janycode/eelock.git

1. Redis distributed lock theory

Redis has a series of commands, which end with NX. NX is the abbreviation of Not eXists. For example, the SETNX command should be understood as: SET if Not eXists.
Set successfully, return 1. If the setting fails, return 0.
Since Redis is in single-process and single-thread mode, it adopts queue mode to change concurrent access into serial access. Commands are executed one by one, so setNx can be used to implement distributed locks.
Before the method is executed, redis is requested to perform the setnx command. If it returns 1,

Guess you like

Origin blog.csdn.net/sinat_36184075/article/details/113839141