From 791e7f0d061671879cbc1db13fbd8b5f3eb41dc0 Mon Sep 17 00:00:00 2001 From: UNGGU0704 Date: Tue, 17 Jun 2025 14:54:56 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20store=20MSA=20Redis=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ktds/hi/store/config/RedisConfig.java | 72 ++++++++++++++++--- .../gateway/ExternalPlatformAdapter.java | 2 + 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/store/src/main/java/com/ktds/hi/store/config/RedisConfig.java b/store/src/main/java/com/ktds/hi/store/config/RedisConfig.java index 7bb997a..51b459e 100644 --- a/store/src/main/java/com/ktds/hi/store/config/RedisConfig.java +++ b/store/src/main/java/com/ktds/hi/store/config/RedisConfig.java @@ -1,46 +1,96 @@ package com.ktds.hi.store.config; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; 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.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; +/** + * Redis 설정 클래스 (Store MSA) + * Redis 연결 및 템플릿 설정을 담당 + */ +@Slf4j @Configuration +@RequiredArgsConstructor public class RedisConfig { - @Value("${spring.data.redis.host:localhost}") + @Value("${spring.data.redis.host}") private String redisHost; - @Value("${spring.data.redis.port:6379}") + @Value("${spring.data.redis.port}") private int redisPort; + @Value("${spring.data.redis.password:}") + private String redisPassword; + + @Value("${spring.data.redis.database:0}") + private int redisDatabase; + + private final ObjectMapper objectMapper; + + /** + * Redis 연결 팩토리 설정 (견고한 연결 관리) + */ @Bean public RedisConnectionFactory redisConnectionFactory() { - return new LettuceConnectionFactory(redisHost, redisPort); + log.info("Redis 연결 설정: host={}, port={}, database={}", redisHost, redisPort, redisDatabase); + + RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort); + config.setDatabase(redisDatabase); + + if (redisPassword != null && !redisPassword.trim().isEmpty()) { + config.setPassword(redisPassword); + log.info("Redis 비밀번호 설정됨"); + } + + LettuceConnectionFactory factory = new LettuceConnectionFactory(config); + + // 연결 검증 활성화 + factory.setValidateConnection(true); + + return factory; } + /** + * RedisTemplate 설정 (JSON 직렬화 포함) + */ @Bean - public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { + public RedisTemplate redisTemplate() { RedisTemplate template = new RedisTemplate<>(); - template.setConnectionFactory(connectionFactory); + template.setConnectionFactory(redisConnectionFactory()); - // 직렬화 설정 + // Key Serializer template.setKeySerializer(new StringRedisSerializer()); - template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); - template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); + // Value Serializer (ObjectMapper 사용) + GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer(objectMapper); + template.setValueSerializer(jsonSerializer); + template.setHashValueSerializer(jsonSerializer); + + template.setDefaultSerializer(jsonSerializer); template.afterPropertiesSet(); + + log.info("RedisTemplate 설정 완료"); return template; } + /** + * StringRedisTemplate 설정 (단순 문자열 작업용) + */ @Bean - public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) { - return new StringRedisTemplate(connectionFactory); + public StringRedisTemplate stringRedisTemplate() { + StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory()); + log.info("StringRedisTemplate 설정 완료"); + return template; } -} +} \ No newline at end of file diff --git a/store/src/main/java/com/ktds/hi/store/infra/gateway/ExternalPlatformAdapter.java b/store/src/main/java/com/ktds/hi/store/infra/gateway/ExternalPlatformAdapter.java index 19b78f9..61f4533 100644 --- a/store/src/main/java/com/ktds/hi/store/infra/gateway/ExternalPlatformAdapter.java +++ b/store/src/main/java/com/ktds/hi/store/infra/gateway/ExternalPlatformAdapter.java @@ -212,6 +212,7 @@ public class ExternalPlatformAdapter implements ExternalPlatformPort { if (!parsedReviews.isEmpty()) { // 🔥 Redis에 저장 + log.info("Redis에 리뷰 데이터 저장 진행 중"); String redisKey = String.format("external:reviews:pending:%d:%s:%d", storeId, platform, System.currentTimeMillis()); @@ -235,6 +236,7 @@ public class ExternalPlatformAdapter implements ExternalPlatformPort { } catch (Exception e) { log.error("카카오 응답 파싱 및 Redis 저장 실패: storeId={}, error={}", storeId, e.getMessage()); + e.printStackTrace(); updateSyncStatus(storeId, platform, "FAILED", 0); return 0; }