Skip to content

Commit

Permalink
feat: AOP 적용해 읽기/쓰기 DB로 요청 분산 (#503)
Browse files Browse the repository at this point in the history
* chore: 커스텀하게 만든 datasource 사용해 db 연결

* feat: transactional readonly 값에 따라 datasource 변경

* feat: transactional을 writerDatabase 어노테이션으로 대체

* feat: DataSource 관련 동작 prod 프로필로 제한

* feat: WriterDatabase 어노테이션 적용

* feat: writerDatasource 사용 후 readerDatasource로 변경

* feat: writer db와 user db의 권한 분리

* chore: 배포 테스트

* chore: 직렬로 deploy

* chore: cicd 범위 원래대로 수정

* chore: osiv 꺼서 인증필터가 사용한 datasource의 영향 제거

* chore: 바뀐 properties 반영

* chore: prod 배포

* chore: cicd 범위 원복

* chore: health-check 요청으로 불필요하게 찍히는 로그 제거

* chore: prod 배포

* chore: cicd 범위 원복

* chore: log 레벨 info에서 debug로 변경

* chore: prod 배포

* chore: cicd 범위 원복

* chore: db 스키마에 대한 validate 검사

* refactor: 불필요한 어노테이션 제거

* refactor: 메서드 순서 사용 순대로 변경
  • Loading branch information
ChooSeoyeon authored and fromitive committed Nov 28, 2024
1 parent 44b6f85 commit 88e5a49
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend-prod-ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
docker run --name ${{ secrets.BE_DOCKER_CONTAINER_NAME }} -d -v /logs:/logs -p 80:8080 -e SPRING_PROFILES_ACTIVE=prod ${{ secrets.BE_DOCKERHUB_USERNAME }}/${{ secrets.BE_DOCKER_IMAGE_NAME_PROD }}:${GITHUB_SHA::7}
deploy-b:
needs: build-and-test
needs: deploy-a
runs-on: prod-b

steps:
Expand Down
2 changes: 1 addition & 1 deletion backend/http/auth.http
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ POST {{base-url}}/auth/login/kakao
Content-Type: application/json

{
"accessToken": ""
"accessToken": "ffff"
}

### 토큰 재발급 API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.zzang.chongdae.auth.service.dto.AuthMemberDto;
import com.zzang.chongdae.auth.service.dto.AuthTokenDto;
import com.zzang.chongdae.auth.service.dto.KakaoLoginRequest;
import com.zzang.chongdae.global.config.WriterDatabase;
import com.zzang.chongdae.global.exception.MarketException;
import com.zzang.chongdae.member.domain.AuthProvider;
import com.zzang.chongdae.member.exception.MemberErrorCode;
Expand All @@ -24,6 +25,7 @@ public class AuthService {
private final NicknameGenerator nickNameGenerator;
private final AuthClient authClient;

@WriterDatabase
public AuthInfoDto kakaoLogin(KakaoLoginRequest request) {
String loginId = authClient.getKakaoUserInfo(request.accessToken());
AuthProvider provider = AuthProvider.KAKAO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.zzang.chongdae.comment.service.dto.CommentRoomInfoResponse;
import com.zzang.chongdae.comment.service.dto.CommentRoomStatusResponse;
import com.zzang.chongdae.comment.service.dto.CommentSaveRequest;
import com.zzang.chongdae.global.config.WriterDatabase;
import com.zzang.chongdae.global.exception.MarketException;
import com.zzang.chongdae.member.repository.entity.MemberEntity;
import com.zzang.chongdae.offering.domain.CommentRoomStatus;
Expand All @@ -35,6 +36,7 @@ public class CommentService {
private final OfferingRepository offeringRepository;
private final OfferingMemberRepository offeringMemberRepository;

@WriterDatabase
public Long saveComment(CommentSaveRequest request, MemberEntity member) {
OfferingEntity offering = offeringRepository.findById(request.offeringId())
.orElseThrow(() -> new MarketException(OfferingErrorCode.NOT_FOUND));
Expand Down Expand Up @@ -84,6 +86,7 @@ public CommentRoomInfoResponse getCommentRoomInfo(Long offeringId, MemberEntity
return new CommentRoomInfoResponse(offeringMember);
}

@WriterDatabase
@Transactional
public CommentRoomStatusResponse updateCommentRoomStatus(Long offeringId, MemberEntity member) {
OfferingEntity offering = offeringRepository.findById(offeringId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zzang.chongdae.offering.service;

import com.zzang.chongdae.global.config.WriterDatabase;
import com.zzang.chongdae.global.exception.MarketException;
import com.zzang.chongdae.member.repository.entity.MemberEntity;
import com.zzang.chongdae.offering.domain.OfferingFilter;
Expand Down Expand Up @@ -97,6 +98,7 @@ private void validateIsParticipant(MemberEntity member, OfferingEntity offering)
}
}

@WriterDatabase
@Transactional
public OfferingMeetingResponse updateOfferingMeeting(
Long offeringId, OfferingMeetingUpdateRequest request, MemberEntity member) {
Expand All @@ -114,6 +116,7 @@ private void validateIsProposer(OfferingEntity offering, MemberEntity member) {
}
}

@WriterDatabase
public Long saveOffering(OfferingSaveRequest request, MemberEntity member) {
OfferingEntity offering = request.toEntity(member);
validateMeetingDate(offering);
Expand Down Expand Up @@ -142,6 +145,7 @@ public OfferingProductImageResponse extractProductImageFromOg(OfferingProductIma
return new OfferingProductImageResponse(imageUrl);
}

@WriterDatabase
@Transactional
public OfferingUpdateResponse updateOffering(Long offeringId, OfferingUpdateRequest request, MemberEntity member) {
OfferingEntity offering = offeringRepository.findById(offeringId)
Expand All @@ -159,6 +163,7 @@ private void validateUpdatedTotalCount(Integer currentCount, Integer updatedTota
}
}

@WriterDatabase
public void deleteOffering(Long offeringId, MemberEntity member) {
OfferingEntity offering = offeringRepository.findById(offeringId)
.orElseThrow(() -> new MarketException(OfferingErrorCode.NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zzang.chongdae.offeringmember.service;

import com.zzang.chongdae.global.config.WriterDatabase;
import com.zzang.chongdae.global.exception.MarketException;
import com.zzang.chongdae.member.repository.entity.MemberEntity;
import com.zzang.chongdae.offering.domain.CommentRoomStatus;
Expand Down Expand Up @@ -29,6 +30,7 @@ public class OfferingMemberService {
private final OfferingMemberRepository offeringMemberRepository;
private final OfferingRepository offeringRepository;

@WriterDatabase
@Transactional
public Long participate(ParticipationRequest request, MemberEntity member) {
OfferingEntity offering = offeringRepository.findById(request.offeringId())
Expand Down Expand Up @@ -62,6 +64,7 @@ private void validateDuplicate(OfferingEntity offering, MemberEntity member) {
}
}

@WriterDatabase
@Transactional
public void cancelParticipate(Long offeringId, MemberEntity member) {
OfferingEntity offering = offeringRepository.findById(offeringId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zzang.chongdae.scheduler.service;

import com.zzang.chongdae.global.config.WriterDatabase;
import com.zzang.chongdae.offering.domain.CommentRoomStatus;
import com.zzang.chongdae.offering.domain.OfferingStatus;
import com.zzang.chongdae.offering.repository.OfferingRepository;
Expand All @@ -18,6 +19,7 @@ public class SchedulerService {

private final OfferingRepository offeringRepository;

@WriterDatabase
@Transactional
@Scheduled(cron = "0 0 0 * * *")
public void run() {
Expand Down
13 changes: 9 additions & 4 deletions backend/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: ${SPRING_DATASOURCE_URL}
username: ${SPRING_DATASOURCE_USERNAME}
password: ${SPRING_DATASOURCE_PASSWORD}
hikari:
read:
jdbc-url: ${SPRING_DATASOURCE_URL}
username: ${SPRING_DATASOURCE_USERNAME}
password: ${SPRING_DATASOURCE_PASSWORD}
write:
jdbc-url: ${SPRING_WRITER_DATASOURCE_URL}
username: ${SPRING_WRITER_DATASOURCE_USERNAME}
password: ${SPRING_WRITER_DATASOURCE_PASSWORD}
jpa:
defer-datasource-initialization: false
hibernate:
Expand Down
3 changes: 3 additions & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
SPRING_DATASOURCE_URL=[SPRING_DATASOURCE_URL]
SPRING_DATASOURCE_USERNAME=[SPRING_DATASOURCE_USERNAME]
SPRING_DATASOURCE_PASSWORD=[SPRING_DATASOURCE_PASSWORD]
SPRING_WRITER_DATASOURCE_URL=[SPRING_WRITER_DATASOURCE_URL]
SPRING_WRITER_DATASOURCE_USERNAME=[SPRING_WRITER_DATASOURCE_USERNAME]
SPRING_WRITER_DATASOURCE_PASSWORD=[SPRING_WRITER_DATASOURCE_PASSWORD]
JWT_ACCESS_SECRET_KEY=[JWT_ACCESS_SECRET_KEY]
JWT_REFRESH_SECRET_KEY=[JWT_REFRESH_SECRET_KEY]
1 change: 1 addition & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ spring:
properties:
hibernate:
format_sql: true
open-in-view: false
servlet:
multipart:
enabled: true
Expand Down

0 comments on commit 88e5a49

Please sign in to comment.