-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Replication ๊ธฐ๋ฅ ์ถ๊ฐ * feat: ์๋ฒ datasource ์ถ๊ฐ
- Loading branch information
Showing
4 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
Submodule secret
updated
from c80dbd to b608f4
55 changes: 55 additions & 0 deletions
55
backend/baton/src/main/java/touch/baton/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,55 @@ | ||
package touch.baton.config; | ||
|
||
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 touch.baton.infra.database.datasource.DataSourceType; | ||
import touch.baton.infra.database.datasource.RoutingDataSource; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Map; | ||
|
||
import static touch.baton.infra.database.datasource.DataSourceType.Name.REPLICA_NAME; | ||
import static touch.baton.infra.database.datasource.DataSourceType.Name.ROUTING_NAME; | ||
import static touch.baton.infra.database.datasource.DataSourceType.Name.SOURCE_NAME; | ||
|
||
@Profile("deploy") | ||
@Configuration | ||
public class DataSourceConfig { | ||
|
||
@Qualifier(SOURCE_NAME) | ||
@ConfigurationProperties(prefix = "spring.datasource.source") | ||
@Bean | ||
public DataSource sourceDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Qualifier(REPLICA_NAME) | ||
@ConfigurationProperties(prefix = "spring.datasource.replica") | ||
@Bean | ||
public DataSource replicaDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Qualifier(ROUTING_NAME) | ||
@Bean | ||
public DataSource routingDataSource(@Qualifier(SOURCE_NAME) final DataSource sourceDataSource, | ||
@Qualifier(REPLICA_NAME) final DataSource replicaDataSource | ||
) { | ||
return RoutingDataSource.createDefaultSetting( | ||
Map.of(DataSourceType.SOURCE, sourceDataSource, | ||
DataSourceType.REPLICA, replicaDataSource) | ||
); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public DataSource dataSource(@Qualifier(ROUTING_NAME) final DataSource replicationRoutingDataSource) { | ||
return new LazyConnectionDataSourceProxy(replicationRoutingDataSource); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/baton/src/main/java/touch/baton/infra/database/datasource/DataSourceType.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,24 @@ | ||
package touch.baton.infra.database.datasource; | ||
|
||
import static touch.baton.infra.database.datasource.DataSourceType.Name.REPLICA_NAME; | ||
import static touch.baton.infra.database.datasource.DataSourceType.Name.SOURCE_NAME; | ||
|
||
public enum DataSourceType { | ||
|
||
SOURCE(SOURCE_NAME), | ||
REPLICA(REPLICA_NAME); | ||
|
||
private final String name; | ||
|
||
DataSourceType(final String name) { | ||
this.name = name; | ||
} | ||
|
||
public static class Name { | ||
|
||
public static final String ROUTING_NAME = "ROUTING"; | ||
public static final String SOURCE_NAME = "SOURCE"; | ||
public static final String REPLICA_NAME = "REPLICA"; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
backend/baton/src/main/java/touch/baton/infra/database/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,27 @@ | ||
package touch.baton.infra.database.datasource; | ||
|
||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
import java.util.Map; | ||
|
||
public class RoutingDataSource extends AbstractRoutingDataSource { | ||
|
||
public static RoutingDataSource createDefaultSetting(final Map<Object, Object> dataSources) { | ||
final RoutingDataSource routingDataSource = new RoutingDataSource(); | ||
routingDataSource.setDefaultTargetDataSource(dataSources.get(DataSourceType.SOURCE)); | ||
routingDataSource.setTargetDataSources(dataSources); | ||
return routingDataSource; | ||
} | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
final boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); | ||
|
||
if (readOnly) { | ||
return DataSourceType.REPLICA; | ||
} | ||
|
||
return DataSourceType.SOURCE; | ||
} | ||
} |