|  |  |  | 
|---|
|  |  |  | package com.moral.api.config.redis; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.time.Duration; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.fasterxml.jackson.annotation.JsonAutoDetect; | 
|---|
|  |  |  | import com.fasterxml.jackson.annotation.PropertyAccessor; | 
|---|
|  |  |  | import com.fasterxml.jackson.databind.ObjectMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.apache.commons.pool2.impl.GenericObjectPoolConfig; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Value; | 
|---|
|  |  |  | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | 
|---|
|  |  |  | 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.RedisPassword; | 
|---|
|  |  |  | import org.springframework.data.redis.connection.RedisStandaloneConfiguration; | 
|---|
|  |  |  | 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.StringRedisTemplate; | 
|---|
|  |  |  | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Configuration | 
|---|
|  |  |  | public class RedisConfig { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean(name="redisTemplate") | 
|---|
|  |  |  | @Bean(name = "redisTemplate") | 
|---|
|  |  |  | @ConditionalOnMissingBean(StringRedisTemplate.class)  //此注解的作用  如果容器中没有RedisTemplate 那就注入    有就不注入了 | 
|---|
|  |  |  | public RedisTemplate<String,Object> stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { | 
|---|
|  |  |  | public RedisTemplate<String, Object> stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { | 
|---|
|  |  |  | RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); | 
|---|
|  |  |  | redisTemplate.setConnectionFactory(redisConnectionFactory); | 
|---|
|  |  |  | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); | 
|---|
|  |  |  | 
|---|
|  |  |  | redisTemplate.afterPropertiesSet(); | 
|---|
|  |  |  | return redisTemplate; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean(name = "tokenRedisTemplate") | 
|---|
|  |  |  | public RedisTemplate<String, Object> getTokenRedisTemplate( | 
|---|
|  |  |  | @Value("${spring.tokenRedis.host}") String hostName, | 
|---|
|  |  |  | @Value("${spring.tokenRedis.password}") String password, | 
|---|
|  |  |  | @Value("${spring.tokenRedis.port}") int port, | 
|---|
|  |  |  | @Value("${spring.tokenRedis.database}") int database, | 
|---|
|  |  |  | @Value("${spring.tokenRedis.pool.max-active}") int maxActive, | 
|---|
|  |  |  | @Value("${spring.tokenRedis.pool.max-idle}") int maxIdle, | 
|---|
|  |  |  | @Value("${spring.tokenRedis.pool.min-idle}") int minIdle, | 
|---|
|  |  |  | @Value("${spring.tokenRedis.pool.max-wait}") long maxWait, | 
|---|
|  |  |  | @Value("${spring.tokenRedis.timeout}") long timeout | 
|---|
|  |  |  | ) { | 
|---|
|  |  |  | RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); | 
|---|
|  |  |  | configuration.setHostName(hostName); | 
|---|
|  |  |  | configuration.setPort(port); | 
|---|
|  |  |  | configuration.setDatabase(database); | 
|---|
|  |  |  | RedisPassword redisPassword = RedisPassword.of(password); | 
|---|
|  |  |  | configuration.setPassword(redisPassword); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); | 
|---|
|  |  |  | genericObjectPoolConfig.setMaxTotal(maxActive); | 
|---|
|  |  |  | genericObjectPoolConfig.setMinIdle(minIdle); | 
|---|
|  |  |  | genericObjectPoolConfig.setMaxIdle(maxIdle); | 
|---|
|  |  |  | genericObjectPoolConfig.setMaxWaitMillis(maxWait); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder(); | 
|---|
|  |  |  | builder.poolConfig(genericObjectPoolConfig); | 
|---|
|  |  |  | builder.commandTimeout(Duration.ofSeconds(timeout)); | 
|---|
|  |  |  | LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build()); | 
|---|
|  |  |  | connectionFactory.afterPropertiesSet(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return createRedisTemplate(connectionFactory); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory) { | 
|---|
|  |  |  | RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | 
|---|
|  |  |  | redisTemplate.setConnectionFactory(redisConnectionFactory); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); | 
|---|
|  |  |  | ObjectMapper objectMapper = new ObjectMapper(); | 
|---|
|  |  |  | objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | 
|---|
|  |  |  | objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | 
|---|
|  |  |  | jackson2JsonRedisSerializer.setObjectMapper(objectMapper); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); | 
|---|
|  |  |  | // key采用String的序列化方式 | 
|---|
|  |  |  | redisTemplate.setKeySerializer(stringRedisSerializer); | 
|---|
|  |  |  | // hash的key也采用String的序列化方式 | 
|---|
|  |  |  | redisTemplate.setHashKeySerializer(stringRedisSerializer); | 
|---|
|  |  |  | // valuevalue采用jackson序列化方式 | 
|---|
|  |  |  | redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); | 
|---|
|  |  |  | // hash的value采用jackson序列化方式 | 
|---|
|  |  |  | redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); | 
|---|
|  |  |  | redisTemplate.afterPropertiesSet(); | 
|---|
|  |  |  | redisTemplate.afterPropertiesSet(); | 
|---|
|  |  |  | return redisTemplate; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|