Skip to content

Commit

Permalink
[BE] infra: 운영 환경 DB에 Replication 적용 (#687)
Browse files Browse the repository at this point in the history
* feat: replication을 위한 config 추가

* refactor: read, write 요청 분리를 위한 readOnly 적용

* refactor: profile 적용

* style: 개행 추가

* refactor: readOnly 변수명 변경

* refactor: dataSourceType enum 적용 및 상수화

* chore: osiv 해제

* infra: 테스트 환경에서 flyway 적용하지 않도록 변경
  • Loading branch information
skylar1220 authored Sep 25, 2024
1 parent 648e77f commit 4032844
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
7 changes: 7 additions & 0 deletions backend/src/main/java/reviewme/config/DataSourceType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package reviewme.config;

public enum DataSourceType {
READ,
WRITE,
;
}
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);
}
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ReviewDetailLookupService {

private final ReviewDetailMapper reviewDetailMapper;

@Transactional(readOnly = true)
public ReviewDetailResponse getReviewDetail(long reviewId, String reviewRequestCode, String groupAccessCode) {
ReviewGroup reviewGroup = reviewGroupRepository.findByReviewRequestCode(reviewRequestCode)
.orElseThrow(() -> new ReviewGroupNotFoundByReviewRequestCodeException(reviewRequestCode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reviewme.review.service.exception.ReviewGroupNotFoundByReviewRequestCodeException;
import reviewme.reviewgroup.domain.ReviewGroup;
import reviewme.reviewgroup.repository.ReviewGroupRepository;
Expand All @@ -13,6 +14,7 @@ public class ReviewGroupLookupService {

private final ReviewGroupRepository reviewGroupRepository;

@Transactional(readOnly = true)
public ReviewGroupResponse getReviewGroupSummary(String reviewRequestCode) {
ReviewGroup reviewGroup = reviewGroupRepository.findByReviewRequestCode(reviewRequestCode)
.orElseThrow(() -> new ReviewGroupNotFoundByReviewRequestCodeException(reviewRequestCode));
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ spring:
show-sql: true
hibernate:
ddl-auto: update
open-in-view: false
2 changes: 2 additions & 0 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ spring:
show-sql: true
hibernate:
ddl-auto: update
flyway:
enabled: false

springdoc:
swagger-ui:
Expand Down

0 comments on commit 4032844

Please sign in to comment.