|
@@ -1,76 +1,61 @@
|
|
package com.fdkankan.redis.config;
|
|
package com.fdkankan.redis.config;
|
|
|
|
|
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.redisson.Redisson;
|
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
|
+import org.redisson.config.Config;
|
|
|
|
+import org.redisson.config.SingleServerConfig;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
|
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
|
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
-import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
|
-import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
-import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
|
|
-import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
@AutoConfigureAfter(RedisAutoConfiguration.class)
|
|
@AutoConfigureAfter(RedisAutoConfiguration.class)
|
|
public class RedisConfig {
|
|
public class RedisConfig {
|
|
|
|
|
|
-// @Autowired
|
|
|
|
-// LettuceConnectionFactory lettuceConnectionFactory;
|
|
|
|
|
|
+ @Value("${spring.redis.host}")
|
|
|
|
+ private String host;
|
|
|
|
|
|
-// @Bean
|
|
|
|
-// public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
|
|
|
|
-// RedisTemplate template = new RedisTemplate();
|
|
|
|
-// template.setConnectionFactory(redisConnectionFactory);
|
|
|
|
-//// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
|
|
|
-// StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
|
|
|
-//
|
|
|
|
-// /*
|
|
|
|
-// * Jackson对象映射器(Object Mapper)可以把JSON解析为用户自定义类对象, 或者解析为JSON内置的树模型的对象
|
|
|
|
-// * DefaultTyping.NON_FINAL,将会为(String、Boolean、Integer、Double)除外的其他类型及非final类型的数组添加反序列化所需要的类型。
|
|
|
|
-// * JsonTypeInfo.As.PROPERTY,它将包含@class属性,作为序列化的一个属性,值就是完全限定名类型。当前类及其属性都会添加这个名为@class的属性。
|
|
|
|
-// */
|
|
|
|
-//// ObjectMapper om = new ObjectMapper();
|
|
|
|
-//// om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
|
-//// om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
|
|
|
-//// jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
|
|
-// template.setKeySerializer(stringRedisSerializer);
|
|
|
|
-// template.setValueSerializer(stringRedisSerializer);
|
|
|
|
-// template.setHashKeySerializer(stringRedisSerializer);
|
|
|
|
-// template.setHashValueSerializer(stringRedisSerializer);
|
|
|
|
-//
|
|
|
|
-// template.afterPropertiesSet();
|
|
|
|
-// template.setEnableTransactionSupport(true);
|
|
|
|
-// return template;
|
|
|
|
-// }
|
|
|
|
|
|
+ @Value("${spring.redis.port}")
|
|
|
|
+ private String port;
|
|
|
|
|
|
|
|
+ @Value("${spring.redis.database}")
|
|
|
|
+ private int database;
|
|
|
|
+
|
|
|
|
+ @Value("${spring.redis.password}")
|
|
|
|
+ private String password;
|
|
|
|
+
|
|
|
|
+ @Bean
|
|
|
|
+ public RedissonClient redissonClient() {
|
|
|
|
+ Config config = new Config();
|
|
|
|
+ SingleServerConfig singleServerConfig = config.useSingleServer();
|
|
|
|
+ singleServerConfig.setAddress("redis://".concat(host).concat(":").concat(port));
|
|
|
|
+ if (StringUtils.isNotEmpty(password)) {
|
|
|
|
+ singleServerConfig.setPassword(password);
|
|
|
|
+ }
|
|
|
|
+ singleServerConfig.setDatabase(database);
|
|
|
|
+ return Redisson.create(config);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
|
RedisTemplate template = new RedisTemplate();
|
|
RedisTemplate template = new RedisTemplate();
|
|
template.setConnectionFactory(redisConnectionFactory);
|
|
template.setConnectionFactory(redisConnectionFactory);
|
|
-// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
|
|
|
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
|
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
|
|
|
|
|
- /*
|
|
|
|
- * Jackson对象映射器(Object Mapper)可以把JSON解析为用户自定义类对象, 或者解析为JSON内置的树模型的对象
|
|
|
|
- * DefaultTyping.NON_FINAL,将会为(String、Boolean、Integer、Double)除外的其他类型及非final类型的数组添加反序列化所需要的类型。
|
|
|
|
- * JsonTypeInfo.As.PROPERTY,它将包含@class属性,作为序列化的一个属性,值就是完全限定名类型。当前类及其属性都会添加这个名为@class的属性。
|
|
|
|
- */
|
|
|
|
-// ObjectMapper om = new ObjectMapper();
|
|
|
|
-// om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
|
-// om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
|
|
|
-// jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
|
|
template.setKeySerializer(stringRedisSerializer);
|
|
template.setKeySerializer(stringRedisSerializer);
|
|
template.setValueSerializer(stringRedisSerializer);
|
|
template.setValueSerializer(stringRedisSerializer);
|
|
template.setHashKeySerializer(stringRedisSerializer);
|
|
template.setHashKeySerializer(stringRedisSerializer);
|
|
template.setHashValueSerializer(stringRedisSerializer);
|
|
template.setHashValueSerializer(stringRedisSerializer);
|
|
|
|
|
|
template.afterPropertiesSet();
|
|
template.afterPropertiesSet();
|
|
-// template.setEnableTransactionSupport(true);
|
|
|
|
return template;
|
|
return template;
|
|
}
|
|
}
|
|
|
|
|