Spring-boot-starter-redis Configuration Detailed
Spring-boot-starter-redis is mainly implemented by configuring the relevant parameters in the Redisconnectionfactory to connect Redis service. Redisconnectionfactory is an interface that has the following 4 specific implementation classes, which we typically use jedisconnectionfactory.
The basic configuration of Redis in the spring boot configuration file is as follows:
# Redis Server Address
spring.redis.host=192.168.0.58
# Redis Server Connection port
spring.redis.port=6379
# Redis Server connection password (default is null, if the Redis service-side profile opens the Requirepass password, the corresponding configuration password should be filled in here)
spring.redis.password=
# Connection time-out (milliseconds)
Spring.redis.timeout=0
Top these 4 items are the basic configuration items in the Jedisconnectionfactory class, in fact, there are some such as connection pool, cluster, master-slave, Sentinel and other configuration, here first simple introduction of the next Connection pool (Jedispoolconfig), Need to know the other configuration can see the source code. Genericobjectpoolconfig is the parent class of Jedispoolconfig, which provides a total of three parameters for Maxtotal, Maxidle, and Maxidle, and also sets the default parameters.
# Connection Pool Maximum number of connections (use negative values to indicate no limit, corresponding to maxtotal)
Spring.redis.pool.max-active=8
# Maximum idle connections in the connection pool
spring.redis.pool.max-idle=8
# Minimum idle connections in the connection pool
spring.redis.pool.min-idle=0
Once the configuration file is configured, a Redis configuration class is also required to configure key and value serialization and to load the relevant parameters in the configuration file
If you only need to use the basic Redis configuration, then use the following configuration class, Spring boot will automatically scan the basic configuration of Redis, but one thing to note is that password, if you set password in the config file, Then you must manually inject jedisconnectionfactory in the configuration class, otherwise it will be reported Noauth authentication required during the startup process. :
- @Configuration
- @EnableCaching
- public class Redisconfig extends cachingconfigurersupport{
- @Bean
- Public keygenerator keygenerator() {
- return New Keygenerator () {
- Public Object generate(object target, Method method, Object ... params) {
- StringBuilder sb = new StringBuilder ();
- Sb.append (Target.getclass (). GetName ());
- Sb.append ("_"). Append (Method.getname ());
- For (Object obj:params) {
- Sb.append ("_"). Append (Obj.tostring ());
- }
- return sb.tostring ();
- }
- };
- }
- @SuppressWarnings ("Rawtypes")
- @Bean
- Public CacheManager cachemanager(redistemplate redistemplate) {
- Rediscachemanager RCM = new Rediscachemanager (redistemplate);
- //Set cache expiration Time
- //rcm.setdefaultexpiration (60);//sec
- return RCM;
- }
- @Bean
- Public redistemplate<string, string> redistemplate(Redisconnectionfactory factory) {
- Stringredistemplate template = new Stringredistemplate (factory);
- @SuppressWarnings ({ "rawtypes", "Unchecked"})
- 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);
- Template.setvalueserializer (Jackson2jsonredisserializer);
- Template.afterpropertiesset ();
- Jedisconnectionfactory JC = (jedisconnectionfactory) factory;
- System.out.println (Jc.gethostname ());
- return template;
- }
- }
If you have also configured parameters such as connection pooling, add the following in the configuration class above:
- @Bean
- Public jedisconnectionfactory redisconnectionfactory() {
- Jedisconnectionfactory factory = new Jedisconnectionfactory ();
- Factory.sethostname (host);
- Factory.setport (port);
- Factory.setpassword (password);
- Factory.settimeout (timeout); //Set connection time-out
- return factory;
- }
使用factory进行set你所配置的值即可。
One thing to explain is that there are a number of properties in the configuration class that are injected into the configuration file, as you can see from the blog:
Click to open link
StringRedisTemplate与RedisTemplate使用时的注意事项:
1、StringRedisTemplate是RedisTemplate的唯一子类
2、StringRedisTemplate默认采用的key序列化方式为setKeySerializer(stringSerializer);此时在使用Spring的缓存注解如@Cacheable的key属性设置值时,就需
- Note that if the parameter type is long then the string type conversion exception is not possible.
- 3, redistemplate the default use of the serialization mode is Jdkserializationredisserializer, it does not have the top problem. Because its serialization method is serialize (object object)
Spring-boot-starter-redis Configuration Detailed