-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from woowacourse-teams/feat/452-db-replication
db replication main merge
- Loading branch information
Showing
7 changed files
with
177 additions
and
57 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
60 changes: 60 additions & 0 deletions
60
server/src/main/java/com/project/yozmcafe/config/datasource/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,60 @@ | ||
package com.project.yozmcafe.config.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 javax.sql.DataSource; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
@Profile({"prod"}) | ||
public class DataSourceConfig { | ||
|
||
public static final String MASTER_DATA_SOURCE = "master"; | ||
private static final String MASTER_DATA_SOURCE_LOCATION = "spring.datasource.master"; | ||
public static final String SLAVE_DATA_SOURCE = "slave"; | ||
private static final String SLAVE_DATA_SOURCE_LOCATION = "spring.datasource.slave"; | ||
private static final String ROUTING_DATA_SOURCE = "routingDataSource"; | ||
|
||
@Bean | ||
@Qualifier(MASTER_DATA_SOURCE) | ||
@ConfigurationProperties(prefix = MASTER_DATA_SOURCE_LOCATION) | ||
public DataSource masterDataSource() { | ||
return DataSourceBuilder.create() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@Qualifier(SLAVE_DATA_SOURCE) | ||
@ConfigurationProperties(prefix = SLAVE_DATA_SOURCE_LOCATION) | ||
public DataSource slaveDataSource() { | ||
return DataSourceBuilder.create() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@Qualifier(ROUTING_DATA_SOURCE) | ||
public DataSource routingDataSource(@Qualifier(MASTER_DATA_SOURCE) DataSource sourceDataSource, | ||
@Qualifier(SLAVE_DATA_SOURCE) DataSource replicaDataSource) { | ||
final RoutingDataSource routingDataSource = new RoutingDataSource(); | ||
final Map<Object, Object> dataSources = Map.of(MASTER_DATA_SOURCE, sourceDataSource, | ||
SLAVE_DATA_SOURCE, replicaDataSource); | ||
|
||
routingDataSource.setDefaultTargetDataSource(dataSources.get(MASTER_DATA_SOURCE)); | ||
routingDataSource.setTargetDataSources(dataSources); | ||
|
||
return routingDataSource; | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public DataSource dataSource(@Qualifier(ROUTING_DATA_SOURCE) DataSource routingDataSource) { | ||
return new LazyConnectionDataSourceProxy(routingDataSource); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
server/src/main/java/com/project/yozmcafe/config/datasource/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,18 @@ | ||
package com.project.yozmcafe.config.datasource; | ||
|
||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
import static com.project.yozmcafe.config.datasource.DataSourceConfig.MASTER_DATA_SOURCE; | ||
import static com.project.yozmcafe.config.datasource.DataSourceConfig.SLAVE_DATA_SOURCE; | ||
|
||
public class RoutingDataSource extends AbstractRoutingDataSource { | ||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
final boolean isReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); | ||
if(isReadOnly){ | ||
return SLAVE_DATA_SOURCE; | ||
} | ||
return MASTER_DATA_SOURCE; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
spring: | ||
auth: | ||
key: testtesttesttesttesttesttesttesttesttesttesttesttesttesttest | ||
accessTokenExpired: 360000 | ||
refreshTokenExpired: 36000000 | ||
google: | ||
tokenUri: testtesttesttesttesttest | ||
authUri: testtesttesttesttesttest | ||
clientId: testtesttesttesttesttest | ||
clientSecret: testtesttesttesttesttest | ||
redirectUri: testtesttesttesttesttest | ||
scope: testtesttesttesttesttest | ||
kakao: | ||
tokenUri: testtesttesttesttesttest | ||
authUri: testtesttesttesttesttest | ||
clientId: testtesttesttesttesttest | ||
clientSecret: testtesttesttesttesttest | ||
redirectUri: testtesttesttesttesttest | ||
scope: testtesttesttesttesttest | ||
|
||
servlet: | ||
multipart: | ||
max-file-size: 11MB | ||
max-request-size: 110MB | ||
|
||
flyway: | ||
enabled: true | ||
baseline-version: 20230901153300 | ||
baseline-on-migrate: true | ||
out-of-order: true | ||
|
||
jpa: | ||
properties: | ||
hibernate: | ||
default_batch_fetch_size: 1000 | ||
format_sql: true | ||
show-sql: true | ||
|
||
s3: | ||
bucket: 2023-team-project/2023-yozm-cafe/test/images |
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
spring: | ||
datasource: | ||
url: jdbc:mysql://localhost:20000/yozm-cafe?useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=UTF-8&serverTimezone=UTC | ||
username: root | ||
password: root | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
|
||
auth: | ||
key: testtesttesttesttesttesttesttesttesttesttesttesttesttesttest | ||
accessTokenExpired: 360000 | ||
refreshTokenExpired: 36000000 | ||
google: | ||
tokenUri: testtesttesttesttesttest | ||
authUri: testtesttesttesttesttest | ||
clientId: testtesttesttesttesttest | ||
clientSecret: testtesttesttesttesttest | ||
redirectUri: testtesttesttesttesttest | ||
scope: testtesttesttesttesttest | ||
kakao: | ||
tokenUri: testtesttesttesttesttest | ||
authUri: testtesttesttesttesttest | ||
clientId: testtesttesttesttesttest | ||
clientSecret: testtesttesttesttesttest | ||
redirectUri: testtesttesttesttesttest | ||
scope: testtesttesttesttesttest | ||
|
||
servlet: | ||
multipart: | ||
max-file-size: 11MB | ||
max-request-size: 110MB | ||
|
||
flyway: | ||
enabled: true | ||
baseline-version: 20230901153300 | ||
baseline-on-migrate: true | ||
out-of-order: true | ||
|
||
jpa: | ||
properties: | ||
hibernate: | ||
default_batch_fetch_size: 1000 | ||
|
||
s3: | ||
bucket: 2023-team-project/2023-yozm-cafe/dev/images | ||
|
||
management: | ||
endpoints: | ||
web: | ||
exposure: | ||
include: prometheus, logfile | ||
|
||
endpoint: | ||
logfile: | ||
external-file: ./logs/spring-boot-logger.log |