Distributed Lock
Why the need for distributed lock it at a single point in time synchronized can be solved, but split after service, each service is a separate machine, can not be resolved, so there has been distributed lock, in fact, it is to use a variety of means achieve acquire exclusive lock, others can not get.
In fact, the premise of doing distributed lock, we need to understand, synchronized why you can not use, and what principles so that he can be used on a machine.
synchronized principles
As we all know Synchronize
the keyword is used to solve the problem of concurrent solutions, the following three ways:
- Synchronous static method locks the current
Class
object. - Sync blocks, the lock
{}
of the object. - Synchronization common method is to lock the current object.
The principle:
JVM
through the entry, exit, an object monitor ( Monitor
) method to achieve synchronization sync block.
Specific implementation is in the preamble to join a method called after compiling monitor.enter
instructions, insert the exit method and exception monitor.exit
instructions.
Its essence is the object of a monitor ( Monitor
) were acquired, and the process of obtaining exclusive so as to achieve the purpose of the same time only one thread access.
For not a lock to get the thread will block the entrance to the methods, until a thread to acquire the lock monitor.exit
before you can continue to try to acquire the lock.
By using javap -c Synchronize
specific information can be viewed after the compilation, here I will not paste.
So you can know that is a separate Java virtual machine can achieve lock, but not more than one hand stretched, and only rely on external forms to generate a unique lock.
These are informational someone else's blog to get personally try to get accurate: link: https: //www.jianshu.com/p/2ba154f275ea
Redis achieve distributed lock
The main principle is the use of the redis set NX, as well as for atomic script script redis use. (Personal view, in fact, one step back to see how their program logic to decide in the end to do this step)
Simply put, what the main processes:
- First, set up a globally unique key and a uniqueness value (value is an unlocked security, before deleting determine what value are the same)
- Redis method using a set of configuration parameters in the form of a multi-key, value, nx, px, expiration time (parameter NX: only key does not exist, in order to set it up operation)
- Redis use the script to script the deletion of the key can only delete your own value (to determine whether the value is before you delete it before me value, whether to turn over, it is not deleted)
- @pointcut cut class notes
- In use around the @around do before and after treatment
- Front lock
- Unlock del later use script script
Code mainly realize
Notes category DistributedLock
@Retention (RetentionPolicy.RUNTIME) @Target (ElementType.METHOD) @Documented public @ interface DistributedLock { / ** * Add your custom form of external configuration properties of distributed locks * For example: key rules, lock timeout, wait time to acquire the lock, and a series of configuration attributes * * / }
Cut class DistributedLockAspect
@Aspect @Component public class DistributedLockAspect { / ** * layer tangent point * / @Pointcut ( "@annotation (com.creditease.hardess.common.annotation.DistributedLock)" ) public void distributedLockAspect () {} / ** * @ param Joinpoint tangent point * @return Object the distributed lock added, the actual business logic code to call the return value * @throws all exceptions generated Throwable, in order to avoid interference to the exception handling process should continue all exceptions thrown * / @Around ( "distributedLockAspect ()" ) public Object doAround (ProceedingJoinPoint Joinpoint) throws{Throwable / ** * Main write lock acquisition, and service logic execution, deletion lock * * / return returnObject; } / ** * Get DistributedLock annotation * * @param Joinpoint * @return annotations are defined * @throws a NoSuchMethodException * / Private static DistributedLock getDistributedLock (ProceedingJoinPoint Joinpoint) throws a NoSuchMethodException { String methodName = joinPoint.getSignature () getName ();. Class classTarget = <?> joinPoint.getTarget().getClass(); Class<?>[] par = ((MethodSignature) joinPoint.getSignature()).getParameterTypes(); Method objMethod = classTarget.getMethod(methodName, par); return objMethod.getAnnotation(DistributedLock.class); } } }