SpringBoot配置RedisTemplate序列化

前言

最新的SpringBoot的相关Starter很多已经省略掉了自定义的@Configuration类,取而代之的是直接在application.yml文件中直接配置,比如Druid现在只需要maven引入其starter并在application.yml文件配置一下就可以了。今天要说的redis也是这样的。
究其原因为什么呢?
因为maven引入的starter里面已经有写好的Configuration了,相当于这个工作Starter已经帮我们做好了,打开你的ide,直接查看源码

package org.springframework.boot.autoconfigure.data.redis;

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {

}
package org.springframework.boot.autoconfigure.data.redis;

import java.time.Duration;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Configuration properties for Redis.
 *
 * @author Dave Syer
 * @author Christoph Strobl
 * @author Eddú Meléndez
 * @author Marco Aust
 * @author Mark Paluch
 * @author Stephane Nicoll
 */
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
}

1、配置Redis

step1
配置application.yml文件,这些属性都可以在上面两个源码类里面得到解析,这里使用了lettuce而不是jedis,前者线程池是线程安全的,这个后期再做解释。

  #redis配置,此处使用lettuce
  redis:
    timeout: 5000ms
    password: 密码
    database: 0
    sentinel:
      nodes: 172.13.37.84:26379,177.15.33.82:26379,172.46.36.82:26379,172.11.31.36:26379,172.14.76.57:26379
      master: master
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0

step2
没有step2了,上面就配置完了。
开玩笑的,上面配置完只能使用StringRedisTemplate,使用其他类型的RedisTemplate还需要配置序列化

@Configuration
public class RedisConfig {

    @Bean
    public  RedisTemplate redisTemplateKeyString(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
    /**配置其他类型的redisTemplate***/
    @Bean
    public RedisTemplate redisTemplateKeyObject(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

你可能感兴趣的:(SpringBoot配置RedisTemplate序列化)