spring boot整合druid以及druid监控

Druid是Java语言中最好的数据库连接池(我个人知识范围内),并且能够提供强大的监控和扩展功能。


下面来说明如何在 spring Boot 中配置使用druid

1:添加druid和spring boot的依赖

<dependency>
		   <groupId>com.alibaba</groupId>
		   <artifactId>druid-spring-boot-starter</artifactId>
		   <version>1.1.1</version>
		</dependency>
 2:如果spring-boot-starter-parent你用的版本还是1.3.2那现在要升级了,在后面运行项目的时候就报错了.找不到
java.lang.NoClassDefFoundError: org/springframework/boot/web/servlet/ServletRegistrationBean

改为1.5.2吧.比较新

2:在application.properties中添加最基本的数据源配置文件

#数据库配置文件
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xyy_v2?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#连接池配置信息
spring.datasource.initial-size=5  
spring.datasource.min-idle=5  
spring.datasource.max-active=20  
spring.datasource.max-wait=60000  
spring.datasource.time-between-eviction-runs-millis=60000  
spring.datasource.min-evictable-idle-time-millis=300000  
spring.datasource.validation-query=SELECT 1 FROM DUAL  
spring.datasource.test-while-idle=true  
spring.datasource.test-on-borrow=false  
spring.datasource.test-on-return=false  
spring.datasource.pool-prepared-statements=true  
spring.datasource.max-pool-prepared-statement-per-connection-size=20  
spring.datasource.filters=stat 
spring.datasource.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 

3-1:spring boot整合druid的方式也和其它一样,可以分为javabean和配置文件properties的方式

我们先来说说bean的方式吧,直接上代码!

     a>配置监控拦截器

     

package com.xyy.util;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

import com.alibaba.druid.support.http.WebStatFilter;

/**
 * druid监控拦截去
 * @ClassName: DruidStatFilter 
 * @author wangqinghua
 * @date 2017年7月24日 上午10:53:40
 */
@WebFilter(filterName="druidWebStatFilter",
urlPatterns="/*",
initParams={
    @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
})
public class DruidStatFilter extends WebStatFilter {

}
    b>druid监控视图配置

package com.xyy.util;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

import com.alibaba.druid.support.http.StatViewServlet;

/**
 * druid监控视图配置
 * @ClassName: DruidStatViewServlet 
 * @author wangqinghua
 * @date 2017年7月24日 上午10:54:27
 */
@WebServlet(urlPatterns = "/druid/*", initParams={
    @WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)
    @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)
    @WebInitParam(name="loginUsername",value="shanhy"),// 用户名
    @WebInitParam(name="loginPassword",value="shanhypwd"),// 密码
    @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
})
public class DruidStatViewServlet extends StatViewServlet {
	private static final long serialVersionUID = 6588499385893299545L;

}


3-2:配置文件配置的方式application.properties(具体配置视个人而定)

#druid WebStatFilter监控配置
spring.datasource.druid.web-stat-filter.enabled= true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.session-stat-max-count=10
spring.datasource.druid.web-stat-filter.principal-session-name=
spring.datasource.druid.web-stat-filter.principal-cookie-name=
spring.datasource.druid.web-stat-filter.profile-enable=
#druid StatViewServlet监控配置
spring.datasource.druid.stat-view-servlet.enabled= true
spring.datasource.druid.stat-view-servlet.url-pattern= /druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=false
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.allow=192.168.1.110,127.0.0.1
spring.datasource.druid.stat-view-servlet.deny=192.168.16.111
spring.datasource.druid.aop-patterns=com.xyy.service

上述配置具体含义我就不描述了,英文水平可以或者之前有使用过的,应该很清楚 害羞,我就不装逼了,免得有人打我!

4:点击main主入口,运行成功.浏览器输入:http://localhost:8080/druid,帐号:admin 密码:admin,就会看到如下效果了

好了,大功告成!来点掌声!抓狂

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一屁小肥咩

您的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值