Distributed lock is not a previous lock, but the general name of all kinds of locks. Read write lock, lock, semaphore, etc.
Import the native core dependency of redisson. Redisson, like the previous jedis # letters, is a client:
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.16.8</version> </dependency>
The previous redistemplate operation, list hash string, etc. are all done by the current process, but redisson puts these objects on redis and redis operates them.
to configure:
package com.atguigu.gulimall.product.config; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyRedisConfig { /** * All redis operations are performed through redisonclient objects * * @return */ @Bean(destroyMethod = "shutdown") public RedissonClient redisson() { //Create configuration Config config = new Config(); // This means using single node mode instead of cluster mode redis pattern. config.useSingleServer().setAddress("redis://" + "192.168.155.3:6379"); //according to config objects creating RedissonClient example return Redisson.create(config); } }
===========================================
@Controller public class IndexController { @Autowired private CategoryService categoryService; @Autowired private RedissonClient redissonClient; @ResponseBody @RequestMapping("/index/hello") public String hello() { RLock lock = redissonClient.getLock("my-lock"); // Lock. This is a blocking wait. I've been waiting here without adding the lock. lock.lock(); try { System.out.println("Lock successfully, execute the business, and the thread of the current lock ID: " + Thread.currentThread().getId()); Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } finally { System.out.println("Release the lock, the thread of the current lock ID: " + Thread.currentThread().getId()); lock.unlock();//Unlock } return "hello"; }
The key in redis also contains the thread ID {95:
After 30s, the business is executed and the lock is released for printing:
===================================
Suppose that the unlocking code is not executed, the program is broken when executing the business, the service is down, and the unlocking code is not executed. Will redisson deadlock?
The answer is no deadlock.
It can be found in redis that this lock has a TTL value.
redisson's strengths:
- Automatic renewal of locks. If the business takes a long time, it will be automatically renewed. There is no need to worry that the lock will be automatically deleted. It is not specified when adding a lock. The default time is 30s, and the renewal is also 30s
- When the business operation of locking is completed, it will not be renewed. Even if it is not unlocked manually, it will be unlocked automatically within 30s.
=================================================
When you lock, if you specify a time, it will not be automatically renewed.
*If we pass the lock timeout, we will send a timeout script to redis to occupy the lock. The default timeout is what we specify
*If we don't specify it, use 30 * 1000 [LockWatchdogTimeout] watchdog default time
*As long as the lock is occupied successfully, a scheduled task will be started. The task is to reset the expiration time of the lock. This time is still the time of [LockWatchdogTimeout]. 1 / 3 of the time of the watchdog will be renewed once to the full time.
Regular tasks are renewed every ten seconds for 30 seconds
If the business has not been completed in 30s, it is a business problem. The specified expiration time is often used in actual combat.