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.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.25.125:3306/springboot?characterEncoding=utf8username: rootpassword: root# 初始化大小,最小,最大initialSize: 5minIdle: 5maxActive: 20# 配置获取连接等待超时的时间(毫秒)maxWait: 60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒timeBetweenEvictionRunsMillis: 60000# 配置有一个连接在连接池中的最小生存时间,单位是毫秒minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: false# 打开PSCache,指定每个连接上PSCache的大小poolPreparedStatements: truemaxPoolPreparedStatementPerConnectionSize: 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对象*/@Beanpublic 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 返回过滤器配置对象*/@Beanpublic 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使用情况了。

参考:http://www.cnblogs.com/han-1034683568/p/6730869.html

druid的后台监控相关推荐

  1. Druid后台监控与过滤器

    Druid是阿里开元的数据源,相较于c3p0,dbcp等数据源具有强大的后台监控功能,刚好最近在学习springboot,涉及到Druid的知识,所以记录一下. springboot实现后台监控主要分 ...

  2. SpringBoot--配置Druid(德鲁伊)数据源监控

    01: Druid(德鲁伊)数据源简介 02: 使用Druid(德鲁伊)数据源 03: 配置Druid(德鲁伊)数据源监控 1. SpringBoot–配置Druid(德鲁伊)数据源监控 Druid ...

  3. sprinigboot(2.2.4)+mysql引入druid的性能监控StateFilter

    根据阿里的druid的官方文档,druid连接池不但性能比现在其他免费的连接池(proxxl.c3p0.dbcp等)性能优秀,更稳定,在高访问下表现优异(阿里自己说经过自己淘宝双十一的检验)而且提供了 ...

  4. Delphi编写后台监控软件

    Delphi编写后台监控软件 文章来源:Delphi程序员之家   后台监控软件,为了达到隐蔽监控的目的,应该满足正常运行时,不显示在任务栏上,在按Ctrl+Alt+Del出现的任务列表中也不显示,管 ...

  5. 阿里Druid连接池监控的两个坑

    转载自 注意:阿里Druid连接池监控的两个坑 阿里的Druid大家都知道是最好的连接池,其强大的监控功能是我们追求的重要特性.但在实际情况中也有不少坑,说下最近遇到的一个坑吧! 问题1:不断打印er ...

  6. druid 连接池监控报错 Sorry, you are not permitted to view this page.

    使用Druid连接池的时候,遇到一个奇怪的问题,在本地(localhost)可以直接打开Druid连接池监控,在其他机器上打开会报错: Sorry, you are not permitted to ...

  7. 简单的实现app界面劫持和后台监控

    如今越来越多的手机玩家都为了折腾一些稀奇古怪的东西,或出于好奇,或出于贪心,往往会root手机去尝试安装一些外挂,刷分,刷粉,修改版等等乱七八糟的东西 正因为此类用户的存在,一些小人就盯上了这方面的市 ...

  8. python adb 实现对支付宝登录及后台监控

    python 加载adb实现对支付宝登录及后台监控,与手机自身(电量,网络状态.进程后台)监控,及实现手机任意位置模拟点击. #!/usr/bin/env python # encoding: utf ...

  9. druid连接池监控

    项目中使用的数据库连接池是阿里开源的druid连接池,经常会有一些场景需要监控和统计数据源.sql的执行情况,因此引入druid提供的监控druid monitor. Druid是一个开源项目,源码托 ...

最新文章

  1. Linux 内核中的 GCC 特性(zz)
  2. C语言实现哈密尔顿hamiltonian算法(附完整源码)
  3. 对于SAP的月结相关流程介绍
  4. bytes转16进制整数 python_Python 十进制转二进制、八进制、十六进制
  5. 8-[多线程] 进程池线程池
  6. 【牛客 - 283H】图论一顿套模板(思维转化,Dijkstra)
  7. Python3基础 try-指定except-as reason 捕获打开一个不存在的文件的时候,会产生OSError异常的示例
  8. bootstrap在php中怎弄,bootstrap插件怎么用
  9. solr带carrot2插件可以用配置好的solrconfig.xml
  10. 银行科技岗位 笔试 专业方向重点 + 面试一般问题
  11. 蓄电池在线监测、蓄电池在线核容系统
  12. Python-脾气暴躁
  13. 程序员修炼(一)----剑指天下
  14. 有关爬虫加载Ajax数据或请求json数据集的(快速高效)方法
  15. Jetseon TX2 IntelRealsense D435i Python
  16. 人力资源管理理论与实务第三章
  17. 网络程序设计 Sockets
  18. 写给初学者的6条网页设计安全配色指南
  19. 安装CDH5.15.0过程详细记录¥坑与解决办法(20180724)
  20. 极光尔沃A6-3d打印机体验

热门文章

  1. 机房服务器维修,机房维护方案
  2. 测试常见面试题(一)
  3. Linux TC流量控制HOWTO中文版
  4. linux 的top命令详解
  5. weblogic相关问题
  6. python填补缺失值数据驱动代码_python填补缺失值数据驱动代码_python数据预处理之缺失值的各种填补方式...
  7. TFTP协议详细分析
  8. Artifact xxx:war exploded: Error during artifact deployment.See server log for details.
  9. 智能小区 安防技术详解及安防隐患杂谈
  10. 数学建模——评价模型