-
Notifications
You must be signed in to change notification settings - Fork 48
[#43] Mysql Master/Slave Replication 적용 #60
base: develop
Are you sure you want to change the base?
Conversation
} else { | ||
throw new WrongDataSourceException("Wrong DataSource Type : Should Check Exception"); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
만약 DataSource Type이 Master나 Slave가 아니라면 개발자들이 어노테이션에 오타를 내거나 이름을 잘못지은 오류일 것입니다.
따라서 아예 컴파일타임에 개발자들이 exception을 알아채고 처리를 해주어야 할 것 같아서 checked Exception인 SQLException을 사용하였습니다.
|
||
@Aspect | ||
@Component | ||
public class SetDataSourceAspect { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 처리해줄 필요없이 스프링에서 제공하는 기능이 있을 것 같습니다~ readOnly
트랜잭션이면 slave에서 읽는다거나요~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Transactional
을 서비스에 붙히고 readOnly인지 아닌지에 따라 DataSoruce들을 분기해줄 수 있는 방식과 지금처럼 AOP방식을 고민을 했엇습니다.
하지만 트랜잭션 방식을 사용하여 라우팅해준다면 모든 서비스에 @Transactional
을 붙혀야 하고 단일연산에도 트랜잭션 설정을 해줘야 해서 AOP방식으로 데이터소스를 나누도록 구현 하였습니다.
성능테스트 결과를 보니 WAS서버 3대로 Scale Out을 적용하여도 Mysql 사용량이 최대 60%정도가 나왔고 슬로우 쿼리도 없었습니다. 또한 레디스 캐싱, 엔진엑스 캐싱등을 이용하여 디비 부하를 분산시켜줬기 때문에 아직까지는 Mysql의 성능이 충분하고 추후 부족하더라도 Scale up으로 충분한 대응이 가능해보입니다. 이에 따라 아직은 Mysql을 이중화할 필요까진 없는 것으로 판단하여 이 PR을 보류중입니다. |
AbstractRoutingDataSource 클래스를 이용하여
TargetDataSources에 slave와 master DataSource를 등록하고
determineCurrentLookupKey 함수를 오버라이드하여
Slave Datasource와 Master DataSource중 선택하여 사용합니다.
어떤 데이터소스를 사용할지는 AOP를 사용하여 구현하였습니다.
메소드(Mapper) 시작전에 AOP를 이용하여 쓰레드로컬 변수에 마스터와 슬레이브중 한개를 넣어놓고
determineCurrentLookupKey함수에서 쓰레드로컬 변수를 가져와 어떤 데이터소스를 사용할지 정합니다.
determineCurrentLookupKey는 JDBC getConnection()함수에서 호출되어 데이터소스가 정해집니다.
자세한 설명은 다음 블로그에 정리하였습니다.
https://tjdrnr05571.tistory.com/14?category=876333
LazyConnectionDataSourceProxy를 사용한 이유
https://tjdrnr05571.tistory.com/15?category=876333