Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the serialization problem caused by SpringBoot Integration Redis

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "SpringBoot integration Redis caused by serialization problems how to solve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "SpringBoot integration Redis caused by serialization problems how to solve" it!

You may already be familiar with SpringBoot, including the commonly used technology of integrating Redis, which has always been written in the same way to integrate Redis, and no problems have been found in writing the configuration class, but there were problems that were difficult to find after adding a Bean to the Redis configuration class last week.

Configure the class code `@ Configuration public class RedisConfig extends CachingConfigurerSupport {

[@ Bean] (https://my.oschina.net/bean)public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) {RedisTemplate redisTemplate = new RedisTemplate (); redisTemplate.setConnectionFactory (redisConnectionFactory); / / replace the default serialization Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class) with Jackson2JsonRedisSerialize; ObjectMapper objectMapper = new ObjectMapper (); objectMapper.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper (objectMapper) / / set value serialization rules and key serialization rules 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 redisSerializer = new StringRedisSerializer (); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class) / / resolve the problem of query cache conversion exception ObjectMapper om = new ObjectMapper (); om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper (om) / / configure serialization (solve garbled problem). Expiration time 30 seconds RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig () .entryTtl (Duration.ofSeconds (1800000)) .serializeKeysWith (RedisSerializationContext.SerializationPair.fromSerializer (redisSerializer)) .serializeValuesWith (RedisSerializationContext.SerializationPair.fromSerializer (jackson2JsonRedisSerializer)) .serileCachingNullValues (); RedisCacheManager cacheManager = RedisCacheManager.builder (factory) .cacheDefaults (config) .build () Return cacheManager;}

} 'that is, with the addition of the cacheManager () method, there will be no problem when the amount of data is small, but once the amount of data is put online, the entire Redis will cause memory overflow and the whole service will fail.

Error log: 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) ~ [Java heap space at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize (JdkSerializationRedisSerializer.java:84) ~ [springMutual dataMutual 2.1.6.RELEASE.jarmist] at org.springframework.session.data.redis.RedisOperationsSessionRepository.onMessage] RELEASE] at org.springframework.session.data.redis.RedisOperationsSessionRepository.onMessage (RELEASE) ~ [SpringMueller dataMustLay2.1.5.RELEASE.jarshaw at org.springframework.data.redis] .listener.RedisMessageListenerContainer.executeListener (RedisMessageListenerContainer.java:250) [springripple 2.1.6.RELEASE.jarring pedigree 2.1.6.RELEASE] at org.springframework.data.redis.listener.RedisMessageListenerContainer.processMessage (RedisMessageListenerContainer.java:240) [springripdataListenerContainer.executeListener (RedisMessageListenerContainer.java:250) [at org.springframework.data.redis.listener.RedisMessageListenerContainer.lambda$dispatchMessage$0 (RedisMessageListenerContainer.java:986)] RELEASE [RELEASE] .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) ~ [SpringMutual 5.1.6.RELEASE.jarmist pezz 5.1.6.RELEASE] at org.springframework.core.serializer.support.DeserializingConverter.convert (DeserializingConverter.java:36) ~ [SpringMurcoreMel 5.1.6.RELEASE.jarlux 5.1.6.RELEASE] at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize (JdkSerializationRedisSerializer.java:82) ~ [springMurray java.lang.OutOfMemoryError: Java heap space 2.1.6.RELEASE.jarfuzag Red2.1.6.RELEASE]... 5 RELEASE: RELEASE

As for why this problem will occur after the injection of this bean, after going to this bean, this problem will not occur again. At present, I have not found the reason. Share it with you to avoid problems with the configuration applications found on the Internet in your own code. Friends who have stepped on the hole are also welcome to answer them.

Thank you for reading, the above is the content of "how to solve the serialization problem caused by SpringBoot integration Redis". After the study of this article, I believe you have a deeper understanding of how to solve the serialization problem caused by SpringBoot integration Redis. The specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report