编程知识 cdmana.com

Redis realizes distributed

redis Common deployment methods of :

   1. stand-alone   A single point of the problem

   2.mster-slave    Read / write separation , Can't switch

   3. sentry     Sentinel mode Monitoring nodes through sentinels , The master node automatically switches when it is down slave by master

   4.cluster    Based on the slot point 16348 A slot   The Lord and from much , Combination formation

Redis Several aspects of distributed lock guarantee :

1. Mutual exclusivity

2. Don't deadlock

3. Most nodes are OK , The lock cannot be lost .

The implementation scheme of distributed lock :

1. You can go directly through  set key value px milliseconds nx  Command implementation lock , adopt Lua Script to unlock  

      There are risks , The node where the lock is located is down , The lock will be lost .

2.Redission Realization

        Redission stay Redis On the basis of Java In memory data grid . Provides a series of distributed Java Common objects , Reentrant lock , Read-write lock , Fair lock , Red lock , It also provides a lot of distributed services .Redission Provides the use of Redis The simplest and most convenient way to .

1. structure redisson It is necessary to implement distributed lock Config
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:5379").setPassword("123456").setDatabase(0);
// 2. structure RedissonClient
RedissonClient redissonClient = Redisson.create(config);
// 3. Get lock object instance ( There is no guarantee that you will get... In the order of threads )
RLock rLock = redissonClient.getLock(lockKey);
try {
    /**
     * 4. Attempt to acquire lock 
     * waitTimeout  The maximum waiting time to try to acquire the lock , Beyond that , It is considered that the acquisition of lock failed 
     * leaseTime    Lock holding time . 
     */
    boolean res = rLock.tryLock((long)waitTimeout, (long)leaseTime, TimeUnit.SECONDS);
    if (res) {
        // Successful lock acquisition , Deal with business here 
    }
} catch (Exception e) {
    throw new RuntimeException("aquire lock fail");
}finally{
    // in any case ,  Finally, we need to unlock 
    rLock.unlock();
}

    If the lock is acquired successfully, a scheduled task will be opened , That is to say watchdog, Regular tasks will be checked and renewed regularly renewExpirationAsync(threadId), Avoid business execution time being greater than lock timeout time , The business is not completed and the lock has been released .

 RedissionLock Also, the problem of node failure has not been solved , There is a problem of losing locks .RedissonRedLock It really solves the problem of single point failure , The price is extra for RedissonRedLock build Redis Environmental Science .

3.RedissonRedLock Algorithm

      Suppose a redis cluster Yes 5 individual master node .

      1. Get the current timestamp , In milliseconds

      2. And above set In a similar way , At every master Nodes create locks , The expiration time is short , Usually tens of milliseconds .

      3. Try to get a lock on most nodes , For example, a total of five nodes need three .

      4. The client calculates the time to acquire the lock , If the time taken by the client to acquire the lock is less than the timeout , Even if you succeed .

      5. If the lock acquisition fails , Then delete the previous lock in turn .

      6. A distributed lock is created by the main person , Then you have to keep polling for locks .

版权声明
本文为[ZH-JSON]所创,转载请带上原文链接,感谢

Scroll to Top