From 3fdb378e7b41169a4bee61df737cbcb1b7a56526 Mon Sep 17 00:00:00 2001 From: UNGGU0704 Date: Tue, 17 Jun 2025 14:23:41 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20Evnet=20Hub=20=EC=97=B0=EA=B2=B0=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExternalIntegrationInteractor.java | 36 ++++++++++--------- store/src/main/resources/application.yml | 5 +++ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/store/src/main/java/com/ktds/hi/store/biz/service/ExternalIntegrationInteractor.java b/store/src/main/java/com/ktds/hi/store/biz/service/ExternalIntegrationInteractor.java index 7329c7c..7ed8caa 100644 --- a/store/src/main/java/com/ktds/hi/store/biz/service/ExternalIntegrationInteractor.java +++ b/store/src/main/java/com/ktds/hi/store/biz/service/ExternalIntegrationInteractor.java @@ -1,6 +1,7 @@ package com.ktds.hi.store.biz.service; import com.azure.messaging.eventhubs.EventData; +import com.azure.messaging.eventhubs.EventDataBatch; import com.azure.messaging.eventhubs.EventHubClientBuilder; import com.azure.messaging.eventhubs.EventHubProducerClient; import com.fasterxml.jackson.databind.ObjectMapper; @@ -14,6 +15,7 @@ import com.ktds.hi.common.exception.BusinessException; import com.ktds.hi.common.exception.ExternalServiceException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -35,6 +37,9 @@ public class ExternalIntegrationInteractor implements ExternalIntegrationUseCase private final RedisTemplate redisTemplate; private final ObjectMapper objectMapper; + @Value("${azure.eventhub.connection-string}") + private String eventHubConnectionString; + @Override public ExternalSyncResponse syncReviews(Long storeId, ExternalSyncRequest request) { log.info("외부 플랫폼 리뷰 동기화 시작: storeId={}, platform={}", storeId, request.getPlatform()); @@ -228,11 +233,14 @@ public class ExternalIntegrationInteractor implements ExternalIntegrationUseCase throw new BusinessException("비밀번호는 필수입니다"); } } - + /** + * Azure Event Hub로 동기화 완료 이벤트 발행 (기존 메서드 수정) + */ private void publishSyncEvent(Long storeId, String platform, int syncedCount) { try { + // Azure Event Hub Producer 생성 (connection string에서 Event Hub 이름 자동 추출) EventHubProducerClient producer = new EventHubClientBuilder() - .connectionString(System.getenv("EVENTHUB_CONNECTION_STRING"), "review-sync") + .connectionString(eventHubConnectionString) .buildProducerClient(); // Redis에서 실제 리뷰 데이터 조회 @@ -241,25 +249,21 @@ public class ExternalIntegrationInteractor implements ExternalIntegrationUseCase EventData eventData = new EventData(payloadJson); - // 메타데이터 추가 - eventData.getProperties().put("storeId", storeId.toString()); - eventData.getProperties().put("platform", platform); - eventData.getProperties().put("eventType", "EXTERNAL_REVIEW_SYNC"); + // EventDataBatch 사용 + EventDataBatch batch = producer.createBatch(); + if (batch.tryAdd(eventData)) { + producer.send(batch); + log.info("동기화 이벤트 발행 성공: storeId={}, platform={}, syncedCount={}", + storeId, platform, syncedCount); + } else { + log.warn("이벤트 배치 추가 실패: storeId={}, platform={}", storeId, platform); + } - producer.send(Arrays.asList(eventData)); - - // 성공 시 Redis에서 해당 데이터 삭제 또는 상태 변경 - markAsProcessedInRedis(storeId, platform); - - log.info("동기화 이벤트 발행 완료: storeId={}, platform={}, syncedCount={}", - storeId, platform, syncedCount); + producer.close(); } catch (Exception e) { log.error("동기화 이벤트 발행 실패: storeId={}, platform={}, error={}", storeId, platform, e.getMessage(), e); - - // 실패 시 재시도 큐로 이동 - moveToRetryQueue(storeId, platform, e.getMessage()); } } diff --git a/store/src/main/resources/application.yml b/store/src/main/resources/application.yml index f3fc027..58d4097 100644 --- a/store/src/main/resources/application.yml +++ b/store/src/main/resources/application.yml @@ -19,6 +19,11 @@ spring: hibernate: format_sql: true dialect: org.hibernate.dialect.PostgreSQLDialect + + # Azure Event Hub 설정 (추가) + azure: + eventhub: + connection-string: ${AZURE_EVENTHUB_CONNECTION_STRING:Endpoint=sb://hiorder-ns-pubsub.servicebus.windows.net/;SharedAccessKeyName=test;SharedAccessKey=aeFep3tpn1OdNGMx6h92IXOaVxLHSQyxJ+AEhO34wJM=;EntityPath=hiorder-name-pubsub} data: redis: