1、加spring boot druid 依赖
注意: 如果spring boot 是2.x 以上,则加入版本较高的druid-spring-boot-starter
2、durid配置文件
在application.properties文件中添加配置
#连接池的配置信息
## 初始化大小,最小,最大
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
## 配置获取连接等待超时的时间
spring.datasource.druid.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.druid.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
3、添加数据源类(java配置方式)
package com.company.project.configurer;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* DruidConfig 读取数据源配置信息并初始化DataSource对象
*/
@Configuration
//@PropertySource(value = "classpath:application-dev.properties")
public class DruidConfig {
/**
* 在同样的DataSource中,首先使用被标注的DataSource(Primary)
* @return
*/
@Bean
@Primary
//统一配置名前缀,可以减少配置代码(不用在写从配置文件读取属性的代码)
@ConfigurationProperties(prefix ="spring.datasource")
public DataSourcedataSource() {
// 配置数据库连接基础信息
DruidDataSource datasource =new DruidDataSource();
return datasource;
}
@Bean
public DataSourceTransactionManagertransactionManager()throws SQLException {
return new DataSourceTransactionManager(dataSource());
}
/**
* 注册一个StatViewServlet
* @return
*/
@Bean
public ServletRegistrationBeanDruidStatViewServle2(){
ServletRegistrationBean servletRegistrationBean =
new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
//添加初始化参数:initParams 部署后可以放开
/** 白名单,如果不配置或value为空,则允许所有 */
servletRegistrationBean.addInitParameter("allow","127.0.0.1");
/** 黑名单,与白名单存在相同IP时,优先于白名单 */
servletRegistrationBean.addInitParameter("deny","192.0.0.1");
/** 用户名 */
servletRegistrationBean.addInitParameter("loginUsername","root");
/** 密码 */
servletRegistrationBean.addInitParameter("loginPassword","123456");
/** 禁用页面上的“Reset All”功能 */
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
}
/**
* 注册一个:WebStatFilter
* @return
*/
@Bean
public FilterRegistrationBeandruidStatFilter2(){
FilterRegistrationBean filterRegistrationBean =new FilterRegistrationBean(new WebStatFilter());
/** 过滤规则 */
filterRegistrationBean.addUrlPatterns("/*");
/** 忽略资源 */
filterRegistrationBean.addInitParameter("exclusions",
"*.html,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
四、监控
访问 http://localhost:8080/druid, 使用上面配置的账号密码。