-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat-be: DataSource Routing을 통한 DB Read/Write 요청 분산 #704
Conversation
@Primary | ||
@DependsOn(ROUTE_DATASOURCE) | ||
public DataSource defaultDataSource() { | ||
return new LazyConnectionDataSourceProxy(routeDataSource()); |
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.
LazyConnectionDataSourceProxy
은 실제로 커넥션이 필요한경우가 아니라면 데이터베이스 풀에서 커넥션을 점유하지 않고 실제로 필요한 시점에만 커넥션을 점유하게 할 수 있게 합니다.
dataSourceMap.put(DataSourceRouter.READ_DATASOURCE_KEY, readDataSource); | ||
dataSourceMap.put(DataSourceRouter.WRITE_DATASOURCE_KEY, wrtieDataSource); | ||
dataSourceRouter.setTargetDataSources(dataSourceMap); | ||
dataSourceRouter.setDefaultTargetDataSource(readDataSource); |
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.
determineCurrentLookupKey() 를 통해 반환받은 Key로부터 DataSource를 반환받을 수 없는 경우에 default DataSource를 사용합니다.
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.
default를 readDataSource로 정한 이유가 있나요? read DB는 read-only여서 write DB로 설정하는게 맞지 않나 싶어서요!
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.
slave DB가 죽었을때는 master를 바라보게 하는것이 writre/read 둘다 가능하니 좋겠네요.
1727232638.558739 |
📌 Test Coverage Report
|
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.
처음 도입해보는 기술이라 학습이 많이 필요했을텐데, 수고하셨습니다, 러쉬!
관련해서 리뷰 남겼습니다. 확인해주세요!
dataSourceMap.put(DataSourceRouter.READ_DATASOURCE_KEY, readDataSource); | ||
dataSourceMap.put(DataSourceRouter.WRITE_DATASOURCE_KEY, wrtieDataSource); | ||
dataSourceRouter.setTargetDataSources(dataSourceMap); | ||
dataSourceRouter.setDefaultTargetDataSource(readDataSource); |
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.
default를 readDataSource로 정한 이유가 있나요? read DB는 read-only여서 write DB로 설정하는게 맞지 않나 싶어서요!
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
|
||
@Configuration | ||
@Profile("prod") |
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.
profile을 설정하지 않고, 매 profile에 read db와 write db의 url을 같게하면 될 것 같아요. profile을 한정한 이유가 있나요?
if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { | ||
return READ_DATASOURCE_KEY; | ||
} |
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.
복제 구성은 주로 writer:read의 관계가 1:N입니다. 현재 구조에서 read db가 늘어나도 코드를 그대로 재사용할 수 있나요?
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.
현재 구조에서는 read data가 늘어났을 때 코드를 재사용할 수 없죠.
read data가 늘어난다면 round-robin 형식으로 datasource를 지정하도록 변경할 수 있을 것 같습니다.
해당 사항은 나중에 반영해보도록 하겠습니다. 적용해보려 했는데 properties를 다루면서 에러를 많이 만날 것 같습니다..
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 wrtieDataSource = writeDataSource(); | ||
DataSource readDataSource = readDataSource(); |
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.
이 포스팅(참고로 코틀린)의 구현 방식을 보고 제안합니다.
@Qualifier
를 활용하여 readDataSource와 writeDataSource를 빈으로 주입하는 건 어떠신가요?
(반영 필수 아님)
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.
반영해보려했으나, 밑에 부분에서도 주입을 해주어야해서 관리포인트가 늘어나는 것 같네요ㅠㅠ
@Bean
@Primary
@DependsOn(ROUTE_DATASOURCE)
public DataSource defaultDataSource() {
return new LazyConnectionDataSourceProxy(routeDataSource());
}
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.
수고하셨습니다~ PR 제목도 수정해주세요!
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Leetaehoon <66353672+xogns1514@users.noreply.github.com>
Original issue description
목적
작업 세부사항
참고 사항
DB_ROUTING_01
closes #703