Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目。Druid支持所有JDBC兼容数据库,包括了Oracle、MySQL、PostgreSQL、SQL Server、H2等。
Druid在监控、可扩展性、稳定性和性能方面具有明显的优势。通过Druid提供的监控功能,可以实时观察数据库连接池和SQL查询的工作情况。使用Druid连接池在一定程度上可以提高数据访问效率。
本章目标
SpringBoot整合Druid完成MySQL数据访问,以及配置Druid监控模块整合。
添加Druid依赖
以后章节不做如何创建项目的讲解了,如果需要了解请到前面的章节查看,谢谢!
因为阿里巴巴开源了druid连接池源码,我们可以通过maven仓库可以获得jar包依赖。访问mvnrepository.com/artifact/com.alibaba/druid选择1.0.29,点击进入后复制maven内容到pom.xml内即可,如下图1所示:

当我们添加maven依赖的时候,项目是不会自动导入的,这时你会看到又下角有个提示,"Enable Auto import"我们点击该选项即可自动下载maven依赖的jar到本地.m2目录并构建到项目中。
添加Druid配置
我们已经将druid添加到我们的项目中,那么接下来我们需要修改application.yml配置文件,添加druid的支持,如下图2所示:

上面配置中的filters:stat表示已经可以使用监控过滤器,这时结合定义一个过滤器,就可以用来监控数据库的使用情况。
初尝试运行项目
我们把第三章:SpringBoot使用SpringDataJPA完成CRUD的数据操作部分代码复制到我们本章项目内,需要复制UserController、UserJPA、UserEntity等到目录结构下,如下图3所示:

上述图3已经把对应的类复制到了本章项目内,我们尝试启动项目,启动日志如下图4所示:

可以看到我红色标注的地方,SpringBoot已经把Druid当做dataSource加载到了项目中,那么我们现在访问用户列表地址:127.0.0.1:8080/user/list,效果如下图5所示:

可以看到我们已经可以访问到用户列表数据,证明我们已经配置成功了,如果中途你出现了什么问题请检查copy过来的类导包是否正确、以及application.yml配置文件内的配置是否正确。
我在再来看下IntelliJ IDEA工具控制台的输出日志,如下图6所示:

上述图6内打印了sql语句,而这个sql则是SpringDataJpa自动生成的,我们已经完成了SpringBoot整合Druid连接池的部分,那么我们接下来开启Druid的监控功能。
开启Druid监控功能
开启监控功能,可以在应用运行的过程中,通过监控提供的多维度数据来分析使用数据库的运行情况,从而可以调整程序设计,以便于优化数据库的访问性能。
下面我们来实现Druid的访问Servlet以及Filter,如下图7所示:

我们已经配置完成了Druid的监控,我们现在来重启项目,查看IntellJ IDEA工具的控制台是否正常运行,如果正常证明你已经配置成功,如果不正请查看配置是否正确,上图7中的@Configuration注解是用来配置SpringBoot项目的配置注解,如果将该注解配置在实体类上,该类内的所有bean以及配置都会应用的全局。
项目运行成功后,我们要访问Druid的监控界面,访问地址:127.0.0.1:8080/druid/login.html,效果如下图8所示:

上述图8我们看到了我们成功的访问了Druid的监控页面,那么我们现在输入我们在DruidConfiguration内配置的用户名、密码登录监控平台,进入监控平台首页,如下图9所示:

我们已经成功的访问到了监控首页,可以看到大致包含了如下几个模块:数据源、SQL监控、SQL防火墙、Web应用、URI监控、Session监控、JSONAPI等。
数据源
可以看到项目中管理的所有数据源配置的详细情况,除了密码没有显示外其他都在。
SQL监控
可以查看所有的执行sql语句
SQL防火墙
druid提供了黑白名单的访问,可以清楚的看到sql防护情况。
Web应用
可以看到目前运行的web程序的详细信息。
URI监控
可以监控到所有的请求路径的请求次数、请求时间等其他参数。
Session监控
可以看到当前的session状况,创建时间、最后活跃时间、请求次数、请求时间等详细参数。
JSONAPI
通过api的形式访问Druid的监控接口,api接口返回Json形式数据。
总结
上述讲解就是本章的全部内容,本章主要讲解了SpringBoot整合Druid数据库连接池完成SpringDataJpa访问MySQL数据,开启了Druid连接池本身自带的数据监控功能,可以清晰的看到SQL执行以及Session活跃情况,方便优化SQL。
本章节的代码已经上传到码云:
SpringBoot配套源码地址:https://gitee.com/hengboy/spring-boot-chapter
SpringCloud配套源码地址:https://gitee.com/hengboy/spring-cloud-chapter
SpringBoot相关系列文章请访问:目录:SpringBoot学习目录
QueryDSL相关系列文章请访问:QueryDSL通用查询框架学习目录
SpringDataJPA相关系列文章请访问:目录:SpringDataJPA学习目录
SpringBoot相关文章请访问:目录:SpringBoot学习目录,感谢阅读!
欢迎加入QQ技术交流群,共同进步。

网友评论
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource(){
return new DruidDataSource();
}
在pom文件中加入
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
在用springboot2.0.3版本出现
Failed to bind properties under 'spring.datasource' to com.alibaba.druid.pool.DruidDataSource:
Property: spring.datasource.filters
Value: stat, wall, log4j
Origin: class path resource [application.yml]:29:14
Reason: org.apache.log4j.Logger
Action:
Update your application's configuration
出错在log4j,把配置中的log4j去除,可以运行,也能看到sql监控,请问这个log4j配置的作用是什么
两种方式解决:
1、在你的自动配置类里DruidConfiguration添加以下方法
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
2、将Druid的pom坐标替换成starter
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
druid换成这个包就可以看到SQL监控了
@Configuration
public class DruidConfiguration {
@Bean
public ServletRegistrationBean statViewServlet() {
//创建servlet注册实体
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
//设置ip白名单
servletRegistrationBean.addInitParameter("allow","127.0.0.1");
//设置ip黑名单,如果allow于deny共同存在时,deny优先于allow
servletRegistrationBean.addInitParameter("deny","192.168.0.19");
//设置控制台管理用户
servletRegistrationBean.addInitParameter("loginUsername","druid");
servletRegistrationBean.addInitParameter("loginPassword","123456");
//是否可以重置数据
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean statFilter() {
//创建过滤器
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//设置过滤器过滤路径
filterRegistrationBean.addUrlPatterns("/*");
//忽略过滤的形式
filterRegistrationBean.addInitParameter("exclusions","*.js,*gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource(){
return new DruidDataSource();
}
可以看到sql监控