Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目。Druid支持所有JDBC兼容的数据库,包括Oracle、MySQL、Derby、PostgreSQL、SQL Server、H2等。Druid在监控、可扩展性、稳定性和性能方面具有明显的优势。通过Druid提供的监控功能,可以实时观察数据库连接池和SQL查询的工作情况。使用Druid连接池,在一定程度上可以提高数据库的访问性能。

本文介绍如何结合Spring Boot开启Druid数据库监控功能。

一、配置Maven依赖

主要加入SpringBootDruid还有MySQL的核心JAR即可。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>springboot.example</groupId>
<artifactId>springboot-druid</artifactId>
<version>1.0-SNAPSHOT</version>
<description>使用Druid连接池提高数据库访问性能</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- springboot操作数据库依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> <!-- mysql连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <!-- Druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.5</version>
</dependency> <!-- 测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies> </project>

二、编写Druid配置文件

Spring Boot的数据源配置默认类型是org.apache.tomcat.jdbc.pool.DataSource,为了使用Druid连接池,可以将数据源类型更换为com.alibaba.druid.pool.DruidDataSource,具体如下代码所示。其中,url,username,password是连接MySQL服务器的配置参数,其他一些参数是设定Druid的工作方式。在resources文件夹下面建立application.yml文件,输入下面的代码完成配置。

spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.25.125:3306/springboot?characterEncoding=utf8
username: root
password: root
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 20
# 配置获取连接等待超时的时间(毫秒)
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置有一个连接在连接池中的最小生存时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
filters: stat, wall, log4j
# 通过connectProperties属性来打开mergeSql功能,慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

上面配置中的filters:stat表示已经可以使用监控过滤器了,这时结合定义一个过滤器,就可以用来监控数据库的使用情况。

三、开启监控功能

开启Druid的监控功能,可以在应用运行期间,通过监控提供的多维度数据来分析使用数据库的运行情况,从而可以调整程序设计,以达到优化数据库访问性能的目的。接下来定义一个监控服务器和一个过滤器,监控服务器设定了访问监控后台的连接地址为“/druid/*”,设定了访问数据库的白名单和黑名单,即通过访问者IP地址来控制访问来源,增加了数据库的安全设置,还设置了一个用来登录监控后台的账户和密码。
代码如下:

package com.lemon.springboot.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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; /**
* Druid配置类
*
* @author lemon
*/
@Configuration
public class DruidConfiguration { /**
* 配置监控服务器
*
* @return 返回监控注册的servlet对象
*/
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
// 添加IP白名单
servletRegistrationBean.addInitParameter("allow", "192.168.25.125,127.0.0.1");
// 添加IP黑名单,当白名单和黑名单重复时,黑名单优先级更高
servletRegistrationBean.addInitParameter("deny", "192.168.25.123");
// 添加控制台管理用户
servletRegistrationBean.addInitParameter("loginUsername", "druid");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
// 是否能够重置数据
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
} /**
* 配置服务过滤器
*
* @return 返回过滤器配置对象
*/
@Bean
public FilterRegistrationBean statFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
// 添加过滤规则
filterRegistrationBean.addUrlPatterns("/*");
// 忽略过滤格式
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,");
return filterRegistrationBean;
}
}

在配置一个Spring Boot应用入口程序:

package com.lemon.springboot.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; /**
* @author lemon
*/
@SpringBootApplication
@ComponentScan(value = {"com.lemon.springboot"})
@EnableAutoConfiguration
public class MainApplication { public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}

直接运行main()方法就开启了监控功能,打开浏览器,可以通过网址http://localhost:8080/druid/index.html打开监控台,输入在配置类中设置的账户druid和密码123456登录就可以查看SQL使用情况了。

转载: https://blog.csdn.net/Lammonpeter/article/details/78468106

最新文章

  1. js二进制与十进制互转
  2. 同步、更新、下载Android Source &amp; SDK from 国内镜像站(转载)
  3. 大数据技术Hadoop入门理论系列之一----hadoop生态圈介绍
  4. 无线安全专题_攻击篇--MAC泛洪攻击
  5. 开发高效的Tag标签系统数据库设计
  6. mysql如何更改数据库名(一键实现mysql改数据库名)
  7. 10 个免费的 jQuery 可视化编辑器插件
  8. JAXB注解【转】
  9. 安卓 安装 platforms 的时候报错--访问 url 出错
  10. Sublime Text使用心得(一)
  11. Unity3D 创建一个简单的2D游戏
  12. Visual Studio项目模板与向导开发
  13. AVL树的旋转操作详解
  14. 知名互联网公司校招 Java 开发岗面试知识点解析
  15. easyui验证扩展
  16. Mac osx 系统安装 eclipse
  17. thinkphp框架,数据动态缓存后,或数据已读取出来,想分页怎么办
  18. ESP32 environment ubuntu
  19. hdu 3038 给区间和,算出多少是错的
  20. 如何学好web安全

热门文章

  1. Prometheus配置文件
  2. CF1188B/E Count Pairs(数学)
  3. 9.本地线程(ThreadLoca)
  4. redis渐进式 rehash
  5. 2019-2020-1 20175313 《信息安全系统设计基础》实现mypwd
  6. Python安装第三方库常用方法
  7. Unity3D 2D模拟经营游戏 洗车沙龙 完整源码
  8. m.baidu.com/?tn=simple 开始有广告了。。。
  9. 批量管理工具:pssh/ansible
  10. configure-nginx-how-to-handle-500-error-on-upstream-itself-while-nginx-handle 自定义header 传递header 定义拦截器