This commit is contained in:
xiangwbs
2018-03-17 23:19:02 +08:00
parent a715d6556f
commit 2ab96a1113
10 changed files with 182 additions and 33 deletions
+5
View File
@@ -39,6 +39,11 @@
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency> <!-- druid连接池 -->
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.20</version>
</dependency>
<!-- lombok:实体类自动生成getting和setting方法,以及equals,hashCode,toString等 -->
<dependency>
<groupId>org.projectlombok</groupId>
@@ -0,0 +1,32 @@
package com.xwbing.domain.entity.model;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Data
@Component
@PropertySource("classpath:druid.properties")
@ConfigurationProperties(prefix = "jdbc")
public class DruidDataSourceModel {
private String url;
private String username;
private String password;
private String driver;
private int initSize;
private int minIdle;
private int maxActive;
private int maxWait;
private int timeBetweenEvictionRunsMillis;
private int minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize;
private String filters;
private String connectionProperties;
private boolean useGlobalDataSourceStat;
}
@@ -0,0 +1,29 @@
# 数据库基本信息配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/springboot2?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=
# 初始化大小,最小,最大
jdbc.initSize=1
jdbc.minIdle=3
jdbc.maxActive=20
# 获取连接最大等待时间
jdbc.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
jdbc.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
jdbc.minEvictableIdleTimeMillis=30000
# 连接性能检测(用来检测连接是否有效的sql,为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用)
jdbc.validationQuery=SELECT 'x'
jdbc.testWhileIdle=true
jdbc.testOnBorrow=false
jdbc.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小 ,mysql建议false
jdbc.poolPreparedStatements=false
jdbc.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters
jdbc.filters=stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
jdbc.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#jdbc.useGlobalDataSourceStat=true
+1
View File
@@ -81,6 +81,7 @@
<artifactId>commons-fileupload</artifactId>
<version>${fileUpLoad.version}</version>
</dependency>
</dependencies>
<build>
<finalName>boot-web</finalName>
@@ -0,0 +1,54 @@
package com.xwbing.configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.xwbing.domain.entity.model.DruidDataSourceModel;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.annotation.Resource;
import java.sql.SQLException;
/**
* 说明: 德鲁伊监控:druid/index.html
* 项目名称: boot-module-demo
* 创建时间: 2017/5/10 16:36
* 作者: xiangwb
*/
@Configuration
@EnableConfigurationProperties(DruidDataSourceModel.class)
public class DruidConfig {
@Resource
private DruidDataSourceModel druidDataSourceModel;
@Bean
@Primary
public DruidDataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(druidDataSourceModel.getUrl());
dataSource.setDriverClassName(druidDataSourceModel.getDriver());
dataSource.setUsername(druidDataSourceModel.getUsername());
dataSource.setPassword(druidDataSourceModel.getPassword());
dataSource.setInitialSize(druidDataSourceModel.getInitSize());
dataSource.setMinIdle(druidDataSourceModel.getMinIdle());
dataSource.setMaxActive(druidDataSourceModel.getMaxActive());
dataSource.setMaxWait(druidDataSourceModel.getMaxWait());
dataSource.setTimeBetweenEvictionRunsMillis(druidDataSourceModel.getTimeBetweenEvictionRunsMillis());
dataSource.setMinEvictableIdleTimeMillis(druidDataSourceModel.getMinEvictableIdleTimeMillis());
dataSource.setValidationQuery(druidDataSourceModel.getValidationQuery());
dataSource.setTestWhileIdle(druidDataSourceModel.isTestWhileIdle());
dataSource.setTestOnBorrow(druidDataSourceModel.isTestOnBorrow());
dataSource.setTestOnReturn(druidDataSourceModel.isTestOnReturn());
dataSource.setPoolPreparedStatements(druidDataSourceModel.isPoolPreparedStatements());
dataSource.setMaxPoolPreparedStatementPerConnectionSize(druidDataSourceModel.getMaxPoolPreparedStatementPerConnectionSize());
dataSource.setConnectionProperties(druidDataSourceModel.getConnectionProperties());
dataSource.setUseGlobalDataSourceStat(druidDataSourceModel.isUseGlobalDataSourceStat());
try {
dataSource.setFilters(druidDataSourceModel.getFilters());
} catch (SQLException e) {
e.printStackTrace();
}
return dataSource;
}
}
@@ -1,31 +0,0 @@
package com.xwbing.configuration;
import com.xwbing.util.captcha.CaptchaServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 说明: 统一的servlet配置
* 项目名称: boot-module-demo
* 创建时间: 2017/5/10 16:36
* 作者: xiangwb
*/
@Configuration
public class ServletConfig {
private final Logger logger = LoggerFactory.getLogger(ServletConfig.class);
@Bean
public ServletRegistrationBean captchaServletRegistrationBean() {
logger.info("注册验证码servlet ======================= ");
CaptchaServlet captchaServlet = new CaptchaServlet();
ServletRegistrationBean registration = new ServletRegistrationBean(captchaServlet);
registration.setEnabled(true);
registration.addUrlMappings("/servlet/captcha");
return registration;
}
/********************************************/
}
@@ -0,0 +1,53 @@
package com.xwbing.configuration;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.xwbing.util.captcha.CaptchaServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 说明: 统一的servlet/filter配置
* 项目名称: boot-module-demo
* 创建时间: 2017/5/10 16:36
* 作者: xiangwb
*/
@Configuration
public class ServletFilterConfig {
private final Logger logger = LoggerFactory.getLogger(ServletFilterConfig.class);
@Bean
public ServletRegistrationBean captchaServlet() {
logger.info("注册验证码servlet ======================= ");
ServletRegistrationBean registration = new ServletRegistrationBean(new CaptchaServlet());
registration.addUrlMappings("/captcha");
return registration;
}
@Bean
public ServletRegistrationBean druidServlet() {
logger.info("注册druidServlet ======================= ");
ServletRegistrationBean registration = new ServletRegistrationBean(new StatViewServlet());
registration.addUrlMappings("/druid/*");
//添加初始化参数:initParams
// registration.addInitParameter("allow", "127.0.0.1");//IP白名单(没有配置或者为空,则允许所有访问)
// registration.addInitParameter("deny", "192.168.31.234");//IP黑名单(存在共同时,deny优先于allow)
registration.addInitParameter("loginUsername", "admin");//用户名
registration.addInitParameter("loginPassword", "123456");//密码
registration.addInitParameter("resetEnable", "false");//禁用HTML页面上的“Reset All”功能
return registration;
}
/*******************************************************/
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
@@ -31,7 +31,7 @@ public class LoginInterceptor extends HandlerInterceptorAdapter {
//映射swagger文档
set.add("/doc");
//验证码
set.add("/servlet/captcha");
set.add("/captcha");
//swagger
set.add("/v2/api-docs");
set.add("/swagger-resources");
+1 -1
View File
@@ -12,7 +12,7 @@ spring:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot2?useUnicode=true&characterEncoding=utf8
username: root
password: xwbing
password:
#jpa
jpa:
database: mysql
+6
View File
@@ -50,6 +50,12 @@
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- 支持注解@ConfigurationProperties 需要get/set方法-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>