diff --git a/pom.xml b/pom.xml index f5a3a864b..fb2f689ee 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,7 @@ 4.0.4.B 6.0.13.Final 1.4.1 + 2.9.0 @@ -59,7 +60,6 @@ - @@ -110,7 +110,6 @@ ${hibernate-validator.version} - io.seata seata-all @@ -123,6 +122,11 @@ ${seata.version} + + org.apache.commons + commons-pool2 + ${commons-pool2-version} + diff --git a/youlai-common/common-redis/pom.xml b/youlai-common/common-redis/pom.xml index 4a88b93a4..5e46fa2a9 100644 --- a/youlai-common/common-redis/pom.xml +++ b/youlai-common/common-redis/pom.xml @@ -11,23 +11,24 @@ common-redis - org.springframework.boot spring-boot-starter-data-redis - + + org.apache.commons + commons-pool2 + + com.fasterxml.jackson.core jackson-databind - com.fasterxml.jackson.core jackson-core - com.fasterxml.jackson.core jackson-annotations diff --git a/youlai-common/common-redis/src/main/java/com/youlai/common/redis/RedisConfig.java b/youlai-common/common-redis/src/main/java/com/youlai/common/redis/RedisConfig.java index 5c583fb0d..9359bcf22 100644 --- a/youlai-common/common-redis/src/main/java/com/youlai/common/redis/RedisConfig.java +++ b/youlai-common/common-redis/src/main/java/com/youlai/common/redis/RedisConfig.java @@ -8,7 +8,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @@ -18,39 +18,28 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; public class RedisConfig { @Bean - public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate<>(); - redisTemplate.setConnectionFactory(factory); + redisTemplate.setConnectionFactory(lettuceConnectionFactory); - StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); - // 使用StringRedisSerializer来序列化和反序列化redis的key值 - redisTemplate.setKeySerializer(stringRedisSerializer); // 用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 - redisTemplate.setValueSerializer(serializer()); - // hash的key也采用String的序列化方式 + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); + redisTemplate.setKeySerializer(stringRedisSerializer); // key + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); + ObjectMapper objectMapper = new ObjectMapper(); + // 指定要序列化的域(field,get,set),访问修饰符(public,private,protected) + objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); + jackson2JsonRedisSerializer.setObjectMapper(objectMapper); + + redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); //value + redisTemplate.setHashKeySerializer(stringRedisSerializer); - // hash的value序列化方式采用jackson - redisTemplate.setHashValueSerializer(serializer()); + redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); + redisTemplate.afterPropertiesSet(); return redisTemplate; } - - /** - * 配置Jackson2JsonRedisSerializer序列化策略 - */ - private Jackson2JsonRedisSerializer serializer() { - // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 - Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); - ObjectMapper objectMapper = new ObjectMapper(); - - // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public - objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); - // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 - objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); - jackson2JsonRedisSerializer.setObjectMapper(objectMapper); - return jackson2JsonRedisSerializer; - } - }