-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE] infra: 운영 환경 DB에 Replication 적용 (#687)
* feat: replication을 위한 config 추가 * refactor: read, write 요청 분리를 위한 readOnly 적용 * refactor: profile 적용 * style: 개행 추가 * refactor: readOnly 변수명 변경 * refactor: dataSourceType enum 적용 및 상수화 * chore: osiv 해제 * infra: 테스트 환경에서 flyway 적용하지 않도록 변경
- Loading branch information
1 parent
648e77f
commit 4032844
Showing
7 changed files
with
86 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package reviewme.config; | ||
|
||
public enum DataSourceType { | ||
READ, | ||
WRITE, | ||
; | ||
} |
57 changes: 57 additions & 0 deletions
57
backend/src/main/java/reviewme/config/ReplicationDatasourceConfig.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,57 @@ | ||
package reviewme.config; | ||
|
||
import java.util.HashMap; | ||
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; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
|
||
@Profile("prod") | ||
@Configuration | ||
public class ReplicationDatasourceConfig { | ||
|
||
public static final String WRITE_DATA_SOURCE_NAME = "writeDataSource"; | ||
public static final String READ_DATA_SOURCE_NAME = "readDataSource"; | ||
public static final String ROUTING_DATA_SOURCE_NAME = "routingDataSource"; | ||
|
||
@Bean(name = WRITE_DATA_SOURCE_NAME) | ||
@ConfigurationProperties(prefix = "spring.datasource.write.hikari") | ||
public DataSource writeDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean(name = READ_DATA_SOURCE_NAME) | ||
@ConfigurationProperties(prefix = "spring.datasource.read.hikari") | ||
public DataSource readDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean | ||
DataSource routingDataSource( | ||
@Qualifier(WRITE_DATA_SOURCE_NAME) DataSource writeDataSource, | ||
@Qualifier(READ_DATA_SOURCE_NAME) DataSource readDataSource) { | ||
AbstractRoutingDataSource routingDataSource = new ReplicationRoutingDataSource(); | ||
Map<Object, Object> dataSourceMap = new HashMap<>(); | ||
dataSourceMap.put(DataSourceType.WRITE, writeDataSource); | ||
dataSourceMap.put(DataSourceType.READ, readDataSource); | ||
|
||
routingDataSource.setTargetDataSources(dataSourceMap); | ||
routingDataSource.setDefaultTargetDataSource(writeDataSource); | ||
|
||
return routingDataSource; | ||
} | ||
|
||
@Primary | ||
@Bean | ||
public DataSource dataSource(@Qualifier(ROUTING_DATA_SOURCE_NAME) DataSource routingDataSource) { | ||
return new LazyConnectionDataSourceProxy(routingDataSource); | ||
} | ||
} | ||
|
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/reviewme/config/ReplicationRoutingDataSource.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,16 @@ | ||
package reviewme.config; | ||
|
||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class ReplicationRoutingDataSource extends AbstractRoutingDataSource { | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); | ||
if (readOnly) { | ||
return DataSourceType.READ; | ||
} | ||
return DataSourceType.WRITE; | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -18,3 +18,4 @@ spring: | |
show-sql: true | ||
hibernate: | ||
ddl-auto: update | ||
open-in-view: false |
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 |
---|---|---|
|
@@ -11,6 +11,8 @@ spring: | |
show-sql: true | ||
hibernate: | ||
ddl-auto: update | ||
flyway: | ||
enabled: false | ||
|
||
springdoc: | ||
swagger-ui: | ||
|