测试用例:
@Test void redisMgetTest(){ List<String> list=new ArrayList(); list.add("lcc"); list.add("ccl"); list.add("clc"); List<Object> res=redisTemplate.opsForValue().multiGet(list); for (Object o:res){ System.out.println(o); } }
执行结果:
还是能跑的,调试看一下是怎么玩的吧
先判断是否在同一个slot,如果是的话估计就和standalone一样,如果不是交给
org.springframework.data.redis.connection.ClusterCommandExecutor#executeMultiKeyCommand:
/** * Run {@link MultiKeyClusterCommandCallback} with on a curated set of nodes serving one or more keys. * * @param cmd must not be {@literal null}. * @return never {@literal null}. * @throws ClusterCommandExecutionFailureException */ public <S, T> MultiNodeResult<T> executeMultiKeyCommand(MultiKeyClusterCommandCallback<S, T> cmd, Iterable<byte[]> keys) { Map<RedisClusterNode, PositionalKeys> nodeKeyMap = new HashMap<>(); int index = 0;
//聚合每个node下面所有的key for (byte[] key : keys) { for (RedisClusterNode node : getClusterTopology().getKeyServingNodes(key)) { nodeKeyMap.computeIfAbsent(node, val -> PositionalKeys.empty()).append(PositionalKey.of(key, index++)); } }
//用并行的方式请求各个redis-server Map<NodeExecution, Future<NodeResult<T>>> futures = new LinkedHashMap<>(); for (Entry<RedisClusterNode, PositionalKeys> entry : nodeKeyMap.entrySet()) { if (entry.getKey().isMaster()) { for (PositionalKey key : entry.getValue()) { futures.put(new NodeExecution(entry.getKey(), key), executor.submit(() -> executeMultiKeyCommandOnSingleNode(cmd, entry.getKey(), key.getBytes()))); } } } return collectResults(futures); }
可以看到这里的实现方式是我们上篇文章提到的并行I/O请求的方式,而不是hash_tag(不是很理解这个)