Fix: store MSA Redis 설정

This commit is contained in:
UNGGU0704 2025-06-17 14:54:56 +09:00
parent 75c4df6436
commit 791e7f0d06
2 changed files with 63 additions and 11 deletions

View File

@ -1,46 +1,96 @@
package com.ktds.hi.store.config; 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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory; 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.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis 설정 클래스 (Store MSA)
* Redis 연결 템플릿 설정을 담당
*/
@Slf4j
@Configuration @Configuration
@RequiredArgsConstructor
public class RedisConfig { public class RedisConfig {
@Value("${spring.data.redis.host:localhost}") @Value("${spring.data.redis.host}")
private String redisHost; private String redisHost;
@Value("${spring.data.redis.port:6379}") @Value("${spring.data.redis.port}")
private int redisPort; 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 @Bean
public RedisConnectionFactory redisConnectionFactory() { 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 @Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory); template.setConnectionFactory(redisConnectionFactory());
// 직렬화 설정 // Key Serializer
template.setKeySerializer(new StringRedisSerializer()); template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer()); 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(); template.afterPropertiesSet();
log.info("RedisTemplate 설정 완료");
return template; return template;
} }
/**
* StringRedisTemplate 설정 (단순 문자열 작업용)
*/
@Bean @Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) { public StringRedisTemplate stringRedisTemplate() {
return new StringRedisTemplate(connectionFactory); StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory());
log.info("StringRedisTemplate 설정 완료");
return template;
} }
} }

View File

@ -212,6 +212,7 @@ public class ExternalPlatformAdapter implements ExternalPlatformPort {
if (!parsedReviews.isEmpty()) { if (!parsedReviews.isEmpty()) {
// 🔥 Redis에 저장 // 🔥 Redis에 저장
log.info("Redis에 리뷰 데이터 저장 진행 중");
String redisKey = String.format("external:reviews:pending:%d:%s:%d", String redisKey = String.format("external:reviews:pending:%d:%s:%d",
storeId, platform, System.currentTimeMillis()); storeId, platform, System.currentTimeMillis());
@ -235,6 +236,7 @@ public class ExternalPlatformAdapter implements ExternalPlatformPort {
} catch (Exception e) { } catch (Exception e) {
log.error("카카오 응답 파싱 및 Redis 저장 실패: storeId={}, error={}", storeId, e.getMessage()); log.error("카카오 응답 파싱 및 Redis 저장 실패: storeId={}, error={}", storeId, e.getMessage());
e.printStackTrace();
updateSyncStatus(storeId, platform, "FAILED", 0); updateSyncStatus(storeId, platform, "FAILED", 0);
return 0; return 0;
} }