From b5e377a99768bb9bb4d850d267a8b58d0c636206 Mon Sep 17 00:00:00 2001 From: pie Date: Mon, 30 Dec 2024 18:27:15 +0900 Subject: [PATCH] =?UTF-8?q?feat=20#85=20:=20redis=20sentinel=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infrastructure/config/RedisConfig.java | 42 ++++++++++++------- src/main/resources/application-dev.yml | 5 ++- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/main/java/pie/tomato/tomatomarket/infrastructure/config/RedisConfig.java b/src/main/java/pie/tomato/tomatomarket/infrastructure/config/RedisConfig.java index 9b20af116..a2af64465 100644 --- a/src/main/java/pie/tomato/tomatomarket/infrastructure/config/RedisConfig.java +++ b/src/main/java/pie/tomato/tomatomarket/infrastructure/config/RedisConfig.java @@ -1,9 +1,13 @@ package pie.tomato.tomatomarket.infrastructure.config; +import java.util.stream.Collectors; + import org.springframework.boot.autoconfigure.data.redis.RedisProperties; 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.RedisNode; +import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @@ -14,19 +18,27 @@ @RequiredArgsConstructor public class RedisConfig { - private final RedisProperties redisProperties; - - @Bean - public RedisConnectionFactory redisConnectionFactory() { - return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort()); - } - - @Bean - public RedisTemplate redisTemplate() { - RedisTemplate redisTemplate = new RedisTemplate<>(); - redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.setValueSerializer(new StringRedisSerializer()); - redisTemplate.setConnectionFactory(redisConnectionFactory()); - return redisTemplate; - } + private final RedisProperties redisProperties; + + @Bean + public RedisConnectionFactory redisConnectionFactory() { + var nodes = redisProperties.getSentinel().getNodes().stream().map(node -> { + String[] hostAndPort = node.split(":"); + return new RedisNode(hostAndPort[0], Integer.parseInt(hostAndPort[1])); + }).collect(Collectors.toList()); + var sentinelConfiguration = new RedisSentinelConfiguration() + .master(redisProperties.getSentinel().getMaster()); + sentinelConfiguration.setSentinels(nodes); + + return new LettuceConnectionFactory(sentinelConfiguration); + } + + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new StringRedisSerializer()); + redisTemplate.setConnectionFactory(redisConnectionFactory()); + return redisTemplate; + } } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 80c2305b4..dd92b0b17 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -4,8 +4,9 @@ spring: username: ${db.dev.datasource.username} password: ${db.dev.datasource.password} redis: - host: localhost - port: 6379 + sentinel: + master: mymaster + nodes: localhost:25379,localhost:25380,localhost:25381 cloud: aws: