|
| 1 | +package com.allog.dallog.global.config.replication; |
| 2 | + |
| 3 | +import static com.allog.dallog.global.config.replication.DataSourceKey.KeyName.REPLICA_1_NAME; |
| 4 | +import static com.allog.dallog.global.config.replication.DataSourceKey.KeyName.REPLICA_2_NAME; |
| 5 | +import static com.allog.dallog.global.config.replication.DataSourceKey.KeyName.SOURCE_NAME; |
| 6 | +import static com.allog.dallog.global.config.replication.DataSourceKey.REPLICA_1; |
| 7 | +import static com.allog.dallog.global.config.replication.DataSourceKey.REPLICA_2; |
| 8 | +import static com.allog.dallog.global.config.replication.DataSourceKey.SOURCE; |
| 9 | + |
| 10 | +import java.util.Map; |
| 11 | +import javax.sql.DataSource; |
| 12 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 13 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 14 | +import org.springframework.boot.jdbc.DataSourceBuilder; |
| 15 | +import org.springframework.context.annotation.Bean; |
| 16 | +import org.springframework.context.annotation.Configuration; |
| 17 | +import org.springframework.context.annotation.Primary; |
| 18 | +import org.springframework.context.annotation.Profile; |
| 19 | +import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; |
| 20 | + |
| 21 | +@Configuration |
| 22 | +@Profile({"prod", "dev"}) |
| 23 | +public class DataSourceConfiguration { |
| 24 | + |
| 25 | + @Bean |
| 26 | + @Primary |
| 27 | + public DataSource dataSource() { |
| 28 | + DataSource determinedDataSource = routingDataSource(sourceDataSource(), replica1DataSource(), |
| 29 | + replica2DataSource()); |
| 30 | + return new LazyConnectionDataSourceProxy(determinedDataSource); |
| 31 | + } |
| 32 | + |
| 33 | + @Bean |
| 34 | + @Qualifier(SOURCE_NAME) |
| 35 | + @ConfigurationProperties(prefix = "spring.datasource.source") |
| 36 | + public DataSource sourceDataSource() { |
| 37 | + return DataSourceBuilder.create() |
| 38 | + .build(); |
| 39 | + } |
| 40 | + |
| 41 | + @Bean |
| 42 | + @Qualifier(REPLICA_1_NAME) |
| 43 | + @ConfigurationProperties(prefix = "spring.datasource.replica1") |
| 44 | + public DataSource replica1DataSource() { |
| 45 | + return DataSourceBuilder.create() |
| 46 | + .build(); |
| 47 | + } |
| 48 | + |
| 49 | + @Bean |
| 50 | + @Qualifier(REPLICA_2_NAME) |
| 51 | + @ConfigurationProperties(prefix = "spring.datasource.replica2") |
| 52 | + public DataSource replica2DataSource() { |
| 53 | + return DataSourceBuilder.create() |
| 54 | + .build(); |
| 55 | + } |
| 56 | + |
| 57 | + @Bean |
| 58 | + public DataSource routingDataSource( |
| 59 | + @Qualifier(SOURCE_NAME) DataSource sourceDataSource, |
| 60 | + @Qualifier(REPLICA_1_NAME) DataSource replica1DataSource, |
| 61 | + @Qualifier(REPLICA_2_NAME) DataSource replica2DataSource |
| 62 | + ) { |
| 63 | + Map<Object, Object> dataSources = Map.of( |
| 64 | + SOURCE, sourceDataSource, REPLICA_1, replica1DataSource, REPLICA_2, replica2DataSource |
| 65 | + ); |
| 66 | + |
| 67 | + RoutingDataSource routingDataSource = new RoutingDataSource(); |
| 68 | + routingDataSource.setTargetDataSources(dataSources); |
| 69 | + routingDataSource.setDefaultTargetDataSource(sourceDataSource); |
| 70 | + |
| 71 | + return routingDataSource; |
| 72 | + } |
| 73 | +} |
0 commit comments