请问老师,批量操作中,使用key获取对应节点的jedis的具体方法是?
来源:10-20 批量操作优化

mongo_m
2018-09-26
在jedis的cluster中,JedisClusterCRC16.getSlot(key)可以通过key获取到对应的slot值,但是接下来如何通过slot值获取对应node的connection呢?
我粗略看了jedis的源码,没有找到可以用的方法。
Better one is reuse current JedisCluster:
- Get nodeMap <String,JedisPool> where key is host.
- Take any host from it for getting information about slot
distribution.- Store slot distribution as TreeMap
- Get slot by key, get host by slot, get JedisPool by host, get Jedis
from JedisPoolprotected void initJedisNodeMap() { try { nodeMap = jedisCluster.getClusterNodes(); String anyHost = nodeMap.keySet().iterator().next(); slotHostMap = getSlotHostMap(anyHost); }catch (JedisClusterException e){ logger.error(e.getMessage()); } } protected Jedis getJedis(String key) { int slot = JedisClusterCRC16.getSlot(key); Map.Entry<Long, String> entry = slotHostMap.lowerEntry(Long.valueOf(slot)); return nodeMap.get(entry.getValue()).getResource(); } public static TreeMap<Long, String> getSlotHostMap(String anyHostAndPortStr) { TreeMap<Long, String> tree = new TreeMap<>(); String parts[] = anyHostAndPortStr.split(":"); HostAndPort anyHostAndPort = new HostAndPort(parts[0], Integer.parseInt(parts[1])); try (Jedis jedis = new Jedis(anyHostAndPort.getHost(), anyHostAndPort.getPort())) { List<Object> list = jedis.clusterSlots(); for (Object object : list) { List<Object> list1 = (List<Object>) object; List<Object> master = (List<Object>) list1.get(2); String hostAndPort = new String((byte[]) master.get(0)) + ":" + master.get(1); tree.put((Long) list1.get(0), hostAndPort); tree.put((Long) list1.get(1), hostAndPort); } } return tree; }
但是这个方法还需要在renewClusterCache的时候同时也更新SlotHostMap缓存中的内容。
另外发现:在maven的仓库中,Jedis的最后版本是2016年。然而RedisTemplate(spring-boot-starter-data-redis.jar中)是从2016年开始发行,直到最近都是活跃的。
是不是我不应该再研究Jedis,而应该在生产环境中选择使用RedisTemplate呢?
RedisTemplate一如spring的风格代码比较臃肿,但是本质上他的效率比起Jedis有哪些优势呢?
写回答
1回答
-
carlosfu
2018-10-28
具体可以参考:https://github.com/sohutv/cachecloud/blob/master/cachecloud-open-client/cachecloud-jedis/src/main/java/redis/clients/jedis/PipelineCluster.java
00
相似问题