spring-data-redis中JedisCluster不支持pipelined问题解决

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 引言了解Jedis的童鞋可能清楚,Jedis中JedisCluster是不支持pipeline操作的,如果使用了redis集群,在spring-boot-starter-data-redis中又正好用到的pipeline,那么会接收到Pipeline is currently not supported for JedisClusterConnection.这样的报错。

引言

了解Jedis的童鞋可能清楚,Jedis中JedisCluster是不支持pipeline操作的,如果使用了redis集群,在spring-boot-starter-data-redis中又正好用到的pipeline,那么会接收到Pipeline is currently not supported for JedisClusterConnection.这样的报错。错误来自于org.springframework.data.redis.connection.jedis.JedisClusterConnection

    /*
     * (non-Javadoc)
     * @see org.springframework.data.redis.connection.RedisConnection#openPipeline()
     */
    @Override
    public void openPipeline() {
        throw new UnsupportedOperationException("Pipeline is currently not supported for JedisClusterConnection.");
    }

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration会帮我们自动配置,无论你redis使用的是standalone、sentinel、cluster配置。这个源码很容易理解,读者可自行阅读,不理解的可以一起讨论。

Lettuce中的pipeline

spring boot 2.0开始,配置spring-boot-starter-data-redis将不依赖Jedis,而是依赖Lettuce,在Lettuce中,redis cluster使用pipeline不会有问题。

知识储备

再往下看可能需要读者具备如下的能力:

  1. redis cluster hash slot
  2. JedisCluster & Jedis的关系
  3. pipeline和*mset等命令的区别

哈希槽(hash slot)

redis cluster一共有16384个桶(hash slot),用来装数据,建立集群的时候每个集群节点会负责一些slot的数据存储,比如我负责0-1000,你负责1001-2000,他负责2001-3000……
数据存储时,每个key在存入redis cluster前,会利用CRC16计算出一个值,这个值就是对应redis cluster的hash slot,就知道这个key会被放到哪个服务器上了。

参考文档:

Redis 集群教程
Redis 集群规范

JedisCluster & Jedis的关系

JedisCluster本质上是使用Jedis来和redis集群进行打交道的,具体过程是:

  1. 获取该key的slot值:JedisClusterCRC16.getSlot(key)
  2. JedisClusterConnectionHandler实例中获取到该slot对应的Jedis实例:Jedis connection = connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
  3. 利用connection操作。

pipeline和*mset等命令的区别

redis提供了mset,hmset之类的命令,或者说集合操作可以使用sadd key 1 2 3 4 5 6 ..... 10000000000这种一口气传一堆数据的命令。
有时候你甚至会发现*mset这种一口气操作一堆数据的速度更快。那么这种使用场景会有什么弊端呢?答案是:阻塞。
操作这一堆数据需要多久,就会阻塞多久。

Redis Cluster下pipeline使用的思考

由于JedisCluster中的所有操作本质上是使用Jedis,而Jedis是支持pipeline操作的,所有,要在redis cluster中使用pipeline是有可能的,只要你操作同一个键即可,准确的说,应该是你操作的键位于同一台服务器,更直白的,你操作的键是同一个Jedis实例。ok,如果你已经晕了,那你需要回看一下“知识储备”。
说说笔者的使用场景吧,我们是把csv文件的一批数据读到内存中,同一批数据是存储到同一个key中的,最后的操作会类似于:

set key member1
set key member2
set key member3
...
set key member100000

操作的是同一个key,可以利用JedisCluster获取到该key的Jedis实例,然后利用pipeline操作。

让spring-data-redis也支持pipeline的思路

提供一下代码思路。

RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
RedisConnection redisConnection = factory.getConnection();
JedisClusterConnection jedisClusterConnection = (JedisClusterConnection) redisConnection;
// 获取到原始到JedisCluster连接
JedisCluster jedisCluster = jedisClusterConnection.getNativeConnection();
// 通过key获取到具体的Jedis实例
// 计算hash slot,根据特定的slot可以获取到特定的Jedis实例
int slot = JedisClusterCRC16.getSlot(key);
/**
 *  不建议这么使用,官方在2.10版本已经修复<a href="https://github.com/xetorthio/jedis/pull/1532">此问题</a><br>
 *  2.10版本中,官方会直接提供JedisCluster#getConnectionFromSlot
 */
Field field = ReflectionUtils.findField(BinaryJedisCluster.class, null, JedisClusterConnectionHandler.class);
field.setAccessible(true);
JedisSlotBasedConnectionHandler jedisClusterConnectionHandler = (JedisSlotBasedConnectionHandler) field.get(jedisCluster);
Jedis jedis = jedisClusterConnectionHandler.getConnectionFromSlot(slot);
// 接下来就是pipeline操作了
Pipeline pipeline = jedis.pipelined();
...
pipeline.syncAndReturnAll();

以上代码完全可以模仿spring-data-redis中RedisTemplate#executePipelined方法写成一个通用的方法,供使用者调用。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
5月前
|
存储 缓存 NoSQL
redis集群+JedisCluster+lua脚本实现分布式锁
redis集群+JedisCluster+lua脚本实现分布式锁
119 0
|
11月前
|
NoSQL 算法 Java
Redis进阶-JedisCluster初始化 & 自动管理连接池中的连接 _ 源码分析
Redis进阶-JedisCluster初始化 & 自动管理连接池中的连接 _ 源码分析
456 0
|
NoSQL Java Redis
Redis集群:使用Spring和jedisCluster操作Redis集群
Redis集群:使用Spring和jedisCluster操作Redis集群
417 0
|
26天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
40 0
|
2月前
|
缓存 Java Maven
Spring Boot自动配置原理
Spring Boot自动配置原理
48 0
|
1月前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
111 0
|
1月前
|
前端开发 搜索推荐 Java
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
|
14天前
|
前端开发 Java 应用服务中间件
Springboot对MVC、tomcat扩展配置
Springboot对MVC、tomcat扩展配置
|
5天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
14 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置