1.application.properties
spring.redis.cluster.nodes= ***:*, ***:*, ***:*
spring.redis.cluster.password=*
spring.redis.cluster.command-timeout=10000
spring.redis.cluster.max-attempts=2
spring.redis.cluster.max-redirects=3
spring.redis.cluster.max-active=16
spring.redis.cluster.max-wait=3000
spring.redis.cluster.max-idle=8
spring.redis.cluster.min-idle=0
spring.redis.cluster.test-on-borrow=true
2.RedisConfig
@Configuration
@ConditionalOnClass(JedisCluster.class)
public class RedisConfig {
Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class);
@Resource
private RedisProperties redisProperties;
/**
* 配置 Redis 连接池信息
*/
@Bean
public JedisPoolConfig getJedisPoolConfig() {
JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
jedisPoolConfig.setTestOnBorrow(redisProperties.isTestOnBorrow());
return jedisPoolConfig;
}
/*
*返回单例JedisCluster
*/
@Bean
public JedisCluster getJedisCluster(){
String[] serverArray = redisProperties.getNodes().split(",");
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
for(String ipPort: serverArray){
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(),Integer.valueOf(ipPortPair[1].trim())));
}
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
genericObjectPoolConfig.setMaxTotal(redisProperties.getMaxActive());
genericObjectPoolConfig.setMinIdle(redisProperties.getMinIdle());
genericObjectPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
// genericObjectPoolConfig.setTestOnBorrow(redisProperties.isTestOnBorrow());
return new JedisCluster(nodes,redisProperties.getCommandTimeout(),redisProperties.getMaxWait(),redisProperties.getMaxAttempts(),redisProperties.getPassword(),genericObjectPoolConfig);
}
/**
* 设置数据存入redis 的序列化方式
* redisTemplate序列化默认使用的jdkSerializeable
* 存储二进制字节码,导致key会出现乱码,所以自定义序列化类
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
logger.info("redis cluster集群连接成功");
return redisTemplate;
}
}
3.RedisProperties
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class RedisProperties {
private String nodes;
private String password;
private Integer commandTimeout;
private Integer maxAttempts;
private Integer maxRedirects;
private Integer maxActive;
private Integer maxWait;
private Integer maxIdle;
private Integer minIdle;
private boolean testOnBorrow;
public String getNodes() {
return nodes;
}
public void setNodes(String nodes) {
this.nodes = nodes;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getCommandTimeout() {
return commandTimeout;
}
public void setCommandTimeout(Integer commandTimeout) {
this.commandTimeout = commandTimeout;
}
public Integer getMaxAttempts() {
return maxAttempts;
}
public void setMaxAttempts(Integer maxAttempts) {
this.maxAttempts = maxAttempts;
}
public Integer getMaxRedirects() {
return maxRedirects;
}
public void setMaxRedirects(Integer maxRedirects) {
this.maxRedirects = maxRedirects;
}
public Integer getMaxActive() {
return maxActive;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public Integer getMaxWait() {
return maxWait;
}
public void setMaxWait(Integer maxWait) {
this.maxWait = maxWait;
}
public Integer getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(Integer maxIdle) {
this.maxIdle = maxIdle;
}
public Integer getMinIdle() {
return minIdle;
}
public void setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
@Override
public String toString() {
return "RedisProperties{" +
"nodes='" + nodes + '\'' +
", password='" + password + '\'' +
", commandTimeout=" + commandTimeout +
", maxAttempts=" + maxAttempts +
", maxRedirects=" + maxRedirects +
", maxActive=" + maxActive +
", maxWait=" + maxWait +
", maxIdle=" + maxIdle +
", minIdle=" + minIdle +
", testOnBorrow=" + testOnBorrow +
'}';
}
}
4.RedisUtil
@Component
public class RedisUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
@Autowired
private JedisCluster jedisCluster;
/**
* 设置缓存
* @param key 缓存key
* @param value 缓存value
*/
public void set(String key, String value) {
jedisCluster.set(key, value);
LOGGER.debug("RedisUtil:set cache key={},value={}", key, value);
}
/**
* 设置缓存对象
* @param key 缓存key
* @param obj 缓存value
*/
public <T> void setObject(String key, T obj , int expireTime) {
jedisCluster.setex(key, expireTime, JSON.toJSONString(obj));
}
/**
* 获取指定key的缓存
* @param key---JSON.parseObject(value, User.class);
*/
public String getObject(String key) {
return jedisCluster.get(key);
}
/**
* 判断当前key值 是否存在
*
* @param key
*/
public boolean hasKey(String key) {
return jedisCluster.exists(key);
}
/**
* 设置缓存,并且自己指定过期时间
* @param key
* @param value
* @param expireTime 过期时间
*/
public void setWithExpireTime( String key, String value, int expireTime) {
jedisCluster.setex(key, expireTime, value);
LOGGER.debug("RedisUtil:setWithExpireTime cache key={},value={},expireTime={}", key, value, expireTime);
}
/**
* 获取指定key的缓存
* @param key
*/
public String get(String key) {
String value = jedisCluster.get(key);
LOGGER.debug("RedisUtil:get cache key={},value={}",key, value);
return value;
}
/**
* 删除指定key的缓存
* @param key
*/
public void delete(String key) {
jedisCluster.del(key);
LOGGER.debug("RedisUtil:delete cache key={}", key);
}
}