- 新建Springboot项目,在pom文件加入以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 配置application.yml
spring:
redis:
host: 127.0.0.1
port: 6379
database: 0
password:
timeout: 10000
jedis:
pool:
max-active: 50
min-idle: 20
- 具体代码实现RedisLock.java
package com.xx.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.Arrays;
@Component
public class RedisLock {
private static final Logger logger = LoggerFactory.getLogger(RedisLock.class);
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private DefaultRedisScript<Long> redisScript;
private static final Long RELEASE_SUCCESS = 1L;
public boolean lock(String key, String value){
if (redisTemplate.opsForValue().setIfAbsent(key, value)){
logger.info("[redis分布式锁]获取成功");
return true;
}
String currentValue = redisTemplate.opsForValue().get(key);
if (!StringUtils.isEmpty(currentValue) && Long.parseLong(currentValue) < System.currentTimeMillis()){
String oldValue = redisTemplate.opsForValue().getAndSet(key, value);
if (!StringUtils.isEmpty(oldValue) && oldValue.equals(currentValue)){
logger.info("[redis分布式锁]获取成功");
return true;
}
}
logger.info("[redis分布式锁]获取失败");
return false;
}
public void unlock(String key, String value){
try {
String currentValue = redisTemplate.opsForValue().get(key);
if (!StringUtils.isEmpty(currentValue) && currentValue.equals(value)){
redisTemplate.opsForValue().getOperations().delete(key);
logger.info("[redis分布式锁]解锁成功");
}
} catch (Exception e) {
logger.error("[redis分布式锁]解锁异常,{}", e);
}
}
public boolean unlockWithLua(String key, String value){
Long result = redisTemplate.execute(redisScript, Arrays.asList(key,value));
return RELEASE_SUCCESS.equals(result);
}
@Bean
public DefaultRedisScript<Long> defaultRedisScript() {
DefaultRedisScript<Long> defaultRedisScript = new DefaultRedisScript<>();
defaultRedisScript.setResultType(Long.class);
defaultRedisScript.setScriptText("if redis.call('get', KEYS[1]) == KEYS[2] then return redis.call('del', KEYS[1]) else return 0 end");
return defaultRedisScript;
}
}