|
@@ -0,0 +1,40 @@
|
|
|
+package com.bowintek.practice.util.multipdb;
|
|
|
+
|
|
|
+import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class DataSourceConfig {
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @ConfigurationProperties(prefix = "spring.datasource.smartsearch")
|
|
|
+ public DataSource masterDataSource(){
|
|
|
+ return new DruidDataSourceBuilder().build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @ConfigurationProperties(prefix = "spring.datasource.postgre")
|
|
|
+ public DataSource slaveDataSource(){
|
|
|
+ return new DruidDataSourceBuilder().build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @Primary
|
|
|
+ public DataSource multipleDataSource(DataSource masterDataSource,DataSource slaveDataSource){
|
|
|
+ DynamicDataSource multipleDataSource = new DynamicDataSource();
|
|
|
+ Map<Object, Object> dataSources = new HashMap<>();
|
|
|
+ dataSources.put(DBTypeEnum.SMARTSEARCH.getValue(), masterDataSource);
|
|
|
+ dataSources.put(DBTypeEnum.POSTGRE.getValue(), slaveDataSource);
|
|
|
+ multipleDataSource.setTargetDataSources(dataSources);
|
|
|
+ multipleDataSource.setDefaultTargetDataSource(masterDataSource);
|
|
|
+
|
|
|
+ return multipleDataSource;
|
|
|
+ }
|
|
|
+}
|