-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat-be: DataSource Routing을 통한 DB Read/Write 요청 분산 (#704)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Leetaehoon <66353672+xogns1514@users.noreply.github.com>
- Loading branch information
1 parent
ce03ba0
commit bb12b49
Showing
6 changed files
with
154 additions
and
15 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
59 changes: 59 additions & 0 deletions
59
backend/src/main/java/com/cruru/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,59 @@ | ||
package com.cruru.config; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
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.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
|
||
@Configuration | ||
public class DataSourceConfig { | ||
|
||
private static final String READ_DATASOURCE = "readDataSource"; | ||
private static final String WRITE_DATASOURCE = "writeDataSource"; | ||
private static final String ROUTE_DATASOURCE = "routeDataSource"; | ||
|
||
@Bean(name = READ_DATASOURCE) | ||
@ConfigurationProperties(prefix = "spring.datasource.read") | ||
public DataSource readDataSource() { | ||
return DataSourceBuilder.create() | ||
.type(HikariDataSource.class) | ||
.build(); | ||
} | ||
|
||
@Bean(name = WRITE_DATASOURCE) | ||
@ConfigurationProperties(prefix = "spring.datasource.write") | ||
public DataSource writeDataSource() { | ||
return DataSourceBuilder.create() | ||
.type(HikariDataSource.class) | ||
.build(); | ||
} | ||
|
||
@Bean(name = ROUTE_DATASOURCE) | ||
@DependsOn({READ_DATASOURCE, WRITE_DATASOURCE}) | ||
public DataSourceRouter routeDataSource() { | ||
DataSourceRouter dataSourceRouter = new DataSourceRouter(); | ||
DataSource writeDataSource = writeDataSource(); | ||
DataSource readDataSource = readDataSource(); | ||
|
||
Map<Object, Object> dataSourceMap = new HashMap<>(); | ||
dataSourceMap.put(DataSourceRouter.READ_DATASOURCE_KEY, readDataSource); | ||
dataSourceMap.put(DataSourceRouter.WRITE_DATASOURCE_KEY, writeDataSource); | ||
dataSourceRouter.setTargetDataSources(dataSourceMap); | ||
dataSourceRouter.setDefaultTargetDataSource(writeDataSource()); | ||
return dataSourceRouter; | ||
} | ||
|
||
@Bean | ||
@Primary | ||
@DependsOn(ROUTE_DATASOURCE) | ||
public DataSource defaultDataSource() { | ||
return new LazyConnectionDataSourceProxy(routeDataSource()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/com/cruru/config/DataSourceRouter.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,18 @@ | ||
package com.cruru.config; | ||
|
||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class DataSourceRouter extends AbstractRoutingDataSource { | ||
|
||
public static final String READ_DATASOURCE_KEY = "read"; | ||
public static final String WRITE_DATASOURCE_KEY = "write"; | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { | ||
return READ_DATASOURCE_KEY; | ||
} | ||
return WRITE_DATASOURCE_KEY; | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
backend/src/test/java/com/cruru/config/DataSourceRouterTest.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,37 @@ | ||
package com.cruru.config; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
@DisplayName("Datasource Routing 테스트") | ||
class DataSourceRouterTest { | ||
|
||
private final DataSourceRouter dataSourceRouter = new DataSourceRouter(); | ||
|
||
@Test | ||
void determineCurrentLookupKeyForReadOnlyTransaction() { | ||
// given | ||
TransactionSynchronizationManager.setCurrentTransactionReadOnly(true); | ||
|
||
// when | ||
Object lookupKey = dataSourceRouter.determineCurrentLookupKey(); | ||
|
||
// then | ||
assertThat(lookupKey).isEqualTo(DataSourceRouter.READ_DATASOURCE_KEY); | ||
} | ||
|
||
@Test | ||
void determineCurrentLookupKeyForReadWriteTransaction() { | ||
// give | ||
TransactionSynchronizationManager.setCurrentTransactionReadOnly(false); | ||
|
||
// when | ||
Object lookupKey = dataSourceRouter.determineCurrentLookupKey(); | ||
|
||
// then | ||
assertThat(lookupKey).isEqualTo(DataSourceRouter.WRITE_DATASOURCE_KEY); | ||
} | ||
} |
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