-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from MJU-InsuranceSystem/read-replica
DB Read Replica 기능 구현
- Loading branch information
Showing
3 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...in/java/fourservings_fiveservings/insurance_system_be/common/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,60 @@ | ||
package fourservings_fiveservings.insurance_system_be.common.config; | ||
|
||
import java.util.HashMap; | ||
import javax.sql.DataSource; | ||
import lombok.extern.slf4j.Slf4j; | ||
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.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
|
||
@Slf4j | ||
@Configuration | ||
public class DataSourceConfig { | ||
|
||
private static final String SOURCE_SERVER = "source"; | ||
private static final String REPLICA_SERVER = "replica"; | ||
|
||
@Bean | ||
@Qualifier(SOURCE_SERVER) | ||
@ConfigurationProperties("spring.datasource.source") | ||
public DataSource masterDataSource() { | ||
log.info("source register"); | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean | ||
@Qualifier(REPLICA_SERVER) | ||
@ConfigurationProperties("spring.datasource.replica") | ||
public DataSource replicaDataSource() { | ||
log.info("replica register"); | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean | ||
public DataSource routingDataSource(@Qualifier(SOURCE_SERVER) DataSource masterDataSource, | ||
@Qualifier(REPLICA_SERVER) DataSource slaveDataSource) { | ||
|
||
RoutingDataSourceConfig routingDataSource = new RoutingDataSourceConfig(); | ||
|
||
HashMap<Object, Object> dataSourceMap = new HashMap<>(); | ||
dataSourceMap.put(SOURCE_SERVER, masterDataSource); | ||
dataSourceMap.put(REPLICA_SERVER, slaveDataSource); | ||
|
||
routingDataSource.setTargetDataSources(dataSourceMap); | ||
routingDataSource.setDefaultTargetDataSource(masterDataSource); | ||
|
||
return routingDataSource; | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public DataSource dataSource() { | ||
DataSource determinedDataSource = routingDataSource(masterDataSource(), | ||
replicaDataSource()); | ||
return new LazyConnectionDataSourceProxy(determinedDataSource); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
.../fourservings_fiveservings/insurance_system_be/common/config/RoutingDataSourceConfig.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,19 @@ | ||
package fourservings_fiveservings.insurance_system_be.common.config; | ||
|
||
import jakarta.annotation.Nullable; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
@Slf4j | ||
public class RoutingDataSourceConfig extends AbstractRoutingDataSource { | ||
|
||
@Nullable | ||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
String lookupKey = | ||
TransactionSynchronizationManager.isCurrentTransactionReadOnly() ? "replica" : "master"; | ||
log.info("Current DataSource is {}", lookupKey); | ||
return lookupKey; | ||
} | ||
} |
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