-
Notifications
You must be signed in to change notification settings - Fork 7
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
RDS reader writer 적용 #701
RDS reader writer 적용 #701
Changes from 2 commits
973703d
dc25796
601f087
40d8ec7
55684ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,63 @@ | ||||||
package codezap.global.rds; | ||||||
|
||||||
import java.util.HashMap; | ||||||
import java.util.Map; | ||||||
|
||||||
import javax.sql.DataSource; | ||||||
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||||||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||||||
import org.springframework.context.annotation.Bean; | ||||||
import org.springframework.context.annotation.Configuration; | ||||||
import org.springframework.context.annotation.DependsOn; | ||||||
import org.springframework.context.annotation.Primary; | ||||||
import org.springframework.context.annotation.Profile; | ||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||||||
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; | ||||||
|
||||||
import com.zaxxer.hikari.HikariDataSource; | ||||||
|
||||||
import lombok.RequiredArgsConstructor; | ||||||
|
||||||
@Configuration | ||||||
@RequiredArgsConstructor | ||||||
@Profile("prod") | ||||||
@EnableJpaRepositories(basePackages = "codezap") | ||||||
public class DataSourceConfig { | ||||||
|
||||||
@Bean | ||||||
@ConfigurationProperties("spring.datasource.write") | ||||||
public DataSource writeDataSource() { | ||||||
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||||||
} | ||||||
|
||||||
@Bean | ||||||
@ConfigurationProperties("spring.datasource.read") | ||||||
public DataSource readeDataSource() { | ||||||
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||||||
} | ||||||
|
||||||
@Bean | ||||||
@DependsOn({"writeDataSource", "readeDataSource"}) | ||||||
public DataSource routeDataSource() { | ||||||
DataSourceRouter dataSourceRouter = new DataSourceRouter(); | ||||||
DataSource writerDataSource = writeDataSource(); | ||||||
DataSource readerDataSource = readeDataSource(); | ||||||
|
||||||
Map<Object, Object> dataSourceMap = new HashMap<>(); | ||||||
dataSourceMap.put("write", writerDataSource); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
위의 키들은 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은데요???! |
||||||
dataSourceMap.put("read", readerDataSource); | ||||||
|
||||||
dataSourceRouter.setTargetDataSources(dataSourceMap); | ||||||
dataSourceRouter.setDefaultTargetDataSource(writerDataSource); | ||||||
|
||||||
return dataSourceRouter; | ||||||
} | ||||||
|
||||||
@Bean | ||||||
@Primary | ||||||
@DependsOn({"routeDataSource"}) | ||||||
public DataSource dataSource() { | ||||||
return new LazyConnectionDataSourceProxy(routeDataSource()); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package codezap.global.rds; | ||
|
||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class DataSourceRouter extends AbstractRoutingDataSource { | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
TransactionSynchronizationManager으로부터 지금 실행되는 트랜잭션이 readOnly으로 마킹되어있는지 체크하는가 보군요 신기방기 |
||
|
||
if(readOnly) { | ||
return "read"; | ||
} | ||
return "write"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ public Page<Template> findAllBy( | |
return templateRepository.findAll(new TemplateSpecification(memberId, keyword, categoryId, tagIds), pageable); | ||
} | ||
|
||
@Transactional | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위에 create에도 @transactional 해주세요 ~ |
||
public Template update( | ||
Member member, | ||
Long templateId, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ | |
<appender-ref ref="console-appender"/> | ||
</logger> | ||
|
||
<root level="WARN"> | ||
<root level="INFO"> | ||
<appender-ref ref="info-appender"/> | ||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 부분은 앞으로도 계속 적용할 정책인가요? 아니면 데모데이 이후 변경될 예정인가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 설정 파일이 잘 적용되는지 정도만 확인하고 삭제하겠습니다~ |
||
<appender-ref ref="warn-appender"/> | ||
<appender-ref ref="error-appender"/> | ||
</root> | ||
|
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.
따로 "spring.datasource.write" 값을 주입하는 코드가 없는데 자동으로 주입이 되는 건가요? 이것도 신기방기하네요
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.
ㄹㅇㄹㅇ 신기한게 한두개가 아님요
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.
아마
@ConfigurationProperties
를 리플렉션해서 주입해주는 코드가 내부에 있을 것 같네요