|
@@ -1,29 +1,26 @@
|
|
|
package com.zhongzheng.framework.config;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import javax.servlet.Filter;
|
|
|
-import javax.servlet.FilterChain;
|
|
|
-import javax.servlet.ServletException;
|
|
|
-import javax.servlet.ServletRequest;
|
|
|
-import javax.servlet.ServletResponse;
|
|
|
-import javax.sql.DataSource;
|
|
|
-
|
|
|
+import com.alibaba.druid.pool.DruidDataSource;
|
|
|
+import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
|
|
+import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
|
|
|
+import com.alibaba.druid.util.Utils;
|
|
|
import com.zhongzheng.framework.config.properties.DruidProperties;
|
|
|
import com.zhongzheng.framework.datasource.DynamicDataSource;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.DependsOn;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
-import com.alibaba.druid.pool.DruidDataSource;
|
|
|
-import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
|
|
-import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
|
|
|
-import com.alibaba.druid.util.Utils;
|
|
|
-import com.zhongzheng.common.enums.DataSourceType;
|
|
|
-import com.zhongzheng.common.utils.spring.SpringUtils;
|
|
|
+
|
|
|
+import javax.servlet.*;
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* druid 配置多数据源
|
|
@@ -33,51 +30,89 @@ import com.zhongzheng.common.utils.spring.SpringUtils;
|
|
|
@Configuration
|
|
|
public class DruidConfig
|
|
|
{
|
|
|
- @Bean
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 主库数据源bean名称
|
|
|
+ */
|
|
|
+ public static final String MASTER_DATASOURCE = "masterDataSource";
|
|
|
+ /**
|
|
|
+ * 从库数据源bean名称
|
|
|
+ */
|
|
|
+ public static final String SLAVE_DATASOURCE = "slaveDataSource";
|
|
|
+
|
|
|
+
|
|
|
+ @Bean(MASTER_DATASOURCE)
|
|
|
@ConfigurationProperties("spring.datasource.druid.master")
|
|
|
- public DataSource masterDataSource(DruidProperties druidProperties)
|
|
|
+ public DruidDataSource masterDataSource(DruidProperties druidProperties)
|
|
|
{
|
|
|
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
|
|
return druidProperties.dataSource(dataSource);
|
|
|
}
|
|
|
|
|
|
- @Bean
|
|
|
+ @Bean(SLAVE_DATASOURCE)
|
|
|
@ConfigurationProperties("spring.datasource.druid.slave")
|
|
|
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
|
|
|
- public DataSource slaveDataSource(DruidProperties druidProperties)
|
|
|
+ public DruidDataSource slaveDataSource(DruidProperties druidProperties)
|
|
|
{
|
|
|
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
|
|
return druidProperties.dataSource(dataSource);
|
|
|
}
|
|
|
|
|
|
- @Bean(name = "dynamicDataSource")
|
|
|
- @Primary
|
|
|
- public DynamicDataSource dataSource(DataSource masterDataSource)
|
|
|
- {
|
|
|
- Map<Object, Object> targetDataSources = new HashMap<>();
|
|
|
- targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
|
|
|
- setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
|
|
|
- return new DynamicDataSource(masterDataSource, targetDataSources);
|
|
|
- }
|
|
|
-
|
|
|
+// @Bean(name = "dynamicDataSource")
|
|
|
+// @Primary
|
|
|
+// public DynamicDataSource dataSource(DataSource masterDataSource)
|
|
|
+// {
|
|
|
+// Map<Object, Object> targetDataSources = new HashMap<>();
|
|
|
+// targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
|
|
|
+// setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
|
|
|
+// return new DynamicDataSource(masterDataSource, targetDataSources);
|
|
|
+// }
|
|
|
+
|
|
|
/**
|
|
|
- * 设置数据源
|
|
|
- *
|
|
|
- * @param targetDataSources 备选数据源集合
|
|
|
- * @param sourceName 数据源名称
|
|
|
- * @param beanName bean名称
|
|
|
+ * 数据源配置
|
|
|
+ * @param masterDataSource 主库数据源对象
|
|
|
+ * @param slaveDataSource 从库数据源对象
|
|
|
+ * @return DataSource
|
|
|
+ * @Primary 优先使用这个DataSource对象bean
|
|
|
*/
|
|
|
- public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- DataSource dataSource = SpringUtils.getBean(beanName);
|
|
|
- targetDataSources.put(sourceName, dataSource);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ @Bean
|
|
|
+ @Primary
|
|
|
+ @DependsOn(value = {MASTER_DATASOURCE, SLAVE_DATASOURCE})
|
|
|
+ public DataSource routingDataSource(@Qualifier(MASTER_DATASOURCE) DruidDataSource masterDataSource,
|
|
|
+ @Qualifier(SLAVE_DATASOURCE) DruidDataSource slaveDataSource) {
|
|
|
+ if (StringUtils.isBlank(slaveDataSource.getUrl())) {
|
|
|
+ return masterDataSource;
|
|
|
}
|
|
|
+ Map<Object, Object> map = new HashMap<>();
|
|
|
+ map.put(DruidConfig.MASTER_DATASOURCE, masterDataSource);
|
|
|
+ map.put(DruidConfig.SLAVE_DATASOURCE, slaveDataSource);
|
|
|
+ //设置动态数据源
|
|
|
+ DynamicDataSource routing = new DynamicDataSource(masterDataSource,map);
|
|
|
+// //设置动态数据源
|
|
|
+// routing.setTargetDataSources(map);
|
|
|
+// //设置默认数据源
|
|
|
+// routing.setDefaultTargetDataSource(masterDataSource);
|
|
|
+ return routing;
|
|
|
}
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 设置数据源
|
|
|
+// *
|
|
|
+// * @param targetDataSources 备选数据源集合
|
|
|
+// * @param sourceName 数据源名称
|
|
|
+// * @param beanName bean名称
|
|
|
+// */
|
|
|
+// public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
|
|
|
+// {
|
|
|
+// try
|
|
|
+// {
|
|
|
+// DataSource dataSource = SpringUtils.getBean(beanName);
|
|
|
+// targetDataSources.put(sourceName, dataSource);
|
|
|
+// }
|
|
|
+// catch (Exception e)
|
|
|
+// {
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
/**
|
|
|
* 去除监控页面底部的广告
|