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

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介:

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

引言
了解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不会有问题。

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

redis cluster hash slot
JedisCluster & Jedis的关系
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集群进行打交道的,具体过程是:

获取该key的slot值:JedisClusterCRC16.getSlot(key)
从JedisClusterConnectionHandler实例中获取到该slot对应的Jedis实例:Jedis connection = connectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
利用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版本已经修复此问题
  • 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();
// jedis会自动将资源归还到连接池
jedis.close();
RedisConnectionUtils.releaseConnection(redisConnection, factory);
以上代码完全可以模仿spring-data-redis中RedisTemplate#executePipelined方法写成一个通用的方法,供使用者调用。

原文地址https://my.oschina.net/u/4554374/blog/4306457

相关文章
|
存储 缓存 NoSQL
redis集群+JedisCluster+lua脚本实现分布式锁
redis集群+JedisCluster+lua脚本实现分布式锁
367 0
|
NoSQL 算法 Java
Redis进阶-JedisCluster初始化 & 自动管理连接池中的连接 _ 源码分析
Redis进阶-JedisCluster初始化 & 自动管理连接池中的连接 _ 源码分析
841 0
|
NoSQL Java Redis
Redis集群:使用Spring和jedisCluster操作Redis集群
Redis集群:使用Spring和jedisCluster操作Redis集群
|
存储 NoSQL Java
spring-data-redis中JedisCluster不支持pipelined问题解决
引言 了解Jedis的童鞋可能清楚,Jedis中JedisCluster是不支持pipeline操作的,如果使用了redis集群,在spring-boot-starter-data-redis中又正好用到的pipeline,那么会接收到Pipeline is currently not supported for JedisClusterConnection.这样的报错。
3861 0
|
2月前
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
729 0
|
6月前
|
前端开发 Java 数据库
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
300 0
|
2月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
375 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
6月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
272 0
|
6月前
|
Java 测试技术 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
本课主要讲解Spring Boot项目中的属性配置方法。在实际开发中,测试与生产环境的配置往往不同,因此不应将配置信息硬编码在代码中,而应使用配置文件管理,如`application.yml`。例如,在微服务架构下,可通过配置文件设置调用其他服务的地址(如订单服务端口8002),并利用`@Value`注解在代码中读取这些配置值。这种方式使项目更灵活,便于后续修改和维护。
91 0
|
6月前
|
SQL Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— application.yml 中对日志的配置
在 Spring Boot 项目中,`application.yml` 文件用于配置日志。通过 `logging.config` 指定日志配置文件(如 `logback.xml`),实现日志详细设置。`logging.level` 可定义包的日志输出级别,例如将 `com.itcodai.course03.dao` 包设为 `trace` 级别,便于开发时查看 SQL 操作。日志级别从高到低为 ERROR、WARN、INFO、DEBUG,生产环境建议调整为较高级别以减少日志量。本课程采用 yml 格式,因其层次清晰,但需注意格式要求。
597 0