头图

Introduction to the use of distributed lock MLock

distributed lock MLock gitee address
Distributed lock MLock github address

Links to series of articles:

1. Based on

Use java+redis to implement commonly used distributed locks, providing annotations and code in two forms of use, which is convenient and simple

2. Features

  1. Support distributed lock
  2. Support distributed check lock
  3. Support for distributed idempotence
  4. Support annotation usage and template usage
  5. Support automatic renewal

3. Get started quickly

3.1 Dependency introduction
<dependencies>
    <!-- 开发测试 -->
    <dependency>
        <groupId>moon</groupId>
        <artifactId>mlock</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>

    <!-- 正式版本 -->
    <dependency>
        <groupId>moon</groupId>
        <artifactId>mlock</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>
3.2 Use
  1. Distributed lock waitMills (the waiting lock time defaults to 0, that is, only one attempt to acquire the lock) Note: Lock needs to be idempotent in the code, and distributed idempotent locks do not require annotation usage
@Service
public class TestService {
    @Lock(domain = "lockTest", keys = {"#pojo.id"}, lockType = LockTypeEnum.LOCK_REDIS, waitTime = 60000)
    public void lockTest(Pojo pojo) {
        // todo 
    }
}
  1. Template usage
@Service
public class TestService {
    public void lockTemplateTest(Pojo pojo) {
        LockTemplate<Boolean> lockTemplate = new LockTemplate<>();
        String lockKey = String.valueOf(pojo.getId());
        Boolean lockResult = lockTemplate.execute(
                LockTypeEnum.LOCK_REDIS,
                "lockTest",
                lockKey,
                60000,
                TimeUnit.SECONDS,
                new ILockCallback<Boolean>() {
                    @Override
                    public Boolean success() throws LockException {
                        // 这里放需要被加锁的代码
                        return null;
                    }

                    @Override
                    public Boolean fail() throws LockException {
                        return null;
                    }

                    @Override
                    public Boolean ex(Exception e) throws LockException {
                        return null;
                    }
                }
        );
    }
}
  1. Distributed check lock checks the existence of the lock, does not lock, if the lock is already locked, skip execution or throw a specified exception
@Service
public class TestService {
    @CheckLock(domain = "checkLockTest", keys = {"#pojo.id"}, lockType = LockTypeEnum.LOCK_REDIS_FORCE)
    public void checkLockTest(Pojo pojo) {
        // todo 
    }
}
  1. Distributed idempotence checks whether the specified key has been operated. If it has been operated, skip execution or throw a specified exception. The default idempotence is reserved for 10 minutes by default, which is suitable for avoiding repeated clicks, payments, creations, MQ repeated consumption, etc.
@Service
public class TestService {
    @Idempotent(domain = "IdempotentTest", keys = {"#token"})
    public void IdempotentTest(String token) {
        // todo
    }
}

莫小点还有救
219 声望26 粉丝

优秀是一种习惯!