RedisLock -- make Redis distributed lock simple
1. Project introduction
This project mainly simplifies the operation of redis distributed transaction, realizes the operation of adding lock and releasing lock, and realizes the operation of waiting for the release of lock elegantly. The process of waiting for the lock to be released mainly uses the listening function of redis, so at present, to ensure that redis has enabled key event listening, that is, "Ex".
- How to check whether redis has enabled monitoring?
After logging in to redis, use the command config get notify keyspace events to view
github address: https://github.com/chimmhuang/redislock
Code cloud address: https://gitee.com/chimmhuang/redislock
Welcome to Start, Fork~
2. Fast use
2.1 introducing maven coordinates
<dependency> <groupId>com.github.chimmhuang</groupId> <artifactId>redislock</artifactId> <version>1.0.2</version> </dependency>
2.2 register RedisLock
- Mode 1 (recommended): add the path of package scanning on the startup class of the project
@ComponentScan(basePackages = "com.github.chimmhuang.redislock") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
- Mode 2: manually register Related bean s
@Configuration public class RedisConfig { @Bean public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } @Bean public RedisListener redisListener(RedisMessageListenerContainer redisMessageListenerContainer) { return new RedisListener(redisMessageListenerContainer); } @Bean public RedisLock redisLock(RedisTemplate redisTemplate) { return new RedisLock(redisTemplate); } }
2.3 use
- Inject redisLock
- use redisLock.lock(key,expire) to lock
- use redisLock.unlock(key) Unlock
Here is a unit test case (train station ticket sales case)
@RunWith(SpringRunner.class) @SpringBootTest public class RedisListenerTest { @Autowired private RedisLock redisLock; /** 100 Tickets */ private static Integer count = 100; @Test public void ticketTest() throws Exception { TicketRunnable tr = new TicketRunnable(); // Four threads correspond to four windows Thread t1 = new Thread(tr,"window A"); Thread t2 = new Thread(tr,"window B"); Thread t3 = new Thread(tr,"window C"); Thread t4 = new Thread(tr,"window D"); t1.start(); t2.start(); t3.start(); t4.start(); Thread.currentThread().join(); } public class TicketRunnable implements Runnable { @Override public void run() { while (count > 0) { redisLock.lock("ticketLock", 3L); if (count > 0) { System.out.println(Thread.currentThread().getName() + "No" + (count--) + "Train tickets"); } redisLock.unlock("ticketLock"); try { Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } } } } }
3. Participation and contribution
Welcome to join us! Mention an Issue Or submit a Pull Request.
At present, it only realizes the simple process of locking and unlocking, and there are other operations to be improved and tested, such as:
-[] in the redis cluster environment, you need to listen to every redis key event
-[] in the active standby mode of redis, there may be a problem that the data (key) is not synchronized in the past during the active standby redis handover
4. Contact the author
QQ(Wechat) : 905369866
Email : chimmhuang@163.com
5. Open source protocol
MIT © Chimm Huang