@Configuration
public class RedisConfig {
@Value("${redis.cluster.nodes}")
List<String> redisNodeList;
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
@Bean
public RedisConnectionFactory prdRedisConnectionFactory() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisNodeList);
// topology 자동 업데이트 옵션 추가
// enablePeriodicRefresh(topology 정보 감시 주기) default value : 60s
ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
.enableAllAdaptiveRefreshTriggers() // MOVED, ASK, PERSISTENT_RECONNECTS, UNCOVERED_SLOT, UNKOWN_NODE trigger시 refresh 수행
.enablePeriodicRefresh(Duration.ofMinutes(30L)) // topology 정보 감시 주기 설정 -> 30분
.build();
ClientOptions clientOptions = ClusterClientOptions.builder()
.topologyRefreshOptions(clusterTopologyRefreshOptions)
.build();
// topology 옵션 및 timeout 세팅
LettuceClientConfiguration clientConfigurations = LettuceClientConfiguration.builder()
.commandTimeout(Duration.ofSeconds(30L)) // command Timeout설정 -> 30초
.clientOptions(clientOptions)
.build();
return new LettuceConnectionFactory(redisClusterConfiguration, clientConfigurations);
}
@Bean
public ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
}