-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT] DB Replication 설정
- Loading branch information
Showing
5 changed files
with
111 additions
and
5 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
backend/src/main/java/ddangkong/config/database/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,54 @@ | ||
package ddangkong.config.database; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
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.Primary; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
|
||
@Profile("prod") | ||
@Configuration | ||
public class DataSourceConfig { | ||
|
||
@Bean | ||
@ConfigurationProperties(prefix = "spring.datasource.source") | ||
public DataSource sourceDataSource() { | ||
return DataSourceBuilder.create() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@ConfigurationProperties(prefix = "spring.datasource.replica1") | ||
public DataSource replica1DataSource() { | ||
return DataSourceBuilder.create() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public DataSource routingDataSource( | ||
DataSource sourceDataSource, | ||
DataSource replica1DataSource | ||
) { | ||
Map<Object, Object> dataSources = new HashMap<>(); | ||
dataSources.put("source", sourceDataSource); | ||
dataSources.put("replica1", replica1DataSource); | ||
|
||
RoutingDataSource routingDataSource = new RoutingDataSource(List.of("replica1")); | ||
routingDataSource.setDefaultTargetDataSource(dataSources.get("source")); | ||
routingDataSource.setTargetDataSources(dataSources); | ||
|
||
return routingDataSource; | ||
} | ||
|
||
@Primary | ||
@Bean | ||
public DataSource dataSource() { | ||
return new LazyConnectionDataSourceProxy(routingDataSource(sourceDataSource(), replica1DataSource())); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/ddangkong/config/database/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,24 @@ | ||
package ddangkong.config.database; | ||
|
||
import java.util.List; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class RoutingDataSource extends AbstractRoutingDataSource { | ||
|
||
private final RoutingReplicas<String> routingReplicas; | ||
|
||
public RoutingDataSource(List<String> routingReplicas) { | ||
this.routingReplicas = new RoutingReplicas<>(routingReplicas); | ||
} | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
boolean isReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); | ||
if (isReadOnly) { | ||
return routingReplicas.get(); | ||
} else { | ||
return "source"; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/ddangkong/config/database/RoutingReplicas.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 ddangkong.config.database; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class RoutingReplicas<T> { | ||
|
||
private final List<T> replicas; | ||
private final AtomicInteger index; | ||
|
||
public RoutingReplicas(List<T> replicas) { | ||
this.replicas = replicas; | ||
index = new AtomicInteger(0); | ||
} | ||
|
||
public T get() { | ||
return replicas.get(index.getAndIncrement() % replicas.size()); | ||
} | ||
} |
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