Druid 是一个非常好用的数据库连接池,但是他的好并不止体现在作为一个连接池加快数据访问性能上和连接管理上,他带有一个强大的监控工具:Druid Monitor。不仅可以监控数据源和慢查询,还可以监控 Web 应用、URI 监控、Session 监控、Spring 监控。

1、引入依赖

在 Spring Boot 项目中加入 druid-spring-boot-starter 依赖

Maven:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</version>
</dependency>

Gradle:

compile 'com.alibaba:druid-spring-boot-starter:1.1.16'

2、配置 application.yml

spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: root
password: test123$
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
initialSize: 1
minIdle: 3
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 30000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开 PSCache,并且指定每个连接上 PSCache 的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的 filters,去掉后监控界面 sql 无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
# 通过 connectProperties 属性来打开 mergeSql 功能;慢 SQL 记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个 DruidDataSource 的监控数据
useGlobalDataSourceStat: true
type: com.alibaba.druid.pool.DruidDataSource

属性说明:

spring.datasource.druid.max-active # 最大连接数

spring.datasource.druid.initial-size # 初始化大小

spring.datasource.druid.min-idle # 最小连接数

spring.datasource.druid.max-wait # 获取连接等待超时时间

spring.datasource.druid.time-between-eviction-runs-millis # 间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

spring.datasource.druid.min-evictable-idle-time-millis # 一个连接在池中最小生存的时间,单位是毫秒

spring.datasource.druid.filters=config,stat,wall,log4j # 配置监控统计拦截的 filters,去掉后监控界面 SQL 无法进行统计,wall 用于防火墙

3、配置 DruidMonitorConfig 类

DruidMonitorConfig 类:


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 com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.context.annotation.Primary; import javax.sql.DataSource; @Configuration
public class DruidMonitorConfig { @Primary
@Bean
@ConfigurationProperties("spring.datasource.druid")
public DataSource druidDataSource() {
return DruidDataSourceBuilder.create().build();
} /**
* 注册 ServletRegistrationBean
*
* @return
*/
@Bean
public ServletRegistrationBean registrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
/** 初始化参数配置,initParams**/
// 白名单
bean.addInitParameter("allow", "127.0.0.1");// 多个 ip 逗号隔开
// IP 黑名单 (存在共同时,deny 优先于 allow) : 如果满足 deny 的话提示:Sorry, you are not permitted to view this page.
// bean.addInitParameter("deny", "192.168.1.73");
// 登录查看信息的账号密码.
bean.addInitParameter("loginUsername", "admin");
bean.addInitParameter("loginPassword", "123456");
// 是否能够重置数据.
bean.addInitParameter("resetEnable", "true");
return bean;
} /**
* 注册 FilterRegistrationBean
*
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则.
bean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
bean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return bean;
} }

到此 Spring Boot 项目集成 Druid 监控完成了,启动 Spring Boot 应用程序,打开浏览器,输入:http://localhost:8080/druid/index.html, 登录后即可看到 Druid 的监控界面。

Spring Boot 项目集成 Alibaba Druid的更多相关文章

  1. [转帖]spring boot项目集成jacoco

    小试牛刀:spring boot项目集成jacoco 2019-03-28 20:14:36 zyq23333 阅读数 509   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议, ...

  2. 【spring boot】集成了druid后,同样的mybatis模糊查询语句出错Caused by: com.alibaba.druid.sql.parser.ParserException: syntax error, error in :'name LIKE '%' ? '%'

    druid版本是 <!-- https://mvnrepository.com/artifact/com.alibaba/druid 数据库连接池--> <dependency> ...

  3. 【Spring Boot&&Spring Cloud系列】Spring Boot项目集成Swagger UI

    前言 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...

  4. Spring boot项目集成Neo4j

    第一步,创建Springboot工程 使用Eclipse 创建Maven项目,并修改pom.xml文件为: <?xml version="1.0" encoding=&quo ...

  5. Spring boot项目集成Sharding Jdbc

    环境 jdk:1.8 framework: spring boot, sharding jdbc database: MySQL 搭建步骤 在pom 中加入sharding 依赖 <depend ...

  6. Spring Boot 项目集成Redis

    目录 集成方式 使用Jedis 使用spring-data-redis Redis的安装 绑定配置 获取Redis客户端 Redis工具的编写 使用 集成方式 使用Jedis Jedis是Redis官 ...

  7. spring boot项目集成zuul网关

    1 zuul简介 Zuul 的官方介绍是 “Zuul is the front door for all requests from devices and web sites to the back ...

  8. Spring Boot项目集成flyway

    一.为什么要使用flyway Flyway的定位:数据库的版本控制.   用一种简单.干净的方案,帮助用户完成数据库迁移的工作.使用Flyway,用户可以从任意一个数据库版本迁移到最新版本,简单而且有 ...

  9. 【spring boot】15.spring boot项目 采用Druid数据库连接池,并启用druid监控功能

    在http://www.cnblogs.com/sxdcgaq8080/p/9039442.html的基础上,来看看spring boot项目中采用Druid连接池. GitHub地址:示例代码 == ...

随机推荐

  1. 快速安装python3

    使用 rpm 包进行安装 先来介绍一下 IUS 这个社区,名字的全写是[Inline with Upstream Stable]取首字母,它主要是一个提供新版本RPM包的社区.具体使用可以查看官方文档 ...

  2. node express返回json数据给前端

    请注意,使用res.json()的格式 1.前面不能添加  res.writeHead(200, {'content-type': 'text/plain;charset=utf-8'}); axio ...

  3. osg::PagedLOD example

    int main() { osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer; osg::ref_ptr< ...

  4. hue集成mysql找不到 libmysqlclient.so.16问题解决

    首先我的配置文件如下,这个是没有问题的 但是在重启hue连接mysql时,却发生了如下问题: 这个错误的意思就是没有找到libmysqlclient_r.so.16这个文件,可能是我安装的mysql有 ...

  5. SSM配置基于注解AOP

    pom.xml <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...

  6. python调用HTMLTestRunner+unittest实现一次执行多个测试类,并生成与每个测试类对应的测试报告,具体看代码,附上整个project代码

    python自动化框架雏形,根据自己需要封装:ui自动化,接口自动化均可适用,python版本为python3.x,不要问我为什么不用python2.x,附上整个project代码:http://fi ...

  7. Django中使用Bootstrap----带view.py视图函数(也就是项目下的脚本文件)

    一.Django中使用Bootstrap 1.首先建立工程,建立工程请参照:https://www.cnblogs.com/effortsing/p/10394511.html 2.在Firstdja ...

  8. 解决kalilinux:“下列签名无效: KEYEXPIRED 1425567400"

    Kali linux由于太长时间未更新,而出现GPG错误 KEYEXPIRED 1425567400.经检查源未出现问题可以解析,deb也不冲突,就是密钥过期了. 解决方式 使用一条命令,添加新的密钥 ...

  9. MATLAB分类与预测算法函数

    1.glmfit() 功能:构建一个广义线性回归模型. 使用格式:b=glmfit(X,y,distr),根据属性数据X以及每个记录对应的类别数据y构建一个线性回归模型,distr可取值为:binom ...

  10. P1020 【导弹拦截】

    题目连接嘤嘤嘤~~ 这个题目还是比较难的(至少对我来说是酱紫的嘤嘤嘤).. 第一问,看题解好像用的都是DP,但其实可以用二分,求最长不上升子序列,因为只要输出答案,不用输出方案,时间复杂度n leg( ...