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

[Feature] 베스트 셀러 Redis 조회 기능 및 스케쥴링 구현 #55

Merged
merged 13 commits into from
Apr 2, 2024

Conversation

jwooo
Copy link
Collaborator

@jwooo jwooo commented Apr 1, 2024

💡 연관된 이슈

close #45

📝 작업 내용

  • Redis 설정
  • Scheduler 설정
  • 베스트 셀러 Redis를 통한 조회 기능 구현
  • 베스트 셀러 스케쥴링을 통한 데이터 자동 수정 기능 구현

💬 리뷰 요구 사항

Redis를 추가하면서 테스트에서 Redis를 사용할 수 없는 문제가 발생하여 TestContainers를 사용하였습니다.

TestContainers를 사용하면서 까지 Redis를 테스트하는 것이 맞을지 아니면 @MockBean으로 처리하여 테스트하는 것이 좋을지 고민하고 있습니다.

어떻게 하는게 좋은 방법인지 몰라 좋은 생각 있으시면 알려주시면 감사하겠습니다 !

@jwooo jwooo added the ✨ Feature 기능 개발 label Apr 1, 2024
@jwooo jwooo requested review from pdohyung and AHNYUNKI April 1, 2024 06:13
@jwooo jwooo self-assigned this Apr 1, 2024
@jwooo jwooo linked an issue Apr 1, 2024 that may be closed by this pull request
4 tasks
Copy link
Member

@AHNYUNKI AHNYUNKI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정우님 코드 잘봤습니다!
제가 남긴 리뷰 한 번 보시고 답 주시면 감사하겠습니다!

그리고 정우님이 남기신 질문

TestContainers를 사용하면서 까지 Redis를 테스트하는 것이 맞을지 아니면 @MockBean으로 처리하여 테스트하는 것이 좋을지 고민하고 있습니다.

이 부분 같은 경우에는 Redis 테스트 시 파이프라인에서 통과하지 못할까봐 @MockBean 처리를 하는것이 좋다고 생각이 들었었는데, 정우님이 잘 해결하신거같아, 굳이 @MockBean 테스트로 진행하지 않으셔도 될거같습니다!

BestSellerResponse.of(key, book.getIsbn(), book.getTitle(), book.getPublisher(),
book.getThumbnail(), book.getAuthors(), book.getDateTime()));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

런타임 Exception은 보다는 무슨 Exception인지 명시 해주는게 좋을거 같습니다!! 정우님!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗! 까먹고 수정하지 못했네요..

수정하겠습니다!!

Comment on lines +34 to +36
@SpringBootTest
@RecordApplicationEvents
@Slf4j
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기에다가 @SpringBootTest를 다시는거보다 RedisTestContainer 쪽에다 다시는게 좋지않을까요 ? 나중에 TalkRoom에도 Redis를 적용시켜야 할 수도 있는데 서버가 두개 켜지는거보다 한 번 켜지는 편이 더 좋을거같아서 얘기드립니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 컨테이너는 Redis 전용 테스트 컨테이너 입니다. 추후에 KafkaPostgreSql이 추가될 수도 있습니다. 이런 경우 나머지 테스트 컨테이너에도 @SpringBootTest를 붙여야 합니다.

나중에 상속으로 하지 않는 방식으로 구현하여 ServiceTestSupport에서 해결할 수 있도록 해보겠습니다!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 알겠습니다

Copy link
Member

@pdohyung pdohyung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

내용 확인했습니다 !

저 또한 외부 시스템은 mock으로 처리하는 게 좋다고 생각합니다.
하지만 정우님 코드를 보니 스프링 내에서 redis는 환경을 다양하게 제공하네요.
그래서 테스트가 정상적으로 동작한다면 현재 테스트 코드도 좋은 것 같습니다 !

그리고 질문 남겼으니 답변 부탁드립니다. 😊

Comment on lines 10 to 15
@SpringBootApplication
@EnableCaching
@EnableScheduling
@EnableRetry
@ConfigurationPropertiesScan
public class JisunginApplication {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

애플리케이션 실행 시 캐싱, 스케줄링, 리트라이가 적용되는 건가요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Enablexxx 어노테이션들은 관련된 라이브러리의 기능을 활성화하는 역할 입니다!

Comment on lines +37 to +41
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());

redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key, Value가 적용됐는데 HashKey, HashValue도 적용한 이유가 궁금합니다 !

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setKeySerializer()setValueSerializer()는 일반적인 키와 값을 저장하는 RedisTemplate의 직렬화 방식을 지정하는 데 사용됩니다.

redis에서의 getset의 명령어와 같은 기능을 사용할 때 String 타입으로 직렬화 한다는 의미입니다.

setHashKeySerializer()setValueSerializer()Redis 해시에 대해 사용됩니다. 저희는 베스트 셀러에 대한 값을 Hash로 저장하기 때문에 해당 설정도 추가적으로 적용하였습니다!

@@ -24,7 +24,7 @@ public CrawlingBook crawlBook(String isbn) {
@Override
public Map<Long, CrawlingBook> crawlBestSellerBook() {
Map<Long, String> bestSellerBookIds = parser.parseBestSellerBookId(fetcher.fetchBestSellerBookId());
Map<Long, CrawlingBook> bestSellerBooks = new HashMap<>();
Map<Long, CrawlingBook> bestSellerBooks = new ConcurrentHashMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thread-safe를 보장하기 위해 ConcurrentHashMap을 쓰신 건가요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashMap으로 구현하였더니 테스트 단계에서 문제가 발생하여 Thread-safeConcurrentHashMap으로 변경하였습니다!

Comment on lines 24 to +25
@Override
@Retryable(retryFor = BusinessException.class, maxAttempts = 5, backoff = @Backoff(delay = 1000))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재시도 횟수가 최대 5회, 각 시도에서 1초만 동작하는 건가요 ?
만약 5회 실패하면 BusinessException을 날리나요 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 메서드에서 BusinessException이 발생하면 5번 재시도 하며 재시도 간의 딜레이는 1초로 설정한 코드입니다!

@jwooo jwooo force-pushed the feature/45-best-seller-redis-and-scheduler branch from 563c6a9 to 2c6f463 Compare April 2, 2024 09:39
Copy link
Member

@AHNYUNKI AHNYUNKI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아이고 고생하셨습니다 정우님~~

Copy link
Member

@pdohyung pdohyung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다 ! 😊

@jwooo jwooo merged commit f55764b into develop Apr 2, 2024
1 check passed
@jwooo jwooo deleted the feature/45-best-seller-redis-and-scheduler branch April 2, 2024 12:28
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.

[Feature] 베스트 셀러 Redis 조회 기능 및 스케쥴링 구현
3 participants