原文链接
前言
Druid是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能。
Spring Boot默认的数据源是:
- 2.0之前:
org.apache.tomcat.jdbc.pool.DataSource
- 2.0及之后:
com.zaxxer.hikari.HikariDataSource
下面来说明如何在 spring Boot 中配置使用Druid。主要包含三个部分:
-
Druid
介绍及文档 - 数据库连接池参数
- 监控统计功能配置
Druid介绍及文档
参加GitHub
数据库连接池参数
# 数据库访问配置
# 主数据源,默认的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
监控统计功能配置
说到底就是Servlet(com.kfit.base.servlet.DruidStatViewServlet
)和Filter(com.kfit.base.servlet.DruidStatFilter
)的配置
- 对于spring mvc使用webx.xml方式
DruidStatView
com.alibaba.druid.support.http.StatViewServlet
resetEnable
true
loginUsername
druid
loginPassword
druid
DruidStatView
/druid/*
druidWebStatFilter
com.alibaba.druid.support.http.WebStatFilter
exclusions
/public/*,*.js,*.css,/druid*,*.jsp,*.swf
principalSessionName
sessionInfo
profileEnable
true
druidWebStatFilter
/*
- 对于Spring Boot有两种配置方式
- 使用原生Servlet和Filter方式配置
- 配置Servlet
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import com.alibaba.druid.support.http.StatViewServlet;
/**
* druid数据源状态监控.
* @author Administrator
*
*/
@WebServlet(urlPatterns="/druid/*",
initParams={
@WebInitParam(name="allow",value="192.168.1.72,127.0.0.1"),// IP白名单(没有配置或者为空,则允许所有访问)
@WebInitParam(name="deny",value="192.168.1.73"),// IP黑名单 (存在共同时,deny优先于allow)
@WebInitParam(name="loginUsername",value="admin"),// 用户名
@WebInitParam(name="loginPassword",value="123456"),// 密码
@WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
}
)
public class DruidStatViewServlet extends StatViewServlet{
privatestaticfinallongserialVersionUID = 1L;
}
- 配置Filter
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;
/**
* druid过滤器.
* @author Administrator
*
*/
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")//忽略资源}
)
public class DruidStatFilter extends WebStatFilter{
}
注意
: 不要忘记在启动类上添加 @ServletComponentScan 注解,spring能够扫描到我们自己编写的servlet和filter,不然就是404了。然后启动项目后访问 http://127.0.0.1:8080/druid/index.html 即可查看数据源及SQL统计等。
- 使用代码注册Servlet和Filter
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
* druid 配置.
*
* 这样的方式不需要添加注解:@ServletComponentScan
*
*/
@Configuration
public class DruidConfiguration {
/**
* 注册一个StatViewServlet
* @return
*/
@Bean
public ServletRegistrationBean DruidStatViewServle(){
//org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
//添加初始化参数:initParams
//白名单:
servletRegistrationBean.addInitParameter("allow","127.0.0.1");
//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
servletRegistrationBean.addInitParameter("deny","192.168.1.73");
//登录查看信息的账号密码.
servletRegistrationBean.addInitParameter("loginUsername","admin");
servletRegistrationBean.addInitParameter("loginPassword","123456");
//是否能够重置数据.
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
}
/**
* 注册一个:filterRegistrationBean
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
启动应用就可以访问:http://127.0.0.1:8080/druid/index.html输入账号和密码:admin/123456 就可以访问了。