Commit 7bd81d03126c2e17145672a9062b80ee801d2f33
1 parent
8e67753a
初始化 redis 配置类
Showing
4 changed files
with
27 additions
and
107 deletions
ruoyi-framework/src/main/java/com/ruoyi/framework/config/FastJson2JsonRedisSerializer.java deleted
| 1 | -package com.ruoyi.framework.config; | ||
| 2 | - | ||
| 3 | -import com.alibaba.fastjson.JSON; | ||
| 4 | -import com.alibaba.fastjson.serializer.SerializerFeature; | ||
| 5 | -import com.fasterxml.jackson.databind.JavaType; | ||
| 6 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 7 | -import com.fasterxml.jackson.databind.type.TypeFactory; | ||
| 8 | -import org.springframework.data.redis.serializer.RedisSerializer; | ||
| 9 | -import org.springframework.data.redis.serializer.SerializationException; | ||
| 10 | -import com.alibaba.fastjson.parser.ParserConfig; | ||
| 11 | -import org.springframework.util.Assert; | ||
| 12 | - | ||
| 13 | -import java.nio.charset.Charset; | ||
| 14 | - | ||
| 15 | -/** | ||
| 16 | - * Redis使用FastJson序列化 | ||
| 17 | - * | ||
| 18 | - * @author ruoyi | ||
| 19 | - */ | ||
| 20 | -public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> { | ||
| 21 | - @SuppressWarnings("unused") | ||
| 22 | - private ObjectMapper objectMapper = new ObjectMapper(); | ||
| 23 | - | ||
| 24 | - public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); | ||
| 25 | - | ||
| 26 | - private Class<T> clazz; | ||
| 27 | - | ||
| 28 | - static { | ||
| 29 | - ParserConfig.getGlobalInstance().setAutoTypeSupport(true); | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - public FastJson2JsonRedisSerializer(Class<T> clazz) { | ||
| 33 | - super(); | ||
| 34 | - this.clazz = clazz; | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - @Override | ||
| 38 | - public byte[] serialize(T t) throws SerializationException { | ||
| 39 | - if (t == null) { | ||
| 40 | - return new byte[0]; | ||
| 41 | - } | ||
| 42 | - return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | - @Override | ||
| 46 | - public T deserialize(byte[] bytes) throws SerializationException { | ||
| 47 | - if (bytes == null || bytes.length <= 0) { | ||
| 48 | - return null; | ||
| 49 | - } | ||
| 50 | - String str = new String(bytes, DEFAULT_CHARSET); | ||
| 51 | - | ||
| 52 | - return JSON.parseObject(str, clazz); | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | - public void setObjectMapper(ObjectMapper objectMapper) { | ||
| 56 | - Assert.notNull(objectMapper, "'objectMapper' must not be null"); | ||
| 57 | - this.objectMapper = objectMapper; | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - protected JavaType getJavaType(Class<?> clazz) { | ||
| 61 | - return TypeFactory.defaultInstance().constructType(clazz); | ||
| 62 | - } | ||
| 63 | -} |
ruoyi-framework/src/main/java/com/ruoyi/framework/config/RedisConfig.java deleted
| 1 | -package com.ruoyi.framework.config; | ||
| 2 | - | ||
| 3 | -import org.springframework.cache.annotation.CachingConfigurerSupport; | ||
| 4 | -import org.springframework.cache.annotation.EnableCaching; | ||
| 5 | -import org.springframework.context.annotation.Bean; | ||
| 6 | -import org.springframework.context.annotation.Configuration; | ||
| 7 | -import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
| 8 | -import org.springframework.data.redis.core.RedisTemplate; | ||
| 9 | -import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
| 10 | -import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
| 11 | -import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
| 12 | -import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
| 13 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 14 | -import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; | ||
| 15 | - | ||
| 16 | -/** | ||
| 17 | - * redis配置 | ||
| 18 | - * | ||
| 19 | - * @author ruoyi | ||
| 20 | - */ | ||
| 21 | -@Configuration | ||
| 22 | -@EnableCaching | ||
| 23 | -public class RedisConfig extends CachingConfigurerSupport { | ||
| 24 | - @Bean | ||
| 25 | - @SuppressWarnings(value = {"unchecked", "rawtypes"}) | ||
| 26 | - public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { | ||
| 27 | - RedisTemplate<Object, Object> template = new RedisTemplate<>(); | ||
| 28 | - template.setConnectionFactory(connectionFactory); | ||
| 29 | - | ||
| 30 | - FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class); | ||
| 31 | - | ||
| 32 | - ObjectMapper mapper = new ObjectMapper(); | ||
| 33 | - mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | ||
| 34 | - mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); | ||
| 35 | - serializer.setObjectMapper(mapper); | ||
| 36 | - | ||
| 37 | - template.setValueSerializer(serializer); | ||
| 38 | - // 使用StringRedisSerializer来序列化和反序列化redis的key值 | ||
| 39 | - template.setKeySerializer(new StringRedisSerializer()); | ||
| 40 | - template.afterPropertiesSet(); | ||
| 41 | - return template; | ||
| 42 | - } | ||
| 43 | -} |
src/main/java/cn/iocoder/dashboard/framework/apollo/internals/DBConfigRepository.java
| @@ -34,7 +34,7 @@ public class DBConfigRepository extends AbstractConfigRepository { | @@ -34,7 +34,7 @@ public class DBConfigRepository extends AbstractConfigRepository { | ||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | private final ConfigUtil m_configUtil; | 36 | private final ConfigUtil m_configUtil; |
| 37 | - private PropertiesFactory propertiesFactory; | 37 | + private final PropertiesFactory propertiesFactory; |
| 38 | private final String m_namespace; | 38 | private final String m_namespace; |
| 39 | 39 | ||
| 40 | /** | 40 | /** |
src/main/java/cn/iocoder/dashboard/framework/redis/config/RedisConfig.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.framework.redis.config; | ||
| 2 | + | ||
| 3 | +import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; | ||
| 4 | +import org.springframework.context.annotation.Bean; | ||
| 5 | +import org.springframework.context.annotation.Configuration; | ||
| 6 | +import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
| 7 | +import org.springframework.data.redis.core.RedisTemplate; | ||
| 8 | +import org.springframework.data.redis.serializer.RedisSerializer; | ||
| 9 | + | ||
| 10 | +@Configuration | ||
| 11 | +public class RedisConfig { | ||
| 12 | + | ||
| 13 | + @Bean | ||
| 14 | + public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { | ||
| 15 | + // 创建 RedisTemplate 对象 | ||
| 16 | + RedisTemplate<String, Object> template = new RedisTemplate<>(); | ||
| 17 | + // 设置 RedisConnection 工厂。😈 它就是实现多种 Java Redis 客户端接入的秘密工厂。感兴趣的胖友,可以自己去撸下。 | ||
| 18 | + template.setConnectionFactory(factory); | ||
| 19 | + // 使用 String 序列化方式,序列化 KEY 。 | ||
| 20 | + template.setKeySerializer(RedisSerializer.string()); | ||
| 21 | + // 使用 JSON 序列化方式(库是 FastJSON ),序列化 VALUE 。 | ||
| 22 | + template.setValueSerializer(new GenericFastJsonRedisSerializer()); | ||
| 23 | + return template; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | +} |