欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot中加入druid对sql进行监控

程序员文章站 2023-08-18 14:15:45
springboot作为现在十分流行的框架,简化Spring应用的初始搭建以及开发过程,现在我们就使用springboot来进行简单的web项目搭建并对项目sql进行监控。 项目的搭建就省略了,springboot项目搭建好以后,进行一下操作, 本例子的项目使用 maven 管理的jar 1.加入依 ......

springboot作为现在十分流行的框架,简化spring应用的初始搭建以及开发过程,现在我们就使用springboot来进行简单的web项目搭建并对项目sql进行监控。

项目的搭建就省略了,springboot项目搭建好以后,进行一下操作, 本例子的项目使用 maven 管理的jar

1.加入依赖, 在pom.xml文件 增加配置

<dependency>
<groupid>com.alibaba</groupid>
<artifactid>druid</artifactid>
<version>1.1.8</version>
</dependency>

2、
配置数据源 ,  在appcation.yml文件加入druid的数据源配置 
# 数据库访问配置
# 主数据源,默认的
spring.datasource.type=com.alibaba.druid.pool.druiddatasource
spring.datasource.driver-class-name=com.mysql.jdbc.driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=admin

# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialsize=5
spring.datasource.minidle=5
spring.datasource.maxactive=20
# 配置获取连接等待超时的时间
spring.datasource.maxwait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timebetweenevictionrunsmillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minevictableidletimemillis=300000
spring.datasource.validationquery=select 1 from dual
spring.datasource.testwhileidle=true
spring.datasource.testonborrow=false
spring.datasource.testonreturn=false
# 打开pscache,并且指定每个连接上pscache的大小
spring.datasource.poolpreparedstatements=true
spring.datasource.maxpoolpreparedstatementperconnectionsize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectproperties属性来打开mergesql功能;慢sql记录
spring.datasource.connectionproperties=druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000
# 合并多个druiddatasource的监控数据
#spring.datasource.useglobaldatasourcestat=true
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.mysql5dialect
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.improvednamingstrategy

3.使用注解的方式,增加druid的过滤器 ,新增一个类 druidstatfilter.java
import com.alibaba.druid.support.http.webstatfilter;
import javax.servlet.annotation.webfilter;
import javax.servlet.annotation.webinitparam;


@webfilter(filtername = "druidwebstatfilter", urlpatterns = "/*",
initparams = {
@webinitparam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
})
public class druidstatfilter extends webstatfilter {

}

4.使用注解的方式 增加 druidstatviewservlet.java 服务类
import com.alibaba.druid.support.http.statviewservlet;
import javax.servlet.annotation.webinitparam;
import javax.servlet.annotation.webservlet;

@suppresswarnings("serial")
@webservlet(urlpatterns = "/druid/*", initparams = {
@webinitparam(name = "allow", value = ""), // ip白名单
@webinitparam(name = "deny", value = ""),
// ip黑名单
@webinitparam(name = "loginusername", value = "admin"), // 用户名
@webinitparam(name = "loginpassword", value = "admin*druid"), // 密码
@webinitparam(name = "resetenable", value = "true")})
public class druidstatviewservlet extends statviewservlet {

}

5.这里有个很重要的事情一定不要忘了在启动类中加上servlet的扫描注解
@servletcomponentscan(value = "自己的包")
6.按理说现在druid就搭建好了可以通过http://localhost:8080/druid/index.html进行正常访问了,但是在操作中我发现sql监控并没有起到作用,也就是并没有sql监控的记录,在多次查阅资料后,
终于找到解决办法,虽然我们在配置文件application.properties中已经配置了druid数据源,但是在这里我们需要再次将这个datasource配置到java配置中,这里我们将这个配置直接写入到启动类中。
@bean
@configurationproperties(prefix="spring.datasource")
public datasource druiddatasource() {
   return new druiddatasource();
}