如果redis 集群中某一台master down ,并且由一台slave 节点升级为master ,在使用spring-data-redis 执行scan 的时候还是会继续去连接down 的节点。导致异常。看了下是lettuce 的问题。因为原生的lettuce 使用也有问题。解决方案,就是过滤掉down 的节点。
Partitions partitions = connection.getPartitions();
System.out.println(partitions);
RedisAdvancedClusterCommands<String, String> sync = connection.sync();
for (RedisClusterNode partition : partitions) {
if (partition.isConnected() && partition.is(RedisClusterNode.NodeFlag.MASTER)) {
RedisClusterCommands<String, String> syncConnection = sync.getConnection(partition.getNodeId());
KeyScanCursor<String> scan = syncConnection.scan(match);
Set<String> result = new HashSet<>(scan.getKeys());
while (!scan.isFinished()){
ScanCursor of = KeyScanCursor.of(scan.getCursor());
scan = syncConnection.scan(of,match);
result.addAll(scan.getKeys());
}
System.out.println(partition.getNodeId()+"\t"+ result.size());
}
}
Spring 的解决方法就是
LettuceConnectionFactory lettuceConnectionFactory=(LettuceConnectionFactory) stringRedisTemplate.getConnectionFactory();
StatefulRedisClusterConnection<String,String> connection = (StatefulRedisClusterConnection<String,String>)lettuceConnectionFactory.getConnection();
Partitions partitions = connection.getPartitions();
for (RedisClusterNode partition : partitions) {
if (partition.isConnected() && partition.is(RedisClusterNode.NodeFlag.MASTER)){
connection.getConnection(partition.getNodeId()).sync().scan(ScanArgs.Builder.limit(100000).match("xxx*"));
}
}
来源:oschina
链接:https://my.oschina.net/u/1266813/blog/4920676