Loading

springboot整合druid踩坑记录

druid中整合日志系统

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>2.13.3</version>
</dependency>

以上是IDEA提示的log4j,但是这其实是log4j2的包,但是在配置druid时,此时的配置文件如下:

#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错
#    java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:
#    https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

以上是部分配置,但是配置监控时采用的是 log4j这样就会产生错误,所以应该将两者进行统一,

  1. 如果配置文件中filters中配置的是log4j,那么导入的log4j依赖应该如下:

    <dependency>-->
        <groupId>log4j</groupId>-->
        <artifactId>log4j</artifactId>-->
        <version>1.2.17</version>-->
    </dependency>-->
    
  2. 如果配置文件中filters中配置的是log4j2,那么导入的log4j2依赖应该如下:

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>2.13.3</version>
    </dependency>
    

springboot中配置文件整合druid,实现监控访问

在项目中使用阿里的druid连接池,pom文件配置:

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid-spring-boot-starter</artifactId>
   <version>1.1.14</version>
</dependency>
12345678910

application.properties配置,官方文档可查:

# Druid连接池配置,官方配置参考:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# Druid其他配置
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=10
spring.datasource.druid.max-wait=10
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000

# Druid WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/*

# Druid StatViewServlet配置,说明请参考Druid Wiki,配置_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=true
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=
spring.datasource.druid.stat-view-servlet.deny=
12345678910111213141516171819202122232425262728

OK,搞定~

接下来,启动项目,打开地址:http://localhost:8080/druid
页面长这样:
druid监控页面
我来请求一下接口,查询sql,确实监控到了:
sql监控
以上:原文链接:https://blog.csdn.net/xqnode/article/details/88377296

posted @ 2020-10-30 11:48  luwanglin  阅读(3152)  评论(0编辑  收藏  举报