|
@@ -0,0 +1,112 @@
|
|
|
+package com.diagbot.config;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
+import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+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.serializer.Jackson2JsonRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
+
|
|
|
+import java.time.Duration;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@Slf4j
|
|
|
+public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
+
|
|
|
+ @Value("${spring.redis.database.mr}")
|
|
|
+ private String databaseMr;
|
|
|
+ @Value("${spring.redis.host}")
|
|
|
+ private String host;
|
|
|
+ @Value("${spring.redis.password}")
|
|
|
+ private String password;
|
|
|
+ @Value("${spring.redis.port}")
|
|
|
+ private int port;
|
|
|
+ @Value("${spring.redis.timeout}")
|
|
|
+ private int timeout;
|
|
|
+ @Value("${spring.redis.lettuce.pool.max-active}")
|
|
|
+ private int maxActive;
|
|
|
+ @Value("${spring.redis.lettuce.pool.max-idle}")
|
|
|
+ private int maxIdle;
|
|
|
+ @Value("${spring.redis.lettuce.pool.max-wait}")
|
|
|
+ private long maxWaitMillis;
|
|
|
+ @Value("${spring.redis.lettuce.pool.min-idle}")
|
|
|
+ private int minIdle;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public GenericObjectPoolConfig getRedisConfig() {
|
|
|
+ GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
|
|
|
+ poolConfig.setMaxTotal(maxActive);
|
|
|
+ poolConfig.setMaxIdle(maxIdle);
|
|
|
+ poolConfig.setMaxWaitMillis(maxWaitMillis);
|
|
|
+ poolConfig.setMinIdle(minIdle);
|
|
|
+ return poolConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Jackson2JsonRedisSerializer getSerializer() {
|
|
|
+ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
|
+ ObjectMapper om = new ObjectMapper();
|
|
|
+ om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
+ om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
+ jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
|
+ return jackson2JsonRedisSerializer;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 电子病历使用的redis
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean("factoryForMr")
|
|
|
+ public LettuceConnectionFactory redisConnectionFactoryForIdc() {
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseMr));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "redisTemplateForMr")
|
|
|
+ public RedisTemplate<String, Object> redisTemplateForIdc(@Qualifier("factoryForMr") LettuceConnectionFactory factory) {
|
|
|
+ return getRedisTemplate(factory);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ private RedisTemplate<String, Object> getRedisTemplate(LettuceConnectionFactory factory) {
|
|
|
+ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
|
|
+ redisTemplate.setConnectionFactory(factory);
|
|
|
+
|
|
|
+ // value值的序列化
|
|
|
+ redisTemplate.setValueSerializer(getSerializer());
|
|
|
+ redisTemplate.setHashValueSerializer(getSerializer());
|
|
|
+ // key的序列化采用StringRedisSerializer
|
|
|
+ redisTemplate.setKeySerializer(new StringRedisSerializer());
|
|
|
+ redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
|
|
+ redisTemplate.afterPropertiesSet();
|
|
|
+ return redisTemplate;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|