-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: DB Replica 설정 * chore: transaction 테스트 * refactor: @transactional 클래스 상단으로 통일 * refactor: CustomTransactionManager 추가 * refactor: CustomTransactionManager 삭제 * refactor: DataSourceConfig prod profile 추가 * chore: 디버깅 코드 삭제 * chore: prod db 환경설정 및 p6spy 설정 추가 * refactor: DataSourceType enum 생성 * refactor: p6spy 제거 * refactor: DataSourceConfig 패키지 변경 * refactor: p6spy 제거 submodule 업데이트 --------- Co-authored-by: LJW25 <dbjwlee@gmail.com>
- Loading branch information
Showing
13 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
Submodule backend-submodule
updated
from 808c77 to 544506
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
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
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
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
62 changes: 62 additions & 0 deletions
62
backend/src/main/java/hanglog/global/config/DataSourceConfig.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package hanglog.global.config; | ||
|
||
import static hanglog.global.config.datasource.DataSourceType.REPLICA; | ||
import static hanglog.global.config.datasource.DataSourceType.SOURCE; | ||
|
||
import hanglog.global.config.datasource.RoutingDataSource; | ||
import java.util.HashMap; | ||
import javax.sql.DataSource; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
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.Primary; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
|
||
@Profile({"dev", "prod"}) | ||
@Configuration | ||
public class DataSourceConfig { | ||
|
||
private static final String SOURCE_SERVER = "SOURCE"; | ||
private static final String REPLICA_SERVER = "REPLICA"; | ||
|
||
@Bean | ||
@Qualifier(SOURCE_SERVER) | ||
@ConfigurationProperties(prefix = "spring.datasource.source") | ||
public DataSource sourceDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean | ||
@Qualifier(REPLICA_SERVER) | ||
@ConfigurationProperties(prefix = "spring.datasource.replica") | ||
public DataSource replicaDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean | ||
public DataSource routingDataSource( | ||
@Qualifier(SOURCE_SERVER) final DataSource sourceDataSource, | ||
@Qualifier(REPLICA_SERVER) final DataSource replicaDataSource | ||
) { | ||
final RoutingDataSource routingDataSource = new RoutingDataSource(); | ||
|
||
final HashMap<Object, Object> dataSourceMap = new HashMap<>(); | ||
dataSourceMap.put(SOURCE, sourceDataSource); | ||
dataSourceMap.put(REPLICA, replicaDataSource); | ||
|
||
routingDataSource.setTargetDataSources(dataSourceMap); | ||
routingDataSource.setDefaultTargetDataSource(sourceDataSource); | ||
|
||
return routingDataSource; | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public DataSource dataSource() { | ||
final DataSource determinedDataSource = routingDataSource(sourceDataSource(), replicaDataSource()); | ||
return new LazyConnectionDataSourceProxy(determinedDataSource); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/hanglog/global/config/datasource/DataSourceType.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package hanglog.global.config.datasource; | ||
|
||
public enum DataSourceType { | ||
|
||
SOURCE, REPLICA | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/hanglog/global/config/datasource/RoutingDataSource.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package hanglog.global.config.datasource; | ||
|
||
import static hanglog.global.config.datasource.DataSourceType.REPLICA; | ||
import static hanglog.global.config.datasource.DataSourceType.SOURCE; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
@Slf4j | ||
public class RoutingDataSource extends AbstractRoutingDataSource { | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
final String currentTransactionName = TransactionSynchronizationManager.getCurrentTransactionName(); | ||
final boolean isReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); | ||
if (isReadOnly) { | ||
log.info(currentTransactionName + " Transaction:" + "Replica 서버로 요청합니다."); | ||
return REPLICA; | ||
} | ||
|
||
log.info(currentTransactionName + " Transaction:" + "Source 서버로 요청합니다."); | ||
return SOURCE; | ||
} | ||
} |
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
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
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
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
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