Introduction to the use of distributed lock MLock
distributed lock MLock gitee address
Distributed lock MLock github address
Links to series of articles:
- 16169896fc0da8 Distributed Lock (1)
- distributed lock (2)-based on Redis implementation
- Distributed lock (3)-Redisson implementation
- distributed lock (4)-based on Mysql implementation of
- distributed lock (5)-MLock introduction (implemented by yourself, based on redis, suitable for real projects)
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
- Support distributed lock
- Support distributed check lock
- Support for distributed idempotence
- Support annotation usage and template usage
- 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
- 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
}
}
- 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;
}
}
);
}
}
- 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
}
}
- 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
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。