诺坎普之旅
2018-09-29 02:08
采纳率: 100%
浏览 4.2k

SpringBoot整合Redis主从哨兵模式,故障切换失败?

目前使用Redis主从复制,添加Sentinel哨兵监听主的状态,如果master断线,自动推荐slave为新master,到这里redis环境一切正常。

使用Jedis进行测试,客户端向master进行写数据,然后人为关闭master,客户端出现连接失败,没有自动进行切换吗?(测试环境是SpringBoot+Redis)

主从和客户端连接池根据哨兵切换是成功的,但是客户端在操作redis时,模拟宕机后,出现无法连接、写数据失败

以下是Redis的配置文件:

 @Bean
public JedisPoolConfig jedisPoolConfig() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxIdle(maxIdle);
    jedisPoolConfig.setMinIdle(minIdle);
    jedisPoolConfig.setMaxTotal(maxTotal);
    jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
    jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    jedisPoolConfig.setTestOnBorrow(testOnBorrow);
    jedisPoolConfig.setTestWhileIdle(testWhileIdle);
    return jedisPoolConfig;
}

@Bean
public RedisSentinelConfiguration sentinelConfiguration(){
    RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
    //配置matser的名称
    RedisNode redisNode = new RedisNode(hostName, port);
    redisNode.setName("mymaster");
    redisSentinelConfiguration.master(redisNode);
    //配置redis的哨兵sentinel
    RedisNode senRedisNode = new RedisNode(sentinelHost,sentinelPort);
    Set<RedisNode> redisNodeSet = new HashSet<RedisNode>();
    redisNodeSet.add(senRedisNode);
    redisSentinelConfiguration.setSentinels(redisNodeSet);
    return redisSentinelConfiguration;
}

@Bean(name="jedisConnectionFactory")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig, RedisSentinelConfiguration sentinelConfig) {   
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfig, jedisPoolConfig);
    jedisConnectionFactory.setPoolConfig(jedisPoolConfig);   
    jedisConnectionFactory.setPort(port);   
    jedisConnectionFactory.setPassword(password);  
    jedisConnectionFactory.setTimeout(timeout);
    return jedisConnectionFactory;
}

2条回答 默认 最新

相关推荐 更多相似问题