redis分散ロックの実装
Springbootエンジニアリング.redis哨兵
1.redisテンプレートの構成
2.redis分散ロックの実現
1.redisテンプレートの構成
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(JedisConnectionFactory redisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
return redisTemplate;
}
}
2.redis分散ロックの実現
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class RedisDistributedLock {
@Autowired
private RedisTemplate redisTemplate;
private Logger logger = LoggerFactory.getLogger(RedisDistributedLock.class);
//redis
private static final Long DEFAULT_LOCK_TIMEOUT = 60*1000L;
//redis value
private static final Integer LOCK_KEY_VALUE = 1;
/**
*
* @param lockKey lockKey
* @return true ,false
*/
public boolean lock(String lockKey){
return lock(lockKey,DEFAULT_LOCK_TIMEOUT);
}
/**
*
* @param lockKey lockKey
* @param timeout , ms
* @return
*/
public boolean lock(String lockKey,long timeout){
logger.info("==============》 ");
if(StringUtils.isEmpty(lockKey)){
logger.error(" , ");
return false;
}
Boolean lockResult = redisTemplate.execute((RedisCallback) connection -> {
JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
byte[] value = jdkSerializer.serialize(LOCK_KEY_VALUE);
return connection.setNX(lockKey.getBytes(), value);
});
if(lockResult){
logger.info(" , ");
redisTemplate.expire(lockKey, timeout, TimeUnit.MILLISECONDS);
}
return lockResult;
}
/**
*
* @param lockKey lockKey
*/
public void unLock(String lockKey){
logger.info("==============》 ");
if(StringUtils.isEmpty(lockKey)){
logger.error(" , ");
return ;
}
Integer redisLockKeyValue = (Integer)redisTemplate.opsForValue().get(lockKey);
if(redisLockKeyValue != null && redisLockKeyValue > 0){
redisTemplate.delete(lockKey);
}
}
}