|
| 1 | +package cn.abel.queue.config; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| 4 | +import com.fasterxml.jackson.annotation.PropertyAccessor; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.beans.factory.annotation.Value; |
| 10 | +import org.springframework.cache.annotation.CachingConfigurerSupport; |
| 11 | +import org.springframework.cache.annotation.EnableCaching; |
| 12 | +import org.springframework.cache.interceptor.KeyGenerator; |
| 13 | +import org.springframework.context.annotation.Bean; |
| 14 | +import org.springframework.context.annotation.Configuration; |
| 15 | +import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 16 | +import org.springframework.data.redis.connection.RedisPassword; |
| 17 | +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; |
| 18 | +import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; |
| 19 | +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
| 20 | +import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; |
| 21 | +import org.springframework.data.redis.core.RedisTemplate; |
| 22 | +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; |
| 23 | +import org.springframework.data.redis.serializer.StringRedisSerializer; |
| 24 | + |
| 25 | +import java.time.Duration; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author yyb |
| 29 | + * @time 2019/8/13 |
| 30 | + */ |
| 31 | +@Configuration |
| 32 | +@EnableCaching |
| 33 | +public class RedisConfig extends CachingConfigurerSupport { |
| 34 | + /** |
| 35 | + * logger |
| 36 | + */ |
| 37 | + private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class); |
| 38 | + @Value("${spring.redis.database}") |
| 39 | + private Integer database; |
| 40 | + @Value("${spring.redis.host}") |
| 41 | + private String host; |
| 42 | + @Value("${spring.redis.port}") |
| 43 | + private Integer port; |
| 44 | + @Value("${spring.redis.password}") |
| 45 | + private String password; |
| 46 | + @Value("${spring.redis.lettuce.pool.max-active}") |
| 47 | + private Integer maxActive; |
| 48 | + @Value("${spring.redis.lettuce.pool.max-wait}") |
| 49 | + private Integer maxWait; |
| 50 | + @Value("${spring.redis.lettuce.pool.max-idle}") |
| 51 | + private Integer maxIdle; |
| 52 | + @Value("${spring.redis.lettuce.pool.min-idle}") |
| 53 | + private Integer minIdle; |
| 54 | + @Value("${spring.redis.lettuce.shutdown-timeout}") |
| 55 | + private Integer timeout; |
| 56 | + |
| 57 | + /** |
| 58 | + * 在使用@Cacheable时,如果不指定key,则使用这个默认的key生成器生成的key |
| 59 | + * |
| 60 | + * @return |
| 61 | + */ |
| 62 | + @Override |
| 63 | + @Bean |
| 64 | + public KeyGenerator keyGenerator() { |
| 65 | + return (target, method, params) -> { |
| 66 | + StringBuilder sb = new StringBuilder(); |
| 67 | + sb.append(target.getClass().getName()); |
| 68 | + sb.append(method.getName()); |
| 69 | + for (Object obj : params) { |
| 70 | + sb.append(obj.toString()); |
| 71 | + } |
| 72 | + return sb.toString(); |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + @Bean(name = "redisTemplate") |
| 77 | + public RedisTemplate<Object, Object> redisTemplate() { |
| 78 | + return getTemplate(redisConnectionFactory()); |
| 79 | + } |
| 80 | + |
| 81 | + private RedisConnectionFactory redisConnectionFactory() { |
| 82 | + return connectionFactory(maxActive, maxIdle, minIdle, maxWait, host, password, timeout, port, database); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * 创建连接 |
| 88 | + * |
| 89 | + * @param maxActive |
| 90 | + * @param maxIdle |
| 91 | + * @param minIdle |
| 92 | + * @param maxWait |
| 93 | + * @param host |
| 94 | + * @param password |
| 95 | + * @param timeout |
| 96 | + * @param port |
| 97 | + * @param database |
| 98 | + * @return |
| 99 | + */ |
| 100 | + private RedisConnectionFactory connectionFactory(Integer maxActive, |
| 101 | + Integer maxIdle, |
| 102 | + Integer minIdle, |
| 103 | + Integer maxWait, |
| 104 | + String host, |
| 105 | + String password, |
| 106 | + Integer timeout, |
| 107 | + Integer port, |
| 108 | + Integer database) { |
| 109 | + RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); |
| 110 | + redisStandaloneConfiguration.setHostName(host); |
| 111 | + redisStandaloneConfiguration.setPort(port); |
| 112 | + redisStandaloneConfiguration.setDatabase(database); |
| 113 | + redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); |
| 114 | + |
| 115 | + GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); |
| 116 | + poolConfig.setMaxTotal(maxActive); |
| 117 | + poolConfig.setMinIdle(minIdle); |
| 118 | + poolConfig.setMaxIdle(maxIdle); |
| 119 | + poolConfig.setMaxWaitMillis(maxWait); |
| 120 | + LettuceClientConfiguration lettucePoolingConfig = LettucePoolingClientConfiguration.builder() |
| 121 | + .poolConfig(poolConfig).shutdownTimeout(Duration.ofMillis(timeout)).build(); |
| 122 | + LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration, |
| 123 | + lettucePoolingConfig); |
| 124 | + connectionFactory.afterPropertiesSet(); |
| 125 | + |
| 126 | + return connectionFactory; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * 创建 RedisTemplate 连接类型,此处为hash |
| 131 | + * |
| 132 | + * @param factory |
| 133 | + * @return |
| 134 | + */ |
| 135 | + private RedisTemplate<Object, Object> getTemplate(RedisConnectionFactory factory) { |
| 136 | + RedisTemplate<Object, Object> template = new RedisTemplate<>(); |
| 137 | + template.setConnectionFactory(factory); |
| 138 | + template.setValueSerializer(jackson2JsonRedisSerializer(new ObjectMapper())); |
| 139 | + template.setKeySerializer(new StringRedisSerializer()); |
| 140 | + template.setHashKeySerializer(new StringRedisSerializer()); |
| 141 | + template.setHashValueSerializer(jackson2JsonRedisSerializer(new ObjectMapper())); |
| 142 | + |
| 143 | + template.afterPropertiesSet(); |
| 144 | + return template; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * 对value 进行序列化 |
| 149 | + * |
| 150 | + * @param objectMapper |
| 151 | + * @return |
| 152 | + */ |
| 153 | + private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(ObjectMapper objectMapper) { |
| 154 | + Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = |
| 155 | + new Jackson2JsonRedisSerializer<>(Object.class); |
| 156 | + jackson2JsonRedisSerializer.setObjectMapper(objectMapper); |
| 157 | + objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
| 158 | + objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
| 159 | + return jackson2JsonRedisSerializer; |
| 160 | + } |
| 161 | +} |
0 commit comments