Simple use of redis distributed lock

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:
Code cloud address:
Welcome to Start, Fork~

2. Fast use

2.1 introducing maven coordinates


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")
public class Application {

    public static void main(String[] args) {, args);
  • Mode 2: manually register Related bean s
public class RedisConfig {

    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        return container;

    public RedisListener redisListener(RedisMessageListenerContainer redisMessageListenerContainer) {
        return new RedisListener(redisMessageListenerContainer);

    public RedisLock redisLock(RedisTemplate redisTemplate) {
        return new RedisLock(redisTemplate);

2.3 use

  1. Inject redisLock
  2. use redisLock.lock(key,expire) to lock
  3. use redisLock.unlock(key) Unlock

Here is a unit test case (train station ticket sales case)

public class RedisListenerTest {

    private RedisLock redisLock;

    /** 100 Tickets */
    private static Integer count = 100;

    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");




    public class TicketRunnable implements Runnable {
        public void run() {
            while (count > 0) {
                redisLock.lock("ticketLock", 3L);
                if (count > 0) {
                    System.out.println(Thread.currentThread().getName() + "No" + (count--) + "Train tickets");

                try {
                } catch (Exception e) {

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 :

5. Open source protocol

MIT © Chimm Huang

Keywords: Java Redis github Maven Windows

Added by work_it_work on Sat, 06 Jun 2020 05:57:21 +0300