zhousq
12 months ago
18 changed files with 367 additions and 328 deletions
@ -1,126 +1,126 @@ |
|||||
package com.lzbi.framework.config; |
package com.lzbi.framework.config; |
||||
|
|
||||
import java.io.IOException; |
//import java.io.IOException;
|
||||
import java.util.HashMap; |
//import java.util.HashMap;
|
||||
import java.util.Map; |
//import java.util.Map;
|
||||
import javax.servlet.Filter; |
//import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain; |
//import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException; |
//import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest; |
//import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse; |
//import javax.servlet.ServletResponse;
|
||||
import javax.sql.DataSource; |
//import javax.sql.DataSource;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
//import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean; |
//import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean; |
//import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration; |
//import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary; |
//import org.springframework.context.annotation.Primary;
|
||||
import com.alibaba.druid.pool.DruidDataSource; |
//import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; |
//import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; |
//import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
|
||||
import com.alibaba.druid.util.Utils; |
//import com.alibaba.druid.util.Utils;
|
||||
import com.lzbi.common.enums.DataSourceType; |
//import com.lzbi.common.enums.DataSourceType;
|
||||
import com.lzbi.common.utils.spring.SpringUtils; |
//import com.lzbi.common.utils.spring.SpringUtils;
|
||||
import com.lzbi.framework.config.properties.DruidProperties; |
//import com.lzbi.framework.config.properties.DruidProperties;
|
||||
import com.lzbi.framework.datasource.DynamicDataSource; |
//import com.lzbi.framework.datasource.DynamicDataSource;
|
||||
|
|
||||
/** |
/** |
||||
* druid 配置多数据源 |
* druid 配置多数据源 |
||||
* |
* |
||||
* @author ruoyi |
* @author ruoyi |
||||
*/ |
*/ |
||||
@Configuration |
//@Configuration
|
||||
public class DruidConfig |
public class DruidConfig |
||||
{ |
{ |
||||
@Bean |
// @Bean
|
||||
@ConfigurationProperties("spring.datasource.druid.master") |
// @ConfigurationProperties("spring.datasource.druid.master")
|
||||
public DataSource masterDataSource(DruidProperties druidProperties) |
// public DataSource masterDataSource(DruidProperties druidProperties)
|
||||
{ |
// {
|
||||
DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); |
// DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||
return druidProperties.dataSource(dataSource); |
// return druidProperties.dataSource(dataSource);
|
||||
} |
// }
|
||||
|
//
|
||||
@Bean |
// @Bean
|
||||
@ConfigurationProperties("spring.datasource.druid.slave") |
// @ConfigurationProperties("spring.datasource.druid.slave")
|
||||
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") |
// @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
|
||||
public DataSource slaveDataSource(DruidProperties druidProperties) |
// public DataSource slaveDataSource(DruidProperties druidProperties)
|
||||
{ |
// {
|
||||
DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); |
// DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||
return druidProperties.dataSource(dataSource); |
// return druidProperties.dataSource(dataSource);
|
||||
} |
// }
|
||||
|
//
|
||||
@Bean(name = "dynamicDataSource") |
// @Bean(name = "dynamicDataSource")
|
||||
@Primary |
// @Primary
|
||||
public DynamicDataSource dataSource(DataSource masterDataSource) |
// public DynamicDataSource dataSource(DataSource masterDataSource)
|
||||
{ |
// {
|
||||
Map<Object, Object> targetDataSources = new HashMap<>(); |
// Map<Object, Object> targetDataSources = new HashMap<>();
|
||||
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); |
// targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
|
||||
setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource"); |
// setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
|
||||
return new DynamicDataSource(masterDataSource, targetDataSources); |
// return new DynamicDataSource(masterDataSource, targetDataSources);
|
||||
} |
// }
|
||||
|
//
|
||||
/** |
// /**
|
||||
* 设置数据源 |
// * 设置数据源
|
||||
* |
// *
|
||||
* @param targetDataSources 备选数据源集合 |
// * @param targetDataSources 备选数据源集合
|
||||
* @param sourceName 数据源名称 |
// * @param sourceName 数据源名称
|
||||
* @param beanName bean名称 |
// * @param beanName bean名称
|
||||
*/ |
// */
|
||||
public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName) |
// public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
|
||||
{ |
// {
|
||||
try |
// try
|
||||
{ |
// {
|
||||
DataSource dataSource = SpringUtils.getBean(beanName); |
// DataSource dataSource = SpringUtils.getBean(beanName);
|
||||
targetDataSources.put(sourceName, dataSource); |
// targetDataSources.put(sourceName, dataSource);
|
||||
} |
// }
|
||||
catch (Exception e) |
// catch (Exception e)
|
||||
{ |
// {
|
||||
} |
// }
|
||||
} |
// }
|
||||
|
//
|
||||
/** |
// /**
|
||||
* 去除监控页面底部的广告 |
// * 去除监控页面底部的广告
|
||||
*/ |
// */
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" }) |
// @SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Bean |
// @Bean
|
||||
@ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true") |
// @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
|
||||
public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) |
// public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
|
||||
{ |
// {
|
||||
// 获取web监控页面的参数
|
// // 获取web监控页面的参数
|
||||
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet(); |
// DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
|
||||
// 提取common.js的配置路径
|
// // 提取common.js的配置路径
|
||||
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*"; |
// String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
|
||||
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js"); |
// String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
|
||||
final String filePath = "support/http/resources/js/common.js"; |
// final String filePath = "support/http/resources/js/common.js";
|
||||
// 创建filter进行过滤
|
// // 创建filter进行过滤
|
||||
Filter filter = new Filter() |
// Filter filter = new Filter()
|
||||
{ |
// {
|
||||
@Override |
// @Override
|
||||
public void init(javax.servlet.FilterConfig filterConfig) throws ServletException |
// public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
|
||||
{ |
// {
|
||||
} |
// }
|
||||
@Override |
// @Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
// public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException |
// throws IOException, ServletException
|
||||
{ |
// {
|
||||
chain.doFilter(request, response); |
// chain.doFilter(request, response);
|
||||
// 重置缓冲区,响应头不会被重置
|
// // 重置缓冲区,响应头不会被重置
|
||||
response.resetBuffer(); |
// response.resetBuffer();
|
||||
// 获取common.js
|
// // 获取common.js
|
||||
String text = Utils.readFromResource(filePath); |
// String text = Utils.readFromResource(filePath);
|
||||
// 正则替换banner, 除去底部的广告信息
|
// // 正则替换banner, 除去底部的广告信息
|
||||
text = text.replaceAll("<a.*?banner\"></a><br/>", ""); |
// text = text.replaceAll("<a.*?banner\"></a><br/>", "");
|
||||
text = text.replaceAll("powered.*?shrek.wang</a>", ""); |
// text = text.replaceAll("powered.*?shrek.wang</a>", "");
|
||||
response.getWriter().write(text); |
// response.getWriter().write(text);
|
||||
} |
// }
|
||||
@Override |
// @Override
|
||||
public void destroy() |
// public void destroy()
|
||||
{ |
// {
|
||||
} |
// }
|
||||
}; |
// };
|
||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean(); |
// FilterRegistrationBean registrationBean = new FilterRegistrationBean();
|
||||
registrationBean.setFilter(filter); |
// registrationBean.setFilter(filter);
|
||||
registrationBean.addUrlPatterns(commonJsPattern); |
// registrationBean.addUrlPatterns(commonJsPattern);
|
||||
return registrationBean; |
// return registrationBean;
|
||||
} |
// }
|
||||
} |
} |
||||
|
Loading…
Reference in new issue