package com.zsk.springboot.redis.redis; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.PropertySource; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; /** * https://blog.csdn.net/qq_36666651/article/details/82226319*/ @Configuration @PropertySource("classpath:redis.properties") public class RedisTemplateConfig { @Value("${redis.lettuce.pool.maxIdle}") private int maxIdle; @Value("${redis.lettuce.pool.minIdle}") private int minIdle; @Value("${redis.lettuce.pool.maxTotal}") private int maxTotal; @Value("${redis.server.host}") private String host; @Value("${redis.server.port}") private int port; @Bean public GenericObjectPoolConfig genericObjectPoolConfig() { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxIdle(maxIdle); poolConfig.setMinIdle(minIdle); poolConfig.setMaxTotal(maxTotal); return poolConfig; } @Bean public LettuceClientConfiguration lettuceClientConfiguration(GenericObjectPoolConfig genericObjectPoolConfig) { return LettucePoolingClientConfiguration.builder() .poolConfig(genericObjectPoolConfig) .build(); } @Bean public RedisStandaloneConfiguration redisSentinelConfiguration() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port); redisStandaloneConfiguration.setDatabase(0); return redisStandaloneConfiguration; } @Bean public LettuceConnectionFactory lettuceConnectionFactory(RedisStandaloneConfiguration redisSentinelConfiguration, LettuceClientConfiguration lettuceClientConfiguration) { return new LettuceConnectionFactory(redisSentinelConfiguration, lettuceClientConfiguration); } @Bean(name = "redisTemplate") @Primary public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { return getStringStringRedisTemplate(factory); } @Bean(name = "redisTemplate2") public RedisTemplate<String, Object> redisTemplate2(RedisConnectionFactory factory) { return getObjectRedisTemplate(factory); } private RedisTemplate<String, String> getStringStringRedisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); template.setKeySerializer(RedisSerializer.string()); template.setValueSerializer(RedisSerializer.string()); template.setHashKeySerializer(RedisSerializer.string()); template.setHashValueSerializer(RedisSerializer.string()); return template; } private RedisTemplate<String, Object> getObjectRedisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = createJackson2JsonRedisSerializer(); template.setKeySerializer(RedisSerializer.string()); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashKeySerializer(RedisSerializer.string()); template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } private Jackson2JsonRedisSerializer<Object> createJackson2JsonRedisSerializer() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); return jackson2JsonRedisSerializer; } }