Springboot+Redis序列化坑
今天在测试springboot整合redis的时候遇到下面这个坑,百度来百度去发现提示都是ajax的问题,真的是醉了,错误提示如下所示,不信大家可以直接复制百度一下答案是什么(流泪中。。。。),错误如下:
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unrecognized token 'b4811c63': was expecting ('true', 'false' or 'null')
1.错误原因排查
然后就进行debug调试,发现代码一直到redisTemplate.opsForZSet().reverseRangeWithScores()这一行都没问题,然后进入redis源代码里面检查,发现是在发送redis服务的时候出现问题,所以可以断定应该是配置的问题,然后仔细检查配置,发现也没有错误,redis序列化的配置如下所示:
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate redisTemplate = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
2.细节分析
这一条线索断掉之后,只能通过错误信息来分析了,错误信息中有一条特别奇怪,就是token 'b4811c63',然后我仔细从redis中对比,发现是之前存储的redis的key值,判断可能是redis中key存在乱码,所以就将redis的key全部清空,自己添加数据进去,发现自己添加的数据是可以的。从这一现象可以得出,应该实现老系统序列化的规则和现在springboot的序列化规则不一样导致的,查看老系统的redis配置信息,如下所示:
<!--序列化-->
<bean name="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean name="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="clusterRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redis4CacheConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="hashKeySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="stringRedisSerializer"/>
<property name="hashValueSerializer" ref="stringRedisSerializer"/>
</bean>
3.问题解决
然后和我们刚才序列化的方式对比一下,发现真的是序列化方式不一样,旧的是通过StringRedisSerializer进行序列化的,springboot是通过Jackson2JsonRedisSerializer进行序列化的。所以为了兼容老系统的序列化方式,这边我将springboot也改成StringRedisSerializer的序列化方式,代码如下所示:
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate redisTemplate = new StringRedisTemplate(factory);
StringRedisSerializer stringRedisSerializer =new StringRedisSerializer();
redisTemplate.setValueSerializer(stringRedisSerializer);
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(stringRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
改完之后,发现这个问题就没有了,redis中就可以照常插入、查询数据了。
总结:
这个问题是很典型的架构优化问题,老系统和新系统代码兼容性问题。从这个坑可以得出这样的结论:百度得到的答案很大一部分都是片面的,我们还是得根据实际情况来分析。其次就是要仔细看报错信息,不要放过一点细节,因为可能你的答案就在这一点细节中。
Springboot+Redis序列化坑的更多相关文章
- SpringBoot Redis序列化配置
Redis配置 #Redis spring.redis.host= spring.redis.port=6379 spring.redis.database=0 # Redis服务器连接密码(默认为空 ...
- SpringBoot Redis使用fastjson进行序列化
在使用spring-data-redis,默认情况下是使用org.springframework.data.redis.serializer.JdkSerializationRedisSerializ ...
- 补习系列(14)-springboot redis 整合-数据读写
目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...
- SpringBoot+Redis整合
SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...
- springboot +redis配置
springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
- SpringBoot + Redis:基本配置及使用
注:本篇博客SpringBoot版本为2.1.5.RELEASE,SpringBoot1.0版本有些配置不适用 一.SpringBoot 配置Redis 1.1 pom 引入spring-boot-s ...
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- redis 序列化存入对象
redis 序列化存入对象 //序列化 public static byte [] serialize(Object obj){ ObjectOutputStream obi=null; ByteAr ...
随机推荐
- js面向(基于)对象编程—类(原型对象)与对象
JS分三个部分: 1. ECMAScript标准--基础语法 2. DOM Document Object Model 文档对象模型 3. BOM Browser Object Moldel 浏览 ...
- IDEA 工具项目的配置及如何打war包
1. Project Structure 1.1 首先点击File-ProjectStructure,进入项目配置: 2.Project Settings配置 2.1 Project 2.1.1 f ...
- CentOS7下安装FTP
1.安装vsftpd yum install -y vsftpd 2.设置 使用命令systemctl status vsftpd.service查看ftp状态. 开启ftp systemctl st ...
- 易语言制作的QQ聊天中常用的GIF图片【带源码下载】
该软件调用网页实现表情包制作,使用了精益模块. 最近比较火的王境泽.张学友.切格瓦拉.为所欲为.今天星期五.黑人问号脸.偷电瓶车.诸葛孔明.金坷垃等都可以通过此软件在线制作属于你的表情包. 太困了懒得 ...
- hive 学习系列一(数据类型的定义)
数字类型(Numeric Types) 整型 TINYINT(取值范围:-128 -- 127) SMALLINT(取值范围:-32,768 to 32,767) INT/INTEGER(取值范围: ...
- Python全栈day 01
Python全栈day 01 一.计算机认识 用户 软件,类似微信.QQ.游戏等应用程序,由程序员编写,在系统中运行,完成各种活动,方便人们使用. 操作系统,主要分为windows系统.Linux系统 ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- python语法root=Tkinter.Tk()
1. Tkinter 是一个python模块,是一个调用Tcl/Tk的接口,它是一个跨平台的脚本图形界面接口.Tkinter不是唯一的python图形编程接口,但是是 其中比较流行的一个.最大的特点是 ...
- centos 6.4安装杀毒软件clamAV 0.98[转]
原文出处: http://dnuser.blog.51cto.com/4863891/1303829 1.查看系统版本 [root@local]# lsb_release -a LSB Versi ...
- 程序员最值得听的歌曲TOP10
No.10 一剪梅 费玉清 - 玉笛公子 <一剪梅>是1984年台湾同名电视剧的片头曲,原唱为林禹胜,经典版本由费玉清演唱,后又成为2009年霍建华.吕一主演电视剧<新一剪梅&g ...