spring-redis实现的Redis分布式锁的例子
时间: 2024-06-13 07:07:03 浏览: 15
以下是使用Spring Redis实现Redis分布式锁的例子:
首先,在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
```
然后,创建一个Redis分布式锁的接口:
```java
public interface RedisLock {
/**
* 获取锁
*
* @param key 锁的key
* @param expireTime 锁的过期时间
* @return 是否获取成功
*/
boolean lock(String key, long expireTime);
/**
* 释放锁
*
* @param key 锁的key
*/
void unlock(String key);
}
```
接着,实现Redis分布式锁的接口:
```java
@Component
public class RedisLockImpl implements RedisLock {
private final RedisTemplate<String, String> redisTemplate;
private final ThreadLocal<Map<String, Integer>> lockers = new ThreadLocal<>();
@Autowired
public RedisLockImpl(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public boolean lock(String key, long expireTime) {
Map<String, Integer> localLockers = getLocalLockers();
Integer count = localLockers.get(key);
if (count != null) {
localLockers.put(key, count + 1);
return true;
}
Boolean result = redisTemplate.opsForValue().setIfAbsent(key, "");
if (!result) {
return false;
}
redisTemplate.expire(key, expireTime, TimeUnit.MILLISECONDS);
localLockers.put(key, 1);
return true;
}
@Override
public void unlock(String key) {
Map<String, Integer> localLockers = getLocalLockers();
Integer count = localLockers.get(key);
if (count == null) {
return;
}
if (count > 1) {
localLockers.put(key, count - 1);
} else {
localLockers.remove(key);
redisTemplate.delete(key);
}
}
private Map<String, Integer> getLocalLockers() {
Map<String, Integer> localLockers = lockers.get();
if (localLockers != null) {
return localLockers;
}
lockers.set(new HashMap<>());
return lockers.get();
}
}
```
最后,在需要使用分布式锁的地方,注入RedisLock接口,使用lock()方法获取锁,使用unlock()方法释放锁:
```java
@Service
public class UserService {
private final RedisLock redisLock;
@Autowired
public UserService(RedisLock redisLock) {
this.redisLock = redisLock;
}
public void updateUser(String userId) {
String lockKey = "user_" + userId;
boolean locked = redisLock.lock(lockKey, 5000);
if (!locked) {
throw new RuntimeException("获取锁失败");
}
try {
// 更新用户信息
} finally {
redisLock.unlock(lockKey);
}
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)