springboot1.5.9采用lettuce方式连接redis集群,redis为集群模式,无法自动刷新拓扑结构而导致当master宕机主从切换期间Lettuce连接Redis报错的问题。
springboot1.5.9集成的spring-data-redis的版本是1.8.9,我没有找到配置自动刷新的接口;网上的文章基本都是springboot2.0以上的,对应的spring-data-redis版本也是2.0以上的,有配置的自动刷新的接口;所以请各位帮忙分析分析,springboot1.5.9采用lettuce的方式如何配置redis集群模式下拓扑自动刷新的问题!
这是我的配置代码
public class RedisConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisConnectionFactory connectionFactory = null;
if (redisProperties.getClusterMode().equals("true")) {
//jedis
//connectionFactory = new JedisConnectionFactory(redisClusterConfiguration(), jedisPoolConfig());
//lettuce
connectionFactory = new LettuceConnectionFactory(redisClusterConfiguration());
((LettuceConnectionFactory) connectionFactory).setTimeout(redisProperties.getTimeout());
if(StringUtils.isNotEmpty(redisProperties.getPassword().trim())){
((LettuceConnectionFactory) connectionFactory).setPassword(redisProperties.getPassword());
}
} else {
connectionFactory = redisStandaloneConfiguration();
((JedisConnectionFactory) connectionFactory).setTimeout(redisProperties.getTimeout());
if(StringUtils.isNotEmpty(redisProperties.getPassword().trim())) {
((JedisConnectionFactory) connectionFactory).setPassword(redisProperties.getPassword());
}
}
return connectionFactory;
}
private JedisConnectionFactory redisStandaloneConfiguration() {
JedisConnectionFactory redisStandaloneConfiguration = new JedisConnectionFactory();
redisStandaloneConfiguration.setHostName(redisProperties.getHosts());
redisStandaloneConfiguration.setPort(redisProperties.getPort());
redisStandaloneConfiguration.setPoolConfig(jedisPoolConfig());
redisStandaloneConfiguration.setUsePool(true);
return redisStandaloneConfiguration;
}
private RedisClusterConfiguration redisClusterConfiguration() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(
Arrays.asList(StringUtils.split(redisProperties.getHosts(), ",")));
redisClusterConfiguration.setMaxRedirects(redisProperties.getMaxRedirects());
return redisClusterConfiguration;
}
private JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
jedisPoolConfig.setMaxTotal(redisProperties.getMaxTotal());
jedisPoolConfig.setTestOnBorrow("true".equals(redisProperties.getTestOnBorrow()));
return jedisPoolConfig;
}
@Bean
public RedisTemplate<?, ?> getRedisTemplate() {
@SuppressWarnings("rawtypes")
RedisTemplate<?, ?> redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory());
// string序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// 使用Jackson2JsonRedisSerialize 替换默认序列化
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);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
我自己看了spring-data-redis 1.8的源码,没有找到配置的地方;请各位大拿帮忙分析分析,springboot1.5.9采用lettuce的方式如何配置redis集群模式下拓扑自动刷新的问题!