spring-data-redis lettuce cluster scan 问题

假如想象 提交于 2021-01-22 21:07:31

如果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*"));
				  }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!