您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot集成Redis引起的序列化問題怎么解決”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“SpringBoot集成Redis引起的序列化問題怎么解決”吧!
想必大家對SpringBoot可能已經很熟悉了,包括集成Redis這種常用的技術,之前一直用一貫的寫法去集成Redis,寫配置類沒發現過任何問題,但是上周在給Redis配置類加了一個Bean之后就出現了很難發現的問題。
配置類代碼 `@Configuration public class RedisConfig extends CachingConfigurerSupport {
[@Bean](https://my.oschina.net/bean) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // 使用Jackson2JsonRedisSerialize 替換默認序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); // 設置value的序列化規則和 key的序列化規則 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } [@Bean](https://my.oschina.net/bean) public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); 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); // 配置序列化(解決亂碼的問題),過期時間30秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(1800000)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; }
}` 也就是增加了cacheManager()這個方法之后,數據量小的時候是沒有任何問題的,但是一旦放到線上,數據量一上來,那么整個Redis就導致內存溢出,整個服務都會掛掉。
錯誤日志: org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.lang.OutOfMemoryError: Java heap space at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:84) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE] at org.springframework.session.data.redis.RedisOperationsSessionRepository.onMessage(RedisOperationsSessionRepository.java:538) ~[spring-session-data-redis-2.1.5.RELEASE.jar!/:2.1.5.RELEASE] at org.springframework.data.redis.listener.RedisMessageListenerContainer.executeListener(RedisMessageListenerContainer.java:250) [spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE] at org.springframework.data.redis.listener.RedisMessageListenerContainer.processMessage(RedisMessageListenerContainer.java:240) [spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE] at org.springframework.data.redis.listener.RedisMessageListenerContainer.lambda$dispatchMessage$0(RedisMessageListenerContainer.java:986) [spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE] at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_191] Caused by: org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.lang.OutOfMemoryError: Java heap space at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:78) ~[spring-core-5.1.6.RELEASE.jar!/:5.1.6.RELEASE] at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:36) ~[spring-core-5.1.6.RELEASE.jar!/:5.1.6.RELEASE] at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:82) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE] ... 5 common frames omitted Caused by: java.lang.OutOfMemoryError: Java heap space
至于為什么注入這個bean之后會出現這個問題,去了這個bean之后,不會再出現這個問題,目前我還沒找到原因,分享出來大家一起參考,避免在網上找的配置應用在自己的代碼中出現問題,也歡迎踩過坑的朋友來解答。
感謝各位的閱讀,以上就是“SpringBoot集成Redis引起的序列化問題怎么解決”的內容了,經過本文的學習后,相信大家對SpringBoot集成Redis引起的序列化問題怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。