Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kafka 적용 : DB 간 데이터 동기화 #80

Merged
merged 14 commits into from
Sep 8, 2023
Merged

Kafka 적용 : DB 간 데이터 동기화 #80

merged 14 commits into from
Sep 8, 2023

Conversation

yooniversal
Copy link
Contributor

@yooniversal yooniversal commented Sep 8, 2023

Kafka 적용

생성일, 최종 수정일 필드 추가

생성일 created_at, 최종 수정일 updated_at 필드를 모든 테이블에 추가했습니다. 해당 내용은 BaseEntity에 설정했습니다.
상품 엔티티 Product에서 updated_at의 최댓값은 크롤링 DB에서 가져오는 상품 기준으로 활용됩니다.

public class BaseEntity {

    @CreatedDate
    @Column(updatable = false, name = "created_at")
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
}

//상속해서 필드 추가
public class Product extends BaseEntity { ... }

Kafka

프로젝트에 Kafka를 적용해 DB 간 데이터 동기화를 수행하는데 활용합니다.
서비스에서 직접적으로 사용하는 운영 DB와 크롤링 상품을 담는 크롤링 DB에서 차이가 없도록 2주마다 동기화 처리를 수행합니다.
크롤링 DB에서 가져올 상품들은 ML 서버에서 구현된 API를 통해 가져오게 되며 새 상품은 신규 등록을, 기존 상품은 가격 및 비활성화 여부를 업데이트합니다.

@Component
@RequiredArgsConstructor
public class CrawlingScheduler {

    private final KafkaProducer kafkaProducer;

    private final int TWO_WEEK = 1000 * 60 * 60 * 24 * 14;

    @Scheduled(fixedDelay = TWO_WEEK) //2주마다 호출
    public void updateCrawledProducts() throws JsonProcessingException {
        //ML 서버 API를 호출해 크롤링 상품 정보를 얻는 API를 호출
        //얻은 상품 정보들을 운영 DB에 초기화까지 한 번에 수행
        kafkaProducer.callCrawledProductCountAPI();
    }
}

S3 객체 이동

크롤링한 상품이 저장되는 S3 버킷은 서비스에서 활용할 S3 버킷과 다릅니다.
때문에 서비스용 S3 버킷에 크롤링 상품 정보를 옮겨주는 작업이 필요해 S3에 접근할 수 있도록 설정 클래스 추가 후 옮기는 로직을 적용했습니다.

ML 서버 API를 통해 가져오는 상품 정보는 웹 배포된 URI가 아니기 때문에
프론트에서 활용할 수 있도록 웹 배포된 URI로 전처리 후 DB에 저장됩니다.

CLOUDFRONT_URL는 AWS CloudFront를 통해 웹 배포된 URI를, productDescriptionUrl는 S3 경로를 의미합니다.

@yooniversal yooniversal added the ✨ Feature 기능 개발 및 개선 label Sep 8, 2023
@yooniversal yooniversal linked an issue Sep 8, 2023 that may be closed by this pull request
@yooniversal yooniversal merged commit 6841295 into main Sep 8, 2023
1 check passed
@yooniversal yooniversal deleted the kafka-config branch September 8, 2023 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ Feature 기능 개발 및 개선
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kafka 설정
1 participant