springboot中加入druid对sql进行监控

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();
}
 


原文地址:https://www.cnblogs.com/ccxp/p/9497328.html

时间: 2024-08-29 22:32:51

springboot中加入druid对sql进行监控的相关文章

springboot中使用druid和监控配置

如果想要监控自己的项目的访问情况及查看配置信息,druid是一个很好的选择,可能你会问druid是什么?有什么用?优点是什么? Druid简介 Druid是阿里巴巴开源的数据库连接池,号称是Java语言中最好的数据库连接池,能够提供强大的监控和扩展功能.GitHub地址:https://github.com/alibaba/druid.Druid有以下优点:1) 可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库

Springboot项目配置druid数据库连接池,并监控统计功能

pom.xml配置依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency>  资源文件配置信息 不管是

springboot之整合druid并配置数据源监控

pom.xml <?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

springboot+druid连接池及监控配置

1. 问题描述 阿里巴巴的数据库连接池Druid在效率与稳定性都很高,被很多开发团队使用,并且自带的Druid监控也很好用,本章简单介绍下springboot+druid配置连接池及监控. 2. 解决方案 2.1 pom.xml springboot 已经有druid的starter,但是好像有点问题,不知道为什么没拿到jar包,有可能是网络问题,还是使用了原生的druid gav. <dependency> <groupId>com.alibaba</groupId>

Druid数据源SQL数据库与Spring监控

Druid监控概要说明 为什么要监控? Druid是什么?德鲁伊 URL监控配置说明 配置步骤 步骤 配置 第一步 web.xml 配置 WebStatFilter 第二步 WebStatFilter 配置拦截参数,Cookie或Session中的用户名 第三步 访问Servlet /druid 查看监控结果 配置web.xml WebStatFilter https://github.com/alibaba/druid/wiki/配置 URL监控效果 Session监控效果 SQL监控配置说明

Druid连接池及监控在spring中的配置

Druid连接池及监控在Spring配置如下: [html] view plaincopy <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url.user.password --> <property 

Java实战之路(1):SpringBoot项目中使用Mybatis打印Sql语句

SpringBoot项目中使用Mybatis打印Sql语句 如题,实际项目中使用很多都会用到SpringBoot+Mybatis的经典搭配进行开发,数据库里明明有数据,可是程序运行就是查不到,此时我们在本地Debug时,需要将Mybatis的实际Sql打印出来,看看Sql与我们期望的是否一致,或者将Sql拿到数据库中直接执行,看看结果.这里简单介绍几种实战中的用法. 方法一 properties:在application.properties配置文件中增加如下配置 logging.level.c

springboot中的那些连接池

hello~各位读者新年好! 回想起前几天在部署springboot项目到正线时,线上环境要求jdk7,可项目是基于jdk8开发的,springboot也是用的springboot2以上的版本,可以说缝缝补补一整天才搞好能满足线上环境的代码,搞完后当然需要小小的了解一下背后的秘密. 好了,话不多说,我们直接进入正题. 其实切换还不算太麻烦,坑就坑在SpringBoot2切换到SpringBoot1后,默认使用的连接池发生了变化,之前做的压力测试又重新搞了一遍. 怨天尤人貌似消极了哈,小编我可是一

springboot整合阿里druid数据源

全配置在application配置文件(方式1) maven依赖 <!--druid的启动器--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.17</version> </dependency> <!--这里不加jdbc依