配置文件处理,添加druid配置
This commit is contained in:
@@ -0,0 +1,163 @@
|
|||||||
|
package com.ydw.bat.wkflow.config;
|
||||||
|
|
||||||
|
import com.alibaba.druid.filter.Filter;
|
||||||
|
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
|
||||||
|
import com.alibaba.druid.filter.stat.StatFilter;
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import com.alibaba.druid.support.http.StatViewServlet;
|
||||||
|
import com.alibaba.druid.support.http.WebStatFilter;
|
||||||
|
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
|
||||||
|
import com.alibaba.druid.wall.WallConfig;
|
||||||
|
import com.alibaba.druid.wall.WallFilter;
|
||||||
|
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
|
||||||
|
import org.springframework.boot.context.properties.bind.BindResult;
|
||||||
|
import org.springframework.boot.context.properties.bind.Binder;
|
||||||
|
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
|
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||||
|
import org.springframework.context.EnvironmentAware;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableTransactionManagement
|
||||||
|
public class DruidDataSourceConfig implements EnvironmentAware {
|
||||||
|
private Properties propertyResolver;
|
||||||
|
|
||||||
|
private Properties configResolver;
|
||||||
|
|
||||||
|
public DruidDataSourceConfig() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnvironment(Environment env) {
|
||||||
|
Iterable sources = ConfigurationPropertySources.get(env);
|
||||||
|
Binder binder = new Binder(sources);
|
||||||
|
BindResult<Properties> propertyResolverBind = binder.bind("spring.datasource", Properties.class);
|
||||||
|
BindResult<Properties> configResolverBind = binder.bind("spring.profiles", Properties.class);
|
||||||
|
|
||||||
|
propertyResolver= propertyResolverBind.get();
|
||||||
|
configResolver = configResolverBind.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Bean(
|
||||||
|
name = {"dataSource"}
|
||||||
|
)
|
||||||
|
public DataSource dataSource() throws SQLException {
|
||||||
|
DruidDataSource datasource = new DruidDataSource();
|
||||||
|
datasource.setUrl(this.propertyResolver.getProperty("url"));
|
||||||
|
datasource.setDriverClassName(this.propertyResolver.getProperty("driver-class-name"));
|
||||||
|
datasource.setUsername(this.propertyResolver.getProperty("username"));
|
||||||
|
datasource.setPassword(this.propertyResolver.getProperty("password"));
|
||||||
|
datasource.setInitialSize(Integer.valueOf(this.propertyResolver.getProperty("initialSize")));
|
||||||
|
datasource.setMinIdle(Integer.valueOf(this.propertyResolver.getProperty("minIdle")));
|
||||||
|
datasource.setMaxWait(Long.valueOf(this.propertyResolver.getProperty("maxWait")));
|
||||||
|
datasource.setMaxActive(Integer.valueOf(this.propertyResolver.getProperty("maxActive")));
|
||||||
|
datasource.setTimeBetweenEvictionRunsMillis(Long.valueOf(this.propertyResolver.getProperty("timeBetweenEvictionRunsMillis")));
|
||||||
|
datasource.setMinEvictableIdleTimeMillis(Long.valueOf(this.propertyResolver.getProperty("minEvictableIdleTimeMillis")));
|
||||||
|
// datasource.setRemoveAbandoned(Boolean.valueOf(this.propertyResolver.getProperty("removeAbandoned")));
|
||||||
|
// datasource.setRemoveAbandonedTimeout(Integer.valueOf(this.propertyResolver.getProperty("removeAbandonedTimeout")));
|
||||||
|
datasource.setTestOnBorrow(Boolean.valueOf(this.propertyResolver.getProperty("testOnBorrow")));
|
||||||
|
datasource.setTestOnReturn(Boolean.valueOf(this.propertyResolver.getProperty("testOnReturn")));
|
||||||
|
datasource.setTestWhileIdle(Boolean.valueOf(this.propertyResolver.getProperty("testWhileIdle")));
|
||||||
|
datasource.setValidationQuery(this.propertyResolver.getProperty("validationQuery"));
|
||||||
|
// datasource.setValidationQueryTimeout(Integer.valueOf(this.propertyResolver.getProperty("validationQueryTimeout")));
|
||||||
|
// datasource.setKeepAlive(Boolean.valueOf(this.propertyResolver.getProperty("keepAlive")));
|
||||||
|
// if("prd".equals(this.configResolver.getProperty("active"))){
|
||||||
|
// datasource.setConnectProperties(this.connectionProper());
|
||||||
|
// }
|
||||||
|
// datasource.setConnectProperties(this.connectionProper());
|
||||||
|
List<Filter> filters = new ArrayList();
|
||||||
|
filters.add(this.wallFilter());
|
||||||
|
filters.add(this.statFilter());
|
||||||
|
filters.add(this.slf4jLogFilter());
|
||||||
|
// if("prd".equals(this.configResolver.getProperty("active"))){
|
||||||
|
// filters.add(new ConfigFilter());
|
||||||
|
// }
|
||||||
|
// filters.add(new ConfigFilter());
|
||||||
|
datasource.setProxyFilters(filters);
|
||||||
|
// 启动时自动初始化维护连接
|
||||||
|
datasource.init();
|
||||||
|
return datasource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public StatFilter statFilter() {
|
||||||
|
StatFilter statFilter = new StatFilter();
|
||||||
|
statFilter.setMergeSql(Boolean.valueOf(this.propertyResolver.getProperty("mergeSql")));
|
||||||
|
statFilter.setSlowSqlMillis(Long.valueOf(this.propertyResolver.getProperty("slowSqlMillis")));
|
||||||
|
statFilter.setLogSlowSql(Boolean.valueOf(this.propertyResolver.getProperty("logSlowSql")));
|
||||||
|
return statFilter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public WallFilter wallFilter() {
|
||||||
|
WallFilter wallFilter = new WallFilter();
|
||||||
|
wallFilter.setLogViolation(true);
|
||||||
|
wallFilter.setThrowException(false);
|
||||||
|
wallFilter.setConfig(this.wallConfig());
|
||||||
|
return wallFilter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Slf4jLogFilter slf4jLogFilter() {
|
||||||
|
Slf4jLogFilter slf4jLogFilter = new Slf4jLogFilter();
|
||||||
|
return slf4jLogFilter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Properties connectionProper(){
|
||||||
|
Properties connection=new Properties();
|
||||||
|
connection.setProperty("config.decrypt","true");
|
||||||
|
connection.setProperty("config.decrypt.key",this.propertyResolver.getProperty("publicKey"));
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ServletRegistrationBean druidServlet() {
|
||||||
|
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
|
||||||
|
servletRegistrationBean.setServlet(new StatViewServlet());
|
||||||
|
servletRegistrationBean.addUrlMappings(new String[]{"/druid/*"});
|
||||||
|
Map<String, String> initParameters = new HashMap();
|
||||||
|
initParameters.put("resetEnable", "true");
|
||||||
|
initParameters.put("loginUsername", "druid");
|
||||||
|
initParameters.put("loginPassword", "druid");
|
||||||
|
servletRegistrationBean.setInitParameters(initParameters);
|
||||||
|
return servletRegistrationBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FilterRegistrationBean filterRegistrationBean() {
|
||||||
|
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
|
||||||
|
filterRegistrationBean.setFilter(new WebStatFilter());
|
||||||
|
filterRegistrationBean.addUrlPatterns(new String[]{"/*"});
|
||||||
|
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
|
||||||
|
return filterRegistrationBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean({"druid-stat-interceptor"})
|
||||||
|
public DruidStatInterceptor DruidStatInterceptor() {
|
||||||
|
DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();
|
||||||
|
return druidStatInterceptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
|
||||||
|
BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
|
||||||
|
beanNameAutoProxyCreator.setProxyTargetClass(true);
|
||||||
|
beanNameAutoProxyCreator.setInterceptorNames(new String[]{"druid-stat-interceptor"});
|
||||||
|
return beanNameAutoProxyCreator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public WallConfig wallConfig() {
|
||||||
|
WallConfig wallConfig = new WallConfig();
|
||||||
|
wallConfig.setMultiStatementAllow(true);
|
||||||
|
return wallConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,6 +33,33 @@ spring:
|
|||||||
url: jdbc:mysql://test.eqbidding.com:3306/bat-wkflow?nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=utf-8
|
url: jdbc:mysql://test.eqbidding.com:3306/bat-wkflow?nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=utf-8
|
||||||
username: fxd
|
username: fxd
|
||||||
password: 123456
|
password: 123456
|
||||||
|
initialSize: 5
|
||||||
|
minIdle: 5
|
||||||
|
maxActive: 20
|
||||||
|
maxWait: 60000
|
||||||
|
timeBetweenEvictionRunsMillis: 60000
|
||||||
|
minEvictableIdleTimeMillis: 300000
|
||||||
|
validationQuery: SELECT 1 FROM DUAL
|
||||||
|
testWhileIdle: true
|
||||||
|
testOnBorrow: false
|
||||||
|
testOnReturn: false
|
||||||
|
slowSqlMillis: 5000
|
||||||
|
logSlowSql: true
|
||||||
|
# 打开PSCache
|
||||||
|
poolPreparedStatements: true
|
||||||
|
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
|
||||||
|
#如果运行时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
|
||||||
|
#则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j
|
||||||
|
filters: stat,wall
|
||||||
|
#指定每个连接上PSCache的大小
|
||||||
|
maxPoolPreparedStatementPerConnectionSize: 20
|
||||||
|
#合并多个DruidDataSource的监控数据
|
||||||
|
useGlobalDataSourceStat: true
|
||||||
|
stat-view-servlet:
|
||||||
|
enabled: true
|
||||||
|
login-username: druid
|
||||||
|
login-password: druid
|
||||||
|
url-pattern: /druid/*
|
||||||
activiti:
|
activiti:
|
||||||
database-schema-update: true
|
database-schema-update: true
|
||||||
# 自动部署验证设置:true-开启(默认)、false-关闭
|
# 自动部署验证设置:true-开启(默认)、false-关闭
|
||||||
|
|||||||
@@ -39,6 +39,33 @@ spring:
|
|||||||
url: jdbc:mysql://121.36.69.172:3307/foton-bat-wkflow-test?nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=utf-8
|
url: jdbc:mysql://121.36.69.172:3307/foton-bat-wkflow-test?nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=utf-8
|
||||||
username: root
|
username: root
|
||||||
password: hzwlsoft.com
|
password: hzwlsoft.com
|
||||||
|
initialSize: 5
|
||||||
|
minIdle: 5
|
||||||
|
maxActive: 20
|
||||||
|
maxWait: 60000
|
||||||
|
timeBetweenEvictionRunsMillis: 60000
|
||||||
|
minEvictableIdleTimeMillis: 300000
|
||||||
|
validationQuery: SELECT 1 FROM DUAL
|
||||||
|
testWhileIdle: true
|
||||||
|
testOnBorrow: false
|
||||||
|
testOnReturn: false
|
||||||
|
slowSqlMillis: 5000
|
||||||
|
logSlowSql: true
|
||||||
|
# 打开PSCache
|
||||||
|
poolPreparedStatements: true
|
||||||
|
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
|
||||||
|
#如果运行时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
|
||||||
|
#则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j
|
||||||
|
filters: stat,wall
|
||||||
|
#指定每个连接上PSCache的大小
|
||||||
|
maxPoolPreparedStatementPerConnectionSize: 20
|
||||||
|
#合并多个DruidDataSource的监控数据
|
||||||
|
useGlobalDataSourceStat: true
|
||||||
|
stat-view-servlet:
|
||||||
|
enabled: true
|
||||||
|
login-username: druid
|
||||||
|
login-password: druid
|
||||||
|
url-pattern: /druid/*
|
||||||
activiti:
|
activiti:
|
||||||
database-schema-update: true
|
database-schema-update: true
|
||||||
# 自动部署验证设置:true-开启(默认)、false-关闭
|
# 自动部署验证设置:true-开启(默认)、false-关闭
|
||||||
|
|||||||
Reference in New Issue
Block a user