In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces "how to configure Springboot integration redis" in detail, with detailed content, clear steps and proper handling of details. I hope this "Springboot integration redis how to configure" article can help you solve your doubts.
1. Import depends on org.springframework.boot spring-boot-starter-data-redis2. Write properties or yml to configure the # Redis local server address, and note that the redis service, the redis-server.exespring.redis.host=127.0.0.1#Redis server port, is enabled. The default is 6379. If you change the spring.redis.port=6379#Redis server connection password after the change, the default is empty. If you set the maximum number of connections to spring.redis.password=# connection pool as set, if you are responsible, there is no limit on the maximum blocking waiting time of spring.redis.jedis.pool.max-active=8# connection pool. If you are responsible, you do not have any restrictions on the maximum idle connection spring.redis.jedis.pool.max-idle=8spring: redis: # Redis local server address in the spring.redis.jedis.pool.max-wait=-1# connection pool. Be sure to enable the redis service, that is, the redis-server.exe host: 127.0.0.1 # Redis server port, which defaults to 6379. If there is any change, the port: 6379 # Redis server connection password is empty by default. If it is set, the maximum number of connections to the password: jedis: pool: # connection pool is set. If it is negative, there is no limit on the maximum blocking waiting time of the max-active: 8 # connection pool. A negative number means there is no limit to the maximum idle connection in the max-wait:-1 # connection pool max-idle: 83. Select Redis template as needed
There are two kinds of Redis templates, one is RedisTemplate, the other is StringRedisTemplate
How to choose depends on the requirements of the data type stored by our redis
RedisTemplate
StringRedisTemplate
The template is a generic class, so it can operate on any type of key and value
The template is serialized by using the JdkSerializationRedisSerializer class to get a byte array, and deserializing to get data of type Object.
This class inherits RedisTemplate
Public class StringRedisTemplate extends RedisTemplate
Because the generics of this class are set to String, the class can only operate on data with both key and value of type String
First, let's talk about the difference between the two templates.
After knowing the difference between the two templates, we have a good choice. If we need to operate on data of entity types such as objects or basic data types other than String, then we choose RedisTemplate. If we naturally use StringRedisTemplate for String types, we can also use RedisTemplate to operate basic data types such as int, but we just need to use the String type to receive when deserializing. Everything is based on specific needs.
4. Configuration and interpretation of templates
Because of the emphasis on configuration, SpringRedisTemplate and utility classes are not described here
Custom configuration class
Package cn.wenhe9.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheConfiguration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate Import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration @ Configurationpublic class RedisConfig extends CachingConfigurerSupport {/ * Custom RedisTemplate * @ param redisConnectionFactory * @ return * / @ Bean public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) {/ / in most cases, RedisTemplate template = new RedisTemplate (); template.setConnectionFactory (redisConnectionFactory) / / use the serialized object of JSON to serialize the data key and value Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); / / ObjectMapper is a working class of Jackson, as the name implies, its function is to map JSON to Java object, that is, deserialization, or to map Java object to JSON, that is, serialize ObjectMapper mapper = new ObjectMapper () / / to set the visibility when serializing, the first parameter is to choose which properties to serialize, such as setter when serializing? Or the second parameter of filed?h is to select which modifier permission attributes to serialize, such as private or public, where any means that all permissions modified attributes are visible (serializable) mapper.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY) / / set the type of failure that is the error. The first one refers to the verification program, where the parameter does not need to be verified, and other parameters can be found in the source code (the author is still in the source code). The second is that this class cannot be modified by final, otherwise it will report an error mapper.activateDefaultTyping (LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper (mapper). / / set the serialization mode of RedisTemplate template to jacksonSeial template.setDefaultSerializer (jacksonSeial); return template } / * Custom cache manager * @ param redisConnectionFactory * @ return * / @ Bean public RedisCacheManager cacheManager (RedisConnectionFactory redisConnectionFactory) {/ / create String and JSON serialized objects and type convert the data of key and value RedisSerializer strSerializer = new StringRedisSerializer (); Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer (Object.class); ObjectMapper mapper = new ObjectMapper () Mapper.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.activateDefaultTyping (LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper (mapper) / / customize cache data serialization method and validity period RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig () .entryTtl (Duration.ofDays (1)) / set the cache validity period to 1 day. SerializeKeysWith (RedisSerializationContext.SerializationPair.fromSerializer (strSerializer)) / / use strSerializer to convert key datatypes. SerializeValuesWith (RedisSerializationContext.SerializationPair.fromSerializer (jacksonSeial)) / / use jacksonSeial to convert the data types of value. / / A pair of empty data does not operate RedisCacheManager cacheManager = RedisCacheManager.builder (redisConnectionFactory) .cacheDefaults (config) .build (); return cacheManager }} here, the article "how to configure Springboot Integration redis" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.