【RedisCluster】redis-cluster mget測試


測試用例:

@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(不是很理解這個)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM