@EnableCaching
@Configuration
public class RedisConfiguration extends CachingConfigurerSupport {
@Autowired
private RedisClusterProperties redisClusterProperties;
@Bean
public RedisConnectionFactory connectionFactory() {
String nodes = redisClusterProperties.getNodes();
List<String> nodeList = new ArrayList<>();
String[] split = nodes.split(",");
for (String node : split) {
nodeList.add(node);
}
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(redisClusterProperties.getMaxActive());
config.setMaxIdle(redisClusterProperties.getMaxIdle());
config.setMinIdle(redisClusterProperties.getMinIdle());
config.setMaxWaitMillis(redisClusterProperties.getMaxWaitMillis());
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.poolConfig(config)
.commandTimeout(Duration.ofMillis(redisClusterProperties.getTimeout()))
.build();
RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(nodeList);
clusterConfig.setMaxRedirects(3);
return new LettuceConnectionFactory(
clusterConfig,clientConfig);
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory),
this.getRedisCacheConfigurationWithTtl(3600),
this.getRedisCacheConfigurationMap()
);
}
@Bean
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuffer sb = new StringBuffer();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {
// 配置redisTemplate
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.QuoteFieldNames);
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
fastJsonRedisSerializer.setFastJsonConfig(config);
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
//GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
redisTemplate.setDefaultSerializer(fastJsonRedisSerializer);
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(fastJsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
return redisTemplate;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.QuoteFieldNames);
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
fastJsonRedisSerializer.setFastJsonConfig(config);
//GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(fastJsonRedisSerializer)
).entryTtl(Duration.ofSeconds(seconds));
return redisCacheConfiguration;
}
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
//redisCacheConfigurationMap.put("city:shard", this.getRedisCacheConfigurationWithTtl(43200));
//redisCacheConfigurationMap.put("city:route:conf", this.getRedisCacheConfigurationWithTtl(43200));
//redisCacheConfigurationMap.put("city:node", this.getRedisCacheConfigurationWithTtl(43200));
return redisCacheConfigurationMap;
}
}
redis.cluster.nodes=10.202.114.65:9168,10.202.114.65:9169,10.202.114.65:9170
redis.cluster.timeout=5000
redis.cluster.maxIdle=10
redis.cluster.minIdle=2
redis.cluster.maxActive=100
redis.cluster.maxWaitMillis=5000