-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE] DB read, write 쿼리 트래픽을 분리해요 (#360)
* chore: 서브모듈 변경사항 반영 * feat: `@Transactional` readOnly 설정 별 DB 트랜잭션 분산 로직 추가 * feat: 읽기 쿼리에 대해 `@Transactional` readOnly 설정 추가 * refactor(DataSourceConfig): `Map.of()` 수정
- Loading branch information
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
backend/src/main/java/kr/momo/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,56 @@ | ||
package kr.momo.config; | ||
|
||
import java.util.Map; | ||
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; | ||
|
||
@Configuration | ||
@Profile("prod") | ||
public class DataSourceConfig { | ||
|
||
public static final String SOURCE_SERVER = "source"; | ||
public static final String REPLICA_SERVER = "replica"; | ||
|
||
@Bean(SOURCE_SERVER) | ||
@ConfigurationProperties(prefix = "spring.datasource.source") | ||
public DataSource sourceDataSource() { | ||
return DataSourceBuilder.create() | ||
.build(); | ||
} | ||
|
||
@Bean(REPLICA_SERVER) | ||
@ConfigurationProperties(prefix = "spring.datasource.replica") | ||
public DataSource replicaDataSource() { | ||
return DataSourceBuilder.create() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public DataSource routingDataSource( | ||
@Qualifier(SOURCE_SERVER) DataSource source, | ||
@Qualifier(REPLICA_SERVER) DataSource replica | ||
) { | ||
RoutingDataSource routingDataSource = new RoutingDataSource(); | ||
Map<Object, Object> dataSourceMap = Map.of( | ||
SOURCE_SERVER, source, | ||
REPLICA_SERVER, replica | ||
); | ||
routingDataSource.setTargetDataSources(dataSourceMap); | ||
routingDataSource.setDefaultTargetDataSource(source); | ||
return routingDataSource; | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public DataSource dataSource() { | ||
DataSource dataSource = routingDataSource(sourceDataSource(), replicaDataSource()); | ||
return new LazyConnectionDataSourceProxy(dataSource); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/kr/momo/config/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,15 @@ | ||
package kr.momo.config; | ||
|
||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class RoutingDataSource extends AbstractRoutingDataSource { | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { | ||
return DataSourceConfig.REPLICA_SERVER; | ||
} | ||
return DataSourceConfig.SOURCE_SERVER; | ||
} | ||
} |
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
Submodule security
updated
from 0b125e to 3977a5