-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 데이터베이스 Replication 구성에 따른 Read / Write 분리하기 (#592)
* feat: DataSource 연결 안됨. * feat: prod 환경에 Read, Write Datasource 구분하여 연결 * feat: prod 환경에 Read, Write Datasource 구분하여 연결 * fix: rds 연결 불가 버그 수정 * feat: 서버 증가로 인한 CD 수정 --------- Co-authored-by: Arachneee <[email protected]>
- Loading branch information
1 parent
243cd0d
commit 0290f46
Showing
5 changed files
with
128 additions
and
1 deletion.
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
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
70 changes: 70 additions & 0 deletions
70
server/src/main/java/server/haengdong/infrastructure/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,70 @@ | ||
package server.haengdong.infrastructure; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
import jakarta.persistence.EntityManagerFactory; | ||
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.DependsOn; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
@Profile("prod") | ||
@Configuration | ||
public class DataSourceConfig { | ||
|
||
private static final String PRIMARY = "primary"; | ||
private static final String SECONDARY = "secondary"; | ||
|
||
@ConfigurationProperties(prefix = "spring.datasource.hikari.primary") | ||
@Bean | ||
public DataSource primaryDataSource() { | ||
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||
} | ||
|
||
@ConfigurationProperties(prefix = "spring.datasource.hikari.secondary") | ||
@Bean | ||
public DataSource secondaryDataSource() { | ||
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||
} | ||
|
||
@DependsOn({"primaryDataSource", "secondaryDataSource"}) | ||
@Bean | ||
public DataSource routingDataSource( | ||
@Qualifier("primaryDataSource") DataSource primary, | ||
@Qualifier("secondaryDataSource") DataSource secondary) { | ||
DynamicRoutingDataSource routingDataSource = new DynamicRoutingDataSource(); | ||
|
||
Map<Object, Object> dataSourceMap = new HashMap<>(); | ||
|
||
dataSourceMap.put(PRIMARY, primary); | ||
dataSourceMap.put(SECONDARY, secondary); | ||
|
||
routingDataSource.setTargetDataSources(dataSourceMap); | ||
routingDataSource.setDefaultTargetDataSource(primary); | ||
|
||
return routingDataSource; | ||
} | ||
|
||
@DependsOn({"routingDataSource"}) | ||
@Primary | ||
@Bean | ||
public DataSource dataSource(DataSource routingDataSource) { | ||
return new LazyConnectionDataSourceProxy(routingDataSource); | ||
} | ||
|
||
@Bean | ||
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { | ||
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); | ||
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory); | ||
return jpaTransactionManager; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
server/src/main/java/server/haengdong/infrastructure/DynamicRoutingDataSource.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 server.haengdong.infrastructure; | ||
|
||
import static org.springframework.transaction.support.TransactionSynchronizationManager.isCurrentTransactionReadOnly; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
|
||
@Slf4j | ||
public class DynamicRoutingDataSource extends AbstractRoutingDataSource { | ||
|
||
private static final String PRIMARY = "primary"; | ||
private static final String SECONDARY = "secondary"; | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
return isCurrentTransactionReadOnly() ? SECONDARY : PRIMARY; | ||
} | ||
} |
Submodule config
updated
from fa4270 to 0650f4