栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Jedis如何写入Redis集群中的特定插槽/节点

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Jedis如何写入Redis集群中的特定插槽/节点

解决方案1:
找到一种解决方案来标识要插入钥匙的插槽。JedisCluster提供了一些API。

int slotNum = JedisClusterCRC16.getSlot(key);
-提供钥匙的插槽号。

Set<HostAndPort> redisClusterNode = new HashSet<HostAndPort>();redisClusterNode.add(new HostAndPort(hostItem, port));JedisSlotbasedConnectionHandler connHandler = new    JedisSlotbasedConnectionHandler(redisClusterNode, poolConfig, 60);Jedis jedis = connHandler.getConnectionFromSlot(slotNum);

这为群集中的特定节点提供了jedis对象(内部来自Jedispool)。
现在,使用上述jedis对象,可以轻松地为特定节点(在集群中)流水线化所有命令

Pipeline pipeline = jedis.pipelined();pipeline.multi();for(Entry<String, Map<String, String>> kvf : kvfs.entrySet()) {   pipeline.hmset(kvf.getKey(), kvf.getValue());}pipeline.exec();

尽管使用JedisCluster(使用JedisCluster)提供了适当的节点,但密钥不能到达预期的性能,但我认为这是由于知道插槽编号和节点的过程所致。
每当我们尝试获取包含插槽号的实际节点(jedis)时,上述过程似乎都建立了到节点(群集中)的物理连接。因此,如果我们拥有数百万个键,这会阻碍性能。
因此,使用Lettuce软件包的另一种方法(如下)帮助我克服了这一点。


解决方案2:
使用的Lettuce软件包支持在群集模式下发送批量命令。

     <groupId>biz.paluch.redis</groupId>     <artifactId>lettuce</artifactId>     <version>4.4.3.Final</version>

程式码片段:

RedisClusterClient client = RedisClusterClient.create(RedisURI.create("hostname", "port"));StatefulRedisClusterConnection<String, String> connection = client.connect();RedisAdvancedClusterAsyncCommands<String, String> commands = connection.async();// Disabling auto-flushingcommands.setAutoFlushCommands(false);List<RedisFuture<?>> futures = new ArrayList<>();// kvf is of type Map<String, Map<String, String>>for (Entry<> e : kvf.entrySet()){   futures.add(commands.hmset( (String) e.getKey(), (Map<String, String>) e.getValue()));}// write all commands to the transport layercommands.flushCommands();// synchronization example: Wait until all futures completeLettuceFutures.awaitAll(10, TimeUnit.SECONDS,futures.toArray(new RedisFuture[futures.size()]));

参考:https : //github.com/lettuce-io/lettuce-core/wiki/Pipelining-and-
command-flushing




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/388214.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号