-
Notifications
You must be signed in to change notification settings - Fork 0
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
주문 결제 기능 구현 #46
Merged
Merged
주문 결제 기능 구현 #46
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 결제 흐름을 정리하면서, 하나의 주문 API에 주문 생성, 결제 등의 트랜잭션을 처리하도록 변경
- 성능 테스트를 위해 실제 외부 API와 연동 잠시 해제
- 1개의 트랜잭션에서 재고 차감과 외부 API 호출이 함께 이루어져 DB 락을 너무 오래 잡고 있는 문제 개선 - 재고 차감 시 비관적 락을 얻지 않고 곧바로 재고 차감 쿼리를 날리도록 변경
- event-driven architecture로 주문 생성 성공 시 이벤트 발행 - 이벤트 리스너에서 결제 승인 요청 API 호출
- DB의 커넥션 유지 시간을 180s로 수정했기 때문에, 이보다 조금 더 작은 시간 동안만 커넥션을 살리도록 변경
- maximum pool 사이즈가 커진 만큼 커넥션을 얻어 scale-out 시 커넥션 부족으로 실행 중지되는 것을 막기 위함 - DB의 커넥션 유지 시간을 짧게 두어 빠르게 반환하도록 설정 - DB 커넥션 대기 시간을 짧게 두어 사용자 요청을 빠르게 처리하도록 설정
- 주문 생성 이후 이벤트 발행 로직 삭제 - 토스 결제 API 호출을 WebClient를 이용한 로직으로 변경하여 비동기 처리 - 결제 승인 요청부터는 비동기 처리 되므로 사용자에게는 정상 응답 반환. 단, 응답메세지는 주문 '요청' 성공으로 반환 - 결제 실패 시 주문 상태를 CANCELED로 변경
- jpa repository의 save메서드는 엔티티의 ID 존재 여부에 따라 로직이 다름. ID가 존재하면 DB에 존재한다고 판단하여 select 쿼리가 발생하는데 이를 막아줌
- 상품을 조회한 후, 지연 로딩을 활용하여 재고를 수정하는 비관적인 락에 비해 곧바로 업데이트 쿼리를 날림으로써 간결하게 재고를 수정하고 row 레벨의 implicit lock을 활용할 수 있다. - 재고 차감 네이밍을 deduct에서 decrease로 변경
- 주문 생성 후 결제 단계에서 실패 시 결제 취소와 구분하기 위해 cancle이 아닌 failed로 처리
- 커넥션 풀 획득 대기시간인 5초를 넘겨 타임아웃이 발생하면 예외 발생
- 결제 요청 승인 메세지를 수신하여 batch 처리하도록 구성
- SQS 메세지 송,수신 클래스 제거 - Jdbc Batch 처리를 위한 repository 제거
- Payment 관련 이벤트를 발행하는 PaymentEventPublisher 생성 - Payment와 관련된 이벤트를 핸들링하는 PaymentEventHandler 생성 - 결제 승인 성공 여부에 대한 이벤트 클래스 분기
- 불필요한 주석 제거 - 결제 승인 이벤트 처리 로직 사용
- 기존 1초~2초 사이의 랜덤값 부여에서 고정 1초로 변경
- 주문 생성, 재고 차감, 아웃박스 생성, 결제 승인 요청 등 여러 도메인을 걸쳐 수행되는 비즈니스 로직을 명확하게 하기 위해 퍼사드 패턴 적용 - 단일 책임 원칙을 준수하기 위해 메서드 분리
- WebClient의 비동기 요청을 사용하지 않아 RestClient로 변경 - Retry, Recover 정책 도입 - 재시도 실패 시 결제 상태 불확실 예외 처리 - 결제 처리 메서드 네이밍 변경
- 기존 spring에서 제공하는 retry가 아닌 resilience4j로 대체
- 결제 요청 승인에 대한 예외 발생이 지속되거나, 지연되면 CircuitBreaker OPEN - 일정 시간 이후 HALF_OPEN으로 변경되어 일부 요청을 처리하고 정상적으로 동작하면 CLOSED로 변경
- CircuitBreaker가 OPEN 상태라면 CallNotPermittedException 예외 발생 - 해당 예외를 캐치해서 처리하도록 구현
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
작업 내용 중 기록 사항
TODO : 미결제 outbox 처리 로직 구현 필요
TODO : 테스트코드 작성 필요
WIKI
연관 이슈
closes #45