k8s部署redis集群,节点重启pod ip变化,java代码不能自动获取到新的pod ip,导致redis集群不可用。
技术栈:spring cloud
目前是通过JedisCluster获取到redis集群的节点,再配置到redis集群。
try (JedisCluster jedisCluster = new JedisCluster(ipAndPortSet, 10000, 10000, RedisConstants.MAX_RE_DIRECTIONS, password, new JedisPoolConfig())) {
Map<String, JedisPool> jedisPoolMap = jedisCluster.getClusterNodes();
for (String hostAndPort : jedisPoolMap.keySet()) {
String[] hostAndPortArray = hostAndPort.split(":");
RedisNode redisNode = new RedisNode(hostAndPortArray[0], Integer.parseInt(hostAndPortArray[1]));
redisNodes.add(redisNode);
}
}
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
redisClusterConfiguration.setClusterNodes(redisNodes);
redisClusterConfiguration.setPassword(password);
redisClusterConfiguration.setMaxRedirects(RedisConstants.MAX_RE_DIRECTIONS);
return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);