配置文件:
1 import com.fasterxml.jackson.annotation.JsonAutoDetect; 2 import com.fasterxml.jackson.annotation.PropertyAccessor; 3 import com.fasterxml.jackson.databind.ObjectMapper; 4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.data.redis.connection.RedisConnectionFactory; 8 import org.springframework.data.redis.core.RedisTemplate; 9 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 10 import org.springframework.data.redis.serializer.StringRedisSerializer; 11 import redis.clients.jedis.JedisPool; 12 import redis.clients.jedis.JedisPoolConfig; 13 14 /** 15 * @ClassName RedisConfiguration 16 * @Description 17 * @Author wqb 18 * @CreateTime 2021-6-29 11:57:45 19 * @Version 1.0 20 **/ 21 22 @Configuration 23 public class RedisConfig{ 24 // extends CachingConfigurerSupport 25 @Value("${spring.redis.host}") 26 private String host; 27 28 @Value("${spring.redis.port}") 29 private Integer port; 30 31 @Value("${spring.redis.password}") 32 private String password; 33 34 @Value("${spring.redis.database}") 35 private Integer database; 36 37 @Value("${spring.redis.timeout}") 38 private Integer timeout; 39 40 @Value("${spring.redis.testOnBorrow}") 41 private Boolean testOnBorrow; 42 43 @Value("${spring.redis.testOnReturn}") 44 private Boolean testOnReturn; 45 46 @Value("${spring.redis.testOnCreate: true}") 47 private Boolean testOnCreate; 48 49 @Value("${spring.redis.enableTransactionSupport: true}") 50 private Boolean enableTransactionSupport; 51 52 @Value("${spring.redis.lettuce.pool.max-active}") 53 private Integer maxActive; 54 55 @Value("${spring.redis.lettuce.pool.max-wait}") 56 private Long maxWait; 57 58 @Value("${spring.redis.lettuce.pool.min-idle}") 59 private Integer minIdle; 60 61 @Value("${spring.redis.lettuce.pool.max-idle}") 62 private Integer maxIdle; 63 64 65 @Bean 66 public JedisPoolConfig getJedisPoolConfig(){ 67 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 68 jedisPoolConfig.setMaxIdle(maxIdle); 69 jedisPoolConfig.setMinIdle(minIdle); 70 jedisPoolConfig.setMaxTotal(maxActive); 71 jedisPoolConfig.setTestOnCreate(testOnCreate); 72 jedisPoolConfig.setTestOnBorrow(testOnBorrow); 73 jedisPoolConfig.setTestOnReturn(testOnReturn); 74 jedisPoolConfig.setMaxWaitMillis(maxWait); 75 return jedisPoolConfig; 76 } 77 78 @Bean 79 public JedisPool getJedisPool(JedisPoolConfig jedisPoolConfig){ 80 JedisPool jedisPool = new JedisPool(jedisPoolConfig,host, port, timeout, password, database); 81 return jedisPool; 82 } 83 84 85 @Bean 86 public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { 87 RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); 88 redisTemplate.setConnectionFactory(connectionFactory); 89 90 // 使用Jackson2JsonRedisSerialize替换默认序列化 91 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 92 93 ObjectMapper objectMapper = new ObjectMapper(); 94 objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 95 objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 96 97 jackson2JsonRedisSerializer.setObjectMapper(objectMapper); 98 99 // 设置key和value的序列化规则 100 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); 101 redisTemplate.setKeySerializer(new StringRedisSerializer()); 102 redisTemplate.afterPropertiesSet(); 103 104 return redisTemplate; 105 } 106 107 108 109 // @Bean 110 // LettuceConnectionFactory lettuceConnectionFactory() { 111 // // 连接池配置 112 // GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); 113 // poolConfig.setMaxIdle(maxIdle == null ? 8 : maxIdle); 114 // poolConfig.setMinIdle(minIdle == null ? 1 : minIdle); 115 // poolConfig.setMaxTotal(maxActive == null ? 8 : maxActive); 116 // poolConfig.setMaxWaitMillis(maxWait == null ? 5000L : maxWait); 117 // poolConfig.setTestOnBorrow(testOnBorrow); 118 // poolConfig.setTestOnReturn(testOnReturn); 119 // LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder() 120 // .poolConfig(poolConfig) 121 // .build(); 122 // // 单机redis 123 // RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(); 124 // redisConfig.setDatabase(database); 125 // redisConfig.setHostName(host); 126 // redisConfig.setPort(port); 127 // if (password != null && !"".equals(password)) { 128 // redisConfig.setPassword(password); 129 // } 130 // 131 // // 哨兵redis 132 // // RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration(); 133 // 134 //// // 集群redis 135 //// RedisClusterConfiguration redisConfig = new RedisClusterConfiguration(); 136 //// Set<RedisNode> nodeses = new HashSet<>(); 137 //// String[] hostses = nodes.split("-"); 138 //// for (String h : hostses) { 139 //// h = h.replaceAll("\\s", "").replaceAll("\n", ""); 140 //// if (!"".equals(h)) { 141 //// String host = h.split(":")[0]; 142 //// int port = Integer.valueOf(h.split(":")[1]); 143 //// nodeses.add(new RedisNode(host, port)); 144 //// } 145 //// } 146 //// redisConfig.setClusterNodes(nodeses); 147 // // 跨集群执行命令时要遵循的最大重定向数量 148 //// redisConfig.setMaxRedirects(3); 149 // redisConfig.setPassword(password); 150 // 151 // return new LettuceConnectionFactory(redisConfig, lettucePoolingClientConfiguration); 152 // } 153 154 // /** 155 // * 自定义缓存key的生成策略。默认的生成策略是看不懂的(乱码内容) 156 // * 通过Spring 的依赖注入特性进行自定义的配置注入并且此类是一个配置类可以更多程度的自定义配置 157 // * @return 158 // */ 159 // @Bean 160 // @Override 161 // public KeyGenerator keyGenerator() { 162 // return new KeyGenerator() { 163 // @Override 164 // public Object generate(Object target, Method method, Object... params) { 165 // StringBuilder sb = new StringBuilder(); 166 // sb.append(target.getClass().getName()); 167 // sb.append(method.getName()); 168 // for (Object obj : params) { 169 // sb.append(obj.toString()); 170 // } 171 // return sb.toString(); 172 // } 173 // }; 174 // } 175 // 176 // /** 177 // * 缓存配置管理器 178 // */ 179 // @Bean 180 // public CacheManager cacheManager(LettuceConnectionFactory lettuceConnectionFactory) { 181 // //以锁写入的方式创建RedisCacheWriter对象 182 // RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(lettuceConnectionFactory); 183 // //创建默认缓存配置对象 184 // RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); 185 // RedisCacheManager cacheManager = new RedisCacheManager(writer, config); 186 // return cacheManager; 187 // } 188 // 189 // 190 191 // @Bean 192 // public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { 193 // RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); 194 // redisTemplate.setConnectionFactory(lettuceConnectionFactory); 195 // 196 // // 使用 Jackson2JsonRedisSerialize 替换默认序列化 JDKSerializer 197 // Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 198 // 199 // ObjectMapper objectMapper = new ObjectMapper(); 200 // objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 201 // objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 202 // 203 // jackson2JsonRedisSerializer.setObjectMapper(objectMapper); 204 // 205 // // 设置key和value的序列化规则 206 // redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); 207 // redisTemplate.setKeySerializer(new StringRedisSerializer()); 208 // redisTemplate.afterPropertiesSet(); 209 // redisTemplate.setEnableTransactionSupport(enableTransactionSupport); 210 // return redisTemplate; 211 // } 212 }