Skip to content

Commit

Permalink
Merge pull request #106 from MJU-InsuranceSystem/read-replica
Browse files Browse the repository at this point in the history
DB Read Replica 기능 구현
  • Loading branch information
0702Yoon authored Dec 7, 2024
2 parents ec4a4cb + 93f4fba commit f7258c4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
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);
}
}
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;
}
}
15 changes: 11 additions & 4 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ spring:
show_sql: true

datasource:
url: ${DB_URL}
driver-class-name: com.mysql.cj.jdbc.Driver
username: ${DB_USER_NAME}
password: ${DB_PASSWORD}
source:
jdbc-url: ${DB_URL}
driver-class-name: com.mysql.cj.jdbc.Driver
username: ${DB_USER_NAME}
password: ${DB_PASSWORD}
replica:
jdbc-url: ${READ_DB_URL}
driver-class-name: com.mysql.cj.jdbc.Driver
username: ${DB_USER_NAME}
password: ${DB_PASSWORD}

0 comments on commit f7258c4

Please sign in to comment.