spring实现redis分布式锁
时间: 2024-05-23 19:08:37 浏览: 142
Spring提供了对Redis分布式锁的支持,可以通过Spring的RedisTemplate或LettuceConnectionFactory来实现。下面是一个简单的实现示例:
1. 首先,在Spring配置文件中配置Redis连接池:
```
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
<constructor-arg name="hostName" value="${redis.host}"/>
<constructor-arg name="port" value="${redis.port}"/>
<constructor-arg name="password" value="${redis.password}"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
</bean>
```
2. 然后,定义一个Redis分布式锁的工具类:
```
@Component
public class RedisLockUtil {
private static final long LOCK_EXPIRE_TIME = 30000; // 锁过期时间,单位毫秒
private static final String LOCK_PREFIX = "lock:"; // 锁前缀
@Autowired
private RedisTemplate<String, String> redisTemplate;
public boolean lock(String key) {
String lockKey = LOCK_PREFIX + key;
long now = System.currentTimeMillis();
long expireTime = now + LOCK_EXPIRE_TIME;
Boolean result = redisTemplate.opsForValue().setIfAbsent(lockKey, String.valueOf(expireTime));
if (result != null && result) {
redisTemplate.expire(lockKey, LOCK_EXPIRE_TIME, TimeUnit.MILLISECONDS);
return true;
}
String oldExpireTime = redisTemplate.opsForValue().get(lockKey);
if (oldExpireTime != null && Long.parseLong(oldExpireTime) < now) {
String newExpireTime = redisTemplate.opsForValue().getAndSet(lockKey, String.valueOf(expireTime));
if (newExpireTime != null && newExpireTime.equals(oldExpireTime)) {
redisTemplate.expire(lockKey, LOCK_EXPIRE_TIME, TimeUnit.MILLISECONDS);
return true;
}
}
return false;
}
public void unlock(String key) {
String lockKey = LOCK_PREFIX + key;
redisTemplate.delete(lockKey);
}
}
```
该工具类定义了两个方法:lock和unlock。其中,lock方法实现了Redis分布式锁的获取,unlock方法实现了Redis分布式锁的释放。在lock方法中,首先将锁的过期时间设置为当前时间+30秒,并通过Redis的setIfAbsent方法尝试获取锁;如果获取成功,则返回true;否则,通过get方法获取锁的过期时间,如果锁已经过期,则通过getAndSet方法更新锁的过期时间并获取旧的过期时间,然后判断旧的过期时间是否等于获取到的过期时间,如果相等,则说明获取到了锁,返回true。在unlock方法中,直接通过delete方法删除锁。
3. 最后,在需要使用分布式锁的地方,注入RedisLockUtil即可使用分布式锁:
```
@Autowired
private RedisLockUtil redisLockUtil;
public void doSomethingWithLock(String key) {
if (redisLockUtil.lock(key)) {
try {
// 获取锁成功后执行业务逻辑
// ...
} finally {
redisLockUtil.unlock(key);
}
} else {
// 获取锁失败后的处理
// ...
}
}
```
上面是一个简单的Spring实现Redis分布式锁的示例,如果您有更多的问题,请继续提问。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](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)