Distributed Lock ---- Redis achieve

   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 Synchronizethe keyword is used to solve the problem of concurrent solutions, the following three ways:

  • Synchronous static method locks the current Classobject.
  • Sync blocks, the lock {}of the object.
  •  Synchronization common method is to lock the current object.

  The principle:
  JVMthrough 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.enterinstructions, insert the exit method and exception monitor.exitinstructions.

  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.exitbefore 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:

  1. First, set up a globally unique key and a uniqueness value (value is an unlocked security, before deleting determine what value are the same)
  2. 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)
  3. 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)
   This is the main content of the script  if redis.call ( "get", KEYS   [1]) == ARGV [1] then return redis.call ( "del", KEYS [1]) else return 0 end
   Its interpretation: get get to the key value to determine whether the pass key value equal to the value to you, then perform the del key equal to 0 otherwise according to the end
   In this regard the recommendation to take a look redis Lua script to a simple: http://redisdoc.com/script/eval.html
  
  In the actual scene can take advantage of cut-point cut in the form of AOP, specifically what needs to realize distributed lock, with a distributed lock, and then with annotations ways to achieve the desired custom settings where there needs to point
  AOP is the main achievement
  1. @pointcut cut class notes
  2. In use around the @around do before and after treatment
  3. Front lock
  4. 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);
    }

}




}

 

Guess you like

Origin www.cnblogs.com/zhouguanglin/p/11246239.html