Integrating Redisson with SpringBoot
I. Introducing Dependence
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.10.6</version> </dependency>
2. Adding configuration to application.properties
#spring.redis.database= spring.redis.host=localhost spring.redis.port=6379 #spring.redis.password= #spring.redis.ssl= #spring.redis.timeout= #spring.redis.cluster.nodes= #spring.redis.sentinel.master= #spring.redis.sentinel.nodes=
3. Re-entrainable locks
Redis-based distributed reentrant lock RLock implements java.util.concurrent.locks.Lock interface
RLock lock = redissonClient.getLock("ReentrantLock"); lock.lock();
If the Edis node responsible for storing the distributed lock goes down and the lock is in the locked state, the lock will be locked. To avoid this, Redisson internally provides a watchdog to monitor locks. Its function is to continuously extend the validity of locks before the Redisson instance is closed. By default, the watchdog's time-out for checking locks is 30 seconds, or it can be specified separately by modifying Config. lockWatchdog Timeout
The lock method provides the leaseTime parameter to specify the lock time beyond which the lock is automatically unlocked.
//Automatic unlocking after 10 seconds of locking lock.lock(10, TimeUnit.SECONDS);
//Attempt to lock, wait for 2 seconds at most, unlock automatically after 10 seconds, get the lock to return true successfully, fail to return false boolean result = lock.tryLock(2, 10, TimeUnit.SECONDS);
//Unlock lock.unlock();
Redisson provides methods for asynchronous execution of distributed locks:
RLock lock = redissonClient.getLock("AsyncLock"); lock.lockAsync(); lock.lockAsync(10, TimeUnit.SECONDS); lock.tryLockAsync(2,10,TimeUnit.SECONDS);
IV. Fair Lock
Distributed reentrant fair locks are also RLock objects that implement the java.util.concurrent.locks.Lock interface. It ensures that when multiple Redisson client threads simultaneously request locking, priority is given to the threads that make the request first.
RLock fairLock = redissonClient.getFairLock("FairLock"); fairLock.lock();
Five, interlocking
Distributed interlocking RedissonMultiLock objects can associate multiple RLock objects into one interlock, and each RLock object instance can come from a different Redisson instance.
RLock lock1 = redissonClient.getLock("lock1"); RLock lock2 = redissonClient.getLock("lock2"); RLock lock3 = redissonClient.getLock("lock3"); RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3); //At the same time lock lock1, lock 2, lock 3, all locks are successfully locked. lock.lock();
Re-entrant lock implementation:
Six, red lock
The Red Lock RedissonRedLock object implements the lock algorithm introduced by Redlock. This object can also be used to associate multiple RLock objects into a red lock, and each RLock object instance can come from a different Redisson instance.
RLock lock1 = redissonClient.getLock("lock1"); RLock lock2 = redissonClient.getLock("lock2"); RLock lock3 = null; RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3); //lock1, lock2, lock3 are locked at the same time. If the red lock is successfully locked on most nodes, it will be successful. lock.lock();
7. Read-write locks
The RReadWriteLock object realizes the java.util.concurrent.locks.ReadWriteLock interface. It also supports automatic expiration unlocking. This object allows multiple read locks at the same time, but only one write lock at most.
RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("ReadWriteLock"); // Read lock readWriteLock.readLock().lock(); // Write lock // readWriteLock.writeLock().lock();
8. Signals
Distributed semaphore RSemaphore objects use interfaces and usages similar to java.util.concurrent.Semaphore
RSemaphore semaphore = redissonClient.getSemaphore("Semaphore"); //The semaphore is 6. semaphore.acquire(6);
//Release semaphores in other threads or other JVM s RSemaphore semaphore = redissonClient.getSemaphore("Semaphore"); semaphore.release(1);
//Release semaphores in other threads or other JVM s RSemaphore semaphore = redissonClient.getSemaphore("Semaphore"); semaphore.release(5);
9. Expirable semaphores
The expirable semaphore RPermitExpirable Semaphore adds an expiration time to each signal based on the RSemaphore object. Each signal can be identified by a separate ID, which can only be released by submitting the ID.
RPermitExpirableSemaphore semaphore = redissonClient.getPermitExpirableSemaphore("mySemaphore"); String permitId = semaphore.acquire(); // Get a signal, valid for only 2 seconds. String permitId = semaphore.acquire(2, TimeUnit.SECONDS);
semaphore.release(permitId);
Ten, locking
Distributed latching RCountDownLatch objects use interfaces and usages similar to java.util.concurrent.CountDownLatch
RCountDownLatch latch = redissonClient.getCountDownLatch("CountDownLatch"); latch.trySetCount(1); latch.await();
//In other threads or other JVM s RCountDownLatch latch = redissonClient.getCountDownLatch("CountDownLatch"); latch.countDown();
Reference resources: https://github.com/redisson/redisson/wiki/8. - Distributed Lock and Synchronizer