Java连接ElastiCache Redis集群报JedisNoReachableClusterNodeException问题排查
解决JedisCluster连接AWS ElastiCache Redis时的No reachable node异常
看起来你的问题根源在于没有给JedisCluster配置SSL连接,而你的AWS ElastiCache集群已经启用了传输中加密(in-transit encryption)。你单节点连接用了rediss://协议(带SSL)所以能成功,但集群连接代码默认用普通的非SSL连接,导致无法和集群节点建立有效连接,最终抛出JedisNoReachableClusterNodeException。
解决方案:修改JedisCluster初始化代码,启用SSL
在Jedis 2.9.0版本中,JedisCluster提供了支持SSL的重载构造方法,你需要在初始化时传入ssl=true的参数,同时补充必要的超时、重试配置。修改后的完整代码如下:
public static void main(String[] args) throws IOException{ final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxWaitMillis(2000); poolConfig.setMaxTotal(300); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("mycachecluster.eaogs8.0001.usw2.cache.amazonaws.com",6379)); jedisClusterNode.add(new HostAndPort("mycachecluster.eaogs8.0002.usw2.cache.amazonaws.com",6379)); // 配置SSL相关参数 int connectionTimeout = 5000; // 连接超时时间(毫秒) int soTimeout = 5000; // 读取超时时间(毫秒) int maxAttempts = 5; // 集群操作重试次数 String password = null; // 如果集群设置了访问密码,替换为实际密码 // 初始化JedisCluster时启用SSL JedisCluster jedisCluster = new JedisCluster( jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, password, poolConfig, true // 关键:启用SSL连接 ); System.out.println("Cluster Size...." + jedisCluster.getClusterNodes().size()); try{ jedisCluster.set("foo", "bar"); String value = jedisCluster.get("foo"); System.out.println("Got value: " + value); } catch(Exception e){ e.printStackTrace(); } finally{ jedisCluster.close(); } }
关键修改说明
- 启用SSL参数:最后一个参数
true告诉JedisCluster使用SSL协议连接,这是匹配你ElastiCache传输加密配置的核心设置; - 超时与重试配置:新增
connectionTimeout和soTimeout避免网络延迟导致的超时,maxAttempts提升集群操作的容错性; - 密码配置:如果你的ElastiCache集群设置了访问密码,务必将
password变量替换为实际密码值。
补充说明
你之前用telnet能连通节点,是因为telnet仅验证端口可达性,但ElastiCache启用加密后,只有SSL连接才能执行正常的Redis命令交互,普通非SSL连接会被拒绝,这也是集群连接失败但telnet测试成功的原因。
内容的提问来源于stack exchange,提问作者Unknown
相关产品推荐
相关产品推荐

