|
@@ -1,11 +1,12 @@
|
|
package com.diagbot.config;
|
|
package com.diagbot.config;
|
|
|
|
|
|
-import com.diagbot.util.BeanUtil;
|
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.cache.CacheManager;
|
|
import org.springframework.cache.CacheManager;
|
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
@@ -13,13 +14,20 @@ import org.springframework.cache.annotation.EnableCaching;
|
|
import org.springframework.cache.interceptor.KeyGenerator;
|
|
import org.springframework.cache.interceptor.KeyGenerator;
|
|
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.context.annotation.Primary;
|
|
|
|
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
|
import org.springframework.data.redis.cache.RedisCacheManager;
|
|
import org.springframework.data.redis.cache.RedisCacheManager;
|
|
-import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
|
|
|
+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.RedisTemplate;
|
|
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
|
+import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
-import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
|
|
+
|
|
|
|
+import java.time.Duration;
|
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
@EnableCaching
|
|
@EnableCaching
|
|
@@ -44,50 +52,46 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
private int port;
|
|
private int port;
|
|
@Value("${spring.redis.timeout}")
|
|
@Value("${spring.redis.timeout}")
|
|
private int timeout;
|
|
private int timeout;
|
|
- @Value("${spring.redis.jedis.pool.max-active}")
|
|
|
|
|
|
+ @Value("${spring.redis.lettuce.pool.max-active}")
|
|
private int maxActive;
|
|
private int maxActive;
|
|
- @Value("${spring.redis.jedis.pool.max-idle}")
|
|
|
|
|
|
+ @Value("${spring.redis.lettuce.pool.max-idle}")
|
|
private int maxIdle;
|
|
private int maxIdle;
|
|
- @Value("${spring.redis.jedis.pool.max-wait}")
|
|
|
|
|
|
+ @Value("${spring.redis.lettuce.pool.max-wait}")
|
|
private long maxWaitMillis;
|
|
private long maxWaitMillis;
|
|
- @Value("${spring.redis.jedis.pool.min-idle}")
|
|
|
|
|
|
+ @Value("${spring.redis.lettuce.pool.min-idle}")
|
|
private int minIdle;
|
|
private int minIdle;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private JedisConnectionFactory jedisConnectionFactory;
|
|
|
|
|
|
+ @Qualifier("factoryForCache")
|
|
|
|
+ private LettuceConnectionFactory lettuceConnectionFactory;
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
- public JedisPoolConfig getRedisConfig() {
|
|
|
|
- JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
|
|
|
|
- jedisPoolConfig.setMaxTotal(maxActive);
|
|
|
|
- jedisPoolConfig.setMaxIdle(maxIdle);
|
|
|
|
- jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
|
|
|
|
- jedisPoolConfig.setMinIdle(minIdle);
|
|
|
|
- return jedisPoolConfig;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Bean(destroyMethod = "destroy")
|
|
|
|
- public JedisConnectionFactory redisConnectionFactory() {
|
|
|
|
- log.info("Create JedisConnectionFactory successful");
|
|
|
|
- JedisConnectionFactory factory = new JedisConnectionFactory();
|
|
|
|
- factory.setHostName(host);
|
|
|
|
- factory.setPort(port);
|
|
|
|
- factory.setTimeout(timeout);
|
|
|
|
- factory.setPassword(password);
|
|
|
|
- factory.setDatabase(Integer.valueOf(databaseCache));
|
|
|
|
- JedisPoolConfig poolConfig = getRedisConfig();
|
|
|
|
- factory.setPoolConfig(poolConfig);
|
|
|
|
- return factory;
|
|
|
|
|
|
+ public GenericObjectPoolConfig getRedisConfig() {
|
|
|
|
+ GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
|
|
|
|
+ poolConfig.setMaxTotal(maxActive);
|
|
|
|
+ poolConfig.setMaxIdle(maxIdle);
|
|
|
|
+ poolConfig.setMaxWaitMillis(maxWaitMillis);
|
|
|
|
+ poolConfig.setMinIdle(minIdle);
|
|
|
|
+ return poolConfig;
|
|
}
|
|
}
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
@Override
|
|
@Override
|
|
public CacheManager cacheManager() {
|
|
public CacheManager cacheManager() {
|
|
- // 初始化缓存管理器,在这里我们可以缓存的整体过期时间什么的,我这里默认没有配置
|
|
|
|
- RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
|
|
|
|
- .RedisCacheManagerBuilder
|
|
|
|
- .fromConnectionFactory(jedisConnectionFactory);
|
|
|
|
- return builder.build();
|
|
|
|
|
|
+ RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
|
|
|
|
+ // 设置 key为string序列化
|
|
|
|
+ .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
|
|
|
|
+ // 设置value为json序列化
|
|
|
|
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(getSerializer()))
|
|
|
|
+ // 不缓存空值
|
|
|
|
+ .disableCachingNullValues();
|
|
|
|
+ RedisCacheManager cacheManager = RedisCacheManager.builder(lettuceConnectionFactory)
|
|
|
|
+ .cacheDefaults(redisCacheConfiguration)
|
|
|
|
+ .transactionAware()
|
|
|
|
+ .build();
|
|
|
|
+ cacheManager.afterPropertiesSet();
|
|
|
|
+ log.info("RedisCacheManager config success");
|
|
|
|
+ return cacheManager;
|
|
}
|
|
}
|
|
|
|
|
|
@Bean(name = "springSessionDefaultRedisSerializer")
|
|
@Bean(name = "springSessionDefaultRedisSerializer")
|
|
@@ -95,19 +99,19 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
return new GenericJackson2JsonRedisSerializer();
|
|
return new GenericJackson2JsonRedisSerializer();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 缓存使用的redis
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Bean("factoryForCache")
|
|
|
|
+ @Primary
|
|
|
|
+ public LettuceConnectionFactory redisConnectionFactory() {
|
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseCache));
|
|
|
|
+ }
|
|
@Bean
|
|
@Bean
|
|
- public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
|
|
|
|
- RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
|
|
|
|
- redisTemplate.setConnectionFactory(jedisConnectionFactory);
|
|
|
|
-
|
|
|
|
- // value值的序列化
|
|
|
|
- redisTemplate.setValueSerializer(getSerializer());
|
|
|
|
- redisTemplate.setHashValueSerializer(getSerializer());
|
|
|
|
- // key的序列化采用StringRedisSerializer
|
|
|
|
- redisTemplate.setKeySerializer(new StringRedisSerializer());
|
|
|
|
- redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
|
|
|
- redisTemplate.afterPropertiesSet();
|
|
|
|
- return redisTemplate;
|
|
|
|
|
|
+ public RedisTemplate<String, Object> redisTemplate() {
|
|
|
|
+ return getRedisTemplate(lettuceConnectionFactory);
|
|
}
|
|
}
|
|
|
|
|
|
private Jackson2JsonRedisSerializer getSerializer() {
|
|
private Jackson2JsonRedisSerializer getSerializer() {
|
|
@@ -124,7 +128,7 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
public KeyGenerator keyGenerator() {
|
|
public KeyGenerator keyGenerator() {
|
|
// 设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存
|
|
// 设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存
|
|
// 使用:进行分割,可以很多显示出层级关系
|
|
// 使用:进行分割,可以很多显示出层级关系
|
|
- // 这里其实就是new了一个KeyGenerator对象,只是这是lambda表达式的写法,我感觉很好用,大家感兴趣可以去了解下
|
|
|
|
|
|
+ // 这里其实就是new了一个KeyGenerator对象
|
|
return (target, method, params) -> {
|
|
return (target, method, params) -> {
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(target.getClass().getName());
|
|
sb.append(target.getClass().getName());
|
|
@@ -139,58 +143,85 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 短信验证使用的redis
|
|
* 短信验证使用的redis
|
|
*
|
|
*
|
|
- * @param factory
|
|
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
|
|
+ @Bean("factoryForIdc")
|
|
|
|
+ public LettuceConnectionFactory redisConnectionFactoryForIdc() {
|
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseIdc));
|
|
|
|
+ }
|
|
|
|
+
|
|
@Bean(name = "redisTemplateForIdc")
|
|
@Bean(name = "redisTemplateForIdc")
|
|
- public RedisTemplate<String, Object> redisTemplateForIdc(JedisConnectionFactory factory) {
|
|
|
|
- return getRedisTemplate(factory, Integer.valueOf(databaseIdc));
|
|
|
|
|
|
+ public RedisTemplate<String, Object> redisTemplateForIdc(@Qualifier("factoryForIdc") LettuceConnectionFactory factory) {
|
|
|
|
+ return getRedisTemplate(factory);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 短信验证使用的redis
|
|
* 短信验证使用的redis
|
|
*
|
|
*
|
|
- * @param factory
|
|
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
|
|
+ @Bean("factoryForSms")
|
|
|
|
+ public LettuceConnectionFactory redisConnectionFactoryForSms() {
|
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseSms));
|
|
|
|
+ }
|
|
|
|
+
|
|
@Bean(name = "redisTemplateForSms")
|
|
@Bean(name = "redisTemplateForSms")
|
|
- public RedisTemplate<String, Object> redisTemplateForSms(JedisConnectionFactory factory) {
|
|
|
|
- return getRedisTemplate(factory, Integer.valueOf(databaseSms));
|
|
|
|
|
|
+ public RedisTemplate<String, Object> redisTemplateForSms(@Qualifier("factoryForSms") LettuceConnectionFactory factory) {
|
|
|
|
+ return getRedisTemplate(factory);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 图片验证使用的redis
|
|
* 图片验证使用的redis
|
|
*
|
|
*
|
|
- * @param factory
|
|
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
|
|
+ @Bean("factoryForImg")
|
|
|
|
+ public LettuceConnectionFactory redisConnectionFactoryForImg() {
|
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseImg));
|
|
|
|
+ }
|
|
|
|
+
|
|
@Bean(name = "redisTemplateForImg")
|
|
@Bean(name = "redisTemplateForImg")
|
|
- public RedisTemplate<String, Object> redisTemplateForImg(JedisConnectionFactory factory) {
|
|
|
|
- return getRedisTemplate(factory, Integer.valueOf(databaseImg));
|
|
|
|
|
|
+ public RedisTemplate<String, Object> redisTemplateForImg(@Qualifier("factoryForImg") LettuceConnectionFactory factory) {
|
|
|
|
+ return getRedisTemplate(factory);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Token使用的redis
|
|
* Token使用的redis
|
|
*
|
|
*
|
|
- * @param factory
|
|
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
|
|
+ @Bean("factoryForToken")
|
|
|
|
+ public LettuceConnectionFactory redisConnectionFactoryForToken() {
|
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseToken));
|
|
|
|
+ }
|
|
|
|
+
|
|
@Bean(name = "redisTemplateForToken")
|
|
@Bean(name = "redisTemplateForToken")
|
|
- public RedisTemplate<String, Object> redisTemplateForToken(JedisConnectionFactory factory) {
|
|
|
|
- return getRedisTemplate(factory, Integer.valueOf(databaseToken));
|
|
|
|
|
|
+ public RedisTemplate<String, Object> redisTemplateForToken(@Qualifier("factoryForToken") LettuceConnectionFactory factory) {
|
|
|
|
+ return getRedisTemplate(factory);
|
|
}
|
|
}
|
|
|
|
|
|
- private RedisTemplate<String, Object> getRedisTemplate(JedisConnectionFactory factory, Integer database) {
|
|
|
|
- JedisConnectionFactory factory2 = new JedisConnectionFactory();
|
|
|
|
- BeanUtil.copyProperties(factory, factory2);
|
|
|
|
- factory2.setDatabase(database);
|
|
|
|
|
|
+ private LettuceConnectionFactory getRedisConnectionFactory(Integer database) {
|
|
|
|
+ RedisStandaloneConfiguration connection = new RedisStandaloneConfiguration();
|
|
|
|
+ connection.setHostName(host);
|
|
|
|
+ connection.setPort(port);
|
|
|
|
+ connection.setPassword(password);
|
|
|
|
+ connection.setDatabase(database);
|
|
|
|
+ GenericObjectPoolConfig poolConfig = getRedisConfig();
|
|
|
|
+ LettuceClientConfiguration builder = LettucePoolingClientConfiguration.builder()
|
|
|
|
+ .commandTimeout(Duration.ofMillis(timeout))
|
|
|
|
+ .poolConfig(poolConfig)
|
|
|
|
+ .shutdownTimeout(Duration.ZERO)
|
|
|
|
+ .build();
|
|
|
|
+ LettuceConnectionFactory factory = new LettuceConnectionFactory(connection, builder);
|
|
|
|
+ return factory;
|
|
|
|
+ }
|
|
|
|
|
|
- RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
|
|
|
|
- redisTemplate.setConnectionFactory(factory2);
|
|
|
|
|
|
+ private RedisTemplate<String, Object> getRedisTemplate(LettuceConnectionFactory factory) {
|
|
|
|
+ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
|
|
|
+ redisTemplate.setConnectionFactory(factory);
|
|
|
|
|
|
// value值的序列化
|
|
// value值的序列化
|
|
redisTemplate.setValueSerializer(getSerializer());
|
|
redisTemplate.setValueSerializer(getSerializer());
|