苍老的岁月
2021-09-27 22:39
采纳率: 66.7%
浏览 45

k8s部署Redis集群,节点重启pod IP变化,Java代码不能自动连接到新的pod IP,如何自动刷新?

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);
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 苍老的岁月 2021-09-28 19:02
    已采纳

    使用这种方式配置k8s容器板redis 集群。

    针对集群节点发生变化,使用lettuce自适应刷新

            GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
            genericObjectPoolConfig.setMaxTotal(redisPropertiesConfig.getPoolMaxTotal());
            genericObjectPoolConfig.setMaxIdle(redisPropertiesConfig.getPoolMaxIdle());
            genericObjectPoolConfig.setMinIdle(redisPropertiesConfig.getPoolMinIdle());
    
            //支持自适应集群拓扑试图刷新
            ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
                    //.enablePeriodicRefresh(Duration.ofSeconds(5))
                    .enableAllAdaptiveRefreshTriggers()
                    .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30))
                    //.enablePeriodicRefresh(Duration.ofSeconds(10))
                    .build();
    
            ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()
                    .timeoutOptions(TimeoutOptions.enabled(Duration.ofSeconds(connTimeout)))
                    .autoReconnect(true)
                    //.pingBeforeActivateConnection(Boolean.TRUE)
                    //.cancelCommandsOnReconnectFailure(Boolean.TRUE)
                    //.disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
                    .topologyRefreshOptions(clusterTopologyRefreshOptions).build();
    
            LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder()
                    .poolConfig(genericObjectPoolConfig)
                    //.readFrom(ReadFrom.NEAREST)
                    .clientOptions(clusterClientOptions).build();
    
            RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(serversArray));
            redisClusterConfiguration.setPassword(password);
            redisClusterConfiguration.setMaxRedirects(RedisConstants.MAX_RE_DIRECTIONS);
            lettuceConnectionFactory = new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
            return lettuceConnectionFactory;
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题