java
package io.renren.common.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
/**
* Redis配置
**/
@Configuration
public class RedisConfig {
@Resource
private RedisConnectionFactory factory;
// 锁前缀
private static final String SCHEMA_PREFIX = "redis://";
@Bean
public Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(){
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
return jackson2JsonRedisSerializer;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
/**
* 创建 RedissonClient,注入IOC容器
*/
@Bean
public RedissonClient redissonClient(RedisProperties redisProperties) {
Config config = new Config();
//单点redis
SingleServerConfig singleServerConfig = config.useSingleServer().
setAddress(SCHEMA_PREFIX + redisProperties.getHost() + ":" + redisProperties.getPort());
if (StringUtils.hasText(redisProperties.getPassword())) {
singleServerConfig.setPassword(redisProperties.getPassword());
}
singleServerConfig.setTimeout((int) redisProperties.getTimeout().toMillis());
singleServerConfig.setPingConnectionInterval(30000);
singleServerConfig.setDatabase(redisProperties.getDatabase());
// 超时时间
long lockWatchTimeOut = 3000;
config.setLockWatchdogTimeout(lockWatchTimeOut);
return Redisson.create(config);
}
java
@Component
public class RedissonLock {
@Autowired
private RedissonClient redissonClient;
private static final String LOCK_PREFIX = "lock:";
/**
* 阻塞方式获取锁
* @param key
* @param expireTime
* @return
*/
public RLock lock(String key,Integer expireTime){
//可重入锁
RLock lock = redissonClient.getLock(LOCK_PREFIX + key);
//lock.lock(expireTime, TimeUnit.SECONDS); //阻塞方式获取锁,设置过期时间
lock.lock();
return lock;
}
/**
* 非阻塞方式获取锁
* @param key
*/
public Boolean tryLock(String key){
try {
RLock lock = redissonClient.getLock(LOCK_PREFIX + key);
return lock.tryLock(5L, TimeUnit.SECONDS); //非阻塞方式获取锁,设置在指定时间内失败重试获取锁
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}
/**
* 非阻塞方式获取锁
* @param key key
* @param expireTime 指定持有时间
*/
public Boolean tryLock(String key,Integer expireTime){
try {
RLock lock = redissonClient.getLock(LOCK_PREFIX + key);
//非阻塞方式获取锁,设置在指定时间内失败重试获取锁
return lock.tryLock(5L,expireTime,TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}
/**
* 释放锁
* @param key
*/
public void unlock(String key) {
RLock lock = redissonClient.getLock(LOCK_PREFIX+key);
if (lock.isLocked()) {
lock.unlock();
}
}
}