|
@@ -1,26 +1,6 @@
|
|
|
package com.zhongzheng.framework.config;
|
|
|
|
|
|
-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 javax.servlet.*;
|
|
|
-import javax.sql.DataSource;
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* druid 配置多数据源
|
|
@@ -30,133 +10,133 @@ import java.util.Map;
|
|
|
@Configuration
|
|
|
public class DruidConfig
|
|
|
{
|
|
|
-
|
|
|
- /**
|
|
|
- * 主库数据源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 DruidDataSource masterDataSource(DruidProperties druidProperties)
|
|
|
- {
|
|
|
- DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
|
|
- return druidProperties.dataSource(dataSource);
|
|
|
- }
|
|
|
-
|
|
|
- @Bean(SLAVE_DATASOURCE)
|
|
|
- @ConfigurationProperties("spring.datasource.druid.slave")
|
|
|
- @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
|
|
|
- public DruidDataSource slaveDataSource(DruidProperties druidProperties)
|
|
|
- {
|
|
|
- DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
|
|
- return druidProperties.dataSource(dataSource);
|
|
|
- }
|
|
|
-
|
|
|
-// @Bean(name = "dynamicDataSource")
|
|
|
-// @Primary
|
|
|
-// public DynamicDataSource dataSource(DataSource masterDataSource)
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 主库数据源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 DruidDataSource masterDataSource(DruidProperties druidProperties)
|
|
|
// {
|
|
|
-// Map<Object, Object> targetDataSources = new HashMap<>();
|
|
|
-// targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
|
|
|
-// setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
|
|
|
-// return new DynamicDataSource(masterDataSource, targetDataSources);
|
|
|
+// DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
|
|
+// return druidProperties.dataSource(dataSource);
|
|
|
// }
|
|
|
-
|
|
|
- /**
|
|
|
- * 数据源配置
|
|
|
- * @param masterDataSource 主库数据源对象
|
|
|
- * @param slaveDataSource 从库数据源对象
|
|
|
- * @return DataSource
|
|
|
- * @Primary 优先使用这个DataSource对象bean
|
|
|
- */
|
|
|
- @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);
|
|
|
+//
|
|
|
+// @Bean(SLAVE_DATASOURCE)
|
|
|
+// @ConfigurationProperties("spring.datasource.druid.slave")
|
|
|
+// @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
|
|
|
+// 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);
|
|
|
+//// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 数据源配置
|
|
|
+// * @param masterDataSource 主库数据源对象
|
|
|
+// * @param slaveDataSource 从库数据源对象
|
|
|
+// * @return DataSource
|
|
|
+// * @Primary 优先使用这个DataSource对象bean
|
|
|
+// */
|
|
|
+// @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);
|
|
|
// //设置动态数据源
|
|
|
-// routing.setTargetDataSources(map);
|
|
|
-// //设置默认数据源
|
|
|
-// routing.setDefaultTargetDataSource(masterDataSource);
|
|
|
- return routing;
|
|
|
- }
|
|
|
-
|
|
|
+// 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)
|
|
|
+//// {
|
|
|
+//// }
|
|
|
+//// }
|
|
|
+//
|
|
|
// /**
|
|
|
-// * 设置数据源
|
|
|
-// *
|
|
|
-// * @param targetDataSources 备选数据源集合
|
|
|
-// * @param sourceName 数据源名称
|
|
|
-// * @param beanName bean名称
|
|
|
+// * 去除监控页面底部的广告
|
|
|
// */
|
|
|
-// public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
|
|
|
+// @SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
+// @Bean
|
|
|
+// @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
|
|
|
+// public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
|
|
|
// {
|
|
|
-// try
|
|
|
-// {
|
|
|
-// DataSource dataSource = SpringUtils.getBean(beanName);
|
|
|
-// targetDataSources.put(sourceName, dataSource);
|
|
|
-// }
|
|
|
-// catch (Exception e)
|
|
|
+// // 获取web监控页面的参数
|
|
|
+// DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
|
|
|
+// // 提取common.js的配置路径
|
|
|
+// String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
|
|
|
+// String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
|
|
|
+// final String filePath = "support/http/resources/js/common.js";
|
|
|
+// // 创建filter进行过滤
|
|
|
+// Filter filter = new Filter()
|
|
|
// {
|
|
|
-// }
|
|
|
+// @Override
|
|
|
+// public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
|
|
|
+// {
|
|
|
+// }
|
|
|
+// @Override
|
|
|
+// public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
|
|
+// throws IOException, ServletException
|
|
|
+// {
|
|
|
+// chain.doFilter(request, response);
|
|
|
+// // 重置缓冲区,响应头不会被重置
|
|
|
+//// response.resetBuffer();
|
|
|
+// // 获取common.js
|
|
|
+// String text = Utils.readFromResource(filePath);
|
|
|
+// // 正则替换banner, 除去底部的广告信息
|
|
|
+// text = text.replaceAll("<a.*?banner\"></a><br/>", "");
|
|
|
+// text = text.replaceAll("powered.*?shrek.wang</a>", "");
|
|
|
+// response.getWriter().write(text);
|
|
|
+// }
|
|
|
+// @Override
|
|
|
+// public void destroy()
|
|
|
+// {
|
|
|
+// }
|
|
|
+// };
|
|
|
+// FilterRegistrationBean registrationBean = new FilterRegistrationBean();
|
|
|
+// registrationBean.setFilter(filter);
|
|
|
+// registrationBean.addUrlPatterns(commonJsPattern);
|
|
|
+// return registrationBean;
|
|
|
// }
|
|
|
-
|
|
|
- /**
|
|
|
- * 去除监控页面底部的广告
|
|
|
- */
|
|
|
- @SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
- @Bean
|
|
|
- @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
|
|
|
- public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
|
|
|
- {
|
|
|
- // 获取web监控页面的参数
|
|
|
- DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
|
|
|
- // 提取common.js的配置路径
|
|
|
- String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
|
|
|
- String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
|
|
|
- final String filePath = "support/http/resources/js/common.js";
|
|
|
- // 创建filter进行过滤
|
|
|
- Filter filter = new Filter()
|
|
|
- {
|
|
|
- @Override
|
|
|
- public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
|
|
|
- {
|
|
|
- }
|
|
|
- @Override
|
|
|
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
|
|
- throws IOException, ServletException
|
|
|
- {
|
|
|
- chain.doFilter(request, response);
|
|
|
- // 重置缓冲区,响应头不会被重置
|
|
|
-// response.resetBuffer();
|
|
|
- // 获取common.js
|
|
|
- String text = Utils.readFromResource(filePath);
|
|
|
- // 正则替换banner, 除去底部的广告信息
|
|
|
- text = text.replaceAll("<a.*?banner\"></a><br/>", "");
|
|
|
- text = text.replaceAll("powered.*?shrek.wang</a>", "");
|
|
|
- response.getWriter().write(text);
|
|
|
- }
|
|
|
- @Override
|
|
|
- public void destroy()
|
|
|
- {
|
|
|
- }
|
|
|
- };
|
|
|
- FilterRegistrationBean registrationBean = new FilterRegistrationBean();
|
|
|
- registrationBean.setFilter(filter);
|
|
|
- registrationBean.addUrlPatterns(commonJsPattern);
|
|
|
- return registrationBean;
|
|
|
- }
|
|
|
}
|