如何在Spring Boot中集成Druid连接池和监控?

使用yaml的配置方式这里

druid官方github地址

https://github.com/alibaba/druid

分析自动配置

  • 扩展配置项 spring.datasource.druid
  • DruidSpringAopConfiguration.class, 监控SpringBean的;配置项:spring.datasource.druid.aop-patterns
  • DruidStatViewServletConfiguration.class, 监控页的配置:spring.datasource.druid.stat-view-servlet;默认开启
  • DruidWebStatFilterConfiguration.class, web监控配置;spring.datasource.druid.web-stat-filter;默认开启
  • DruidFilterConfiguration.class}) 所有Druid自己filter的配置

导入依赖

<!--       druid的starter自动配置(包含了druid的jar)-->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid-spring-boot-starter</artifactId>
         <version>1.1.17</version>
      </dependency>
      <!--   log4j-->
      <!-- https://mvnrepository.com/artifact/log4j/log4j -->
      <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <version>1.2.17</version>
      </dependency>

写配置

#配置数据源
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
#    druid配置
    druid:
      aop-patterns: com.example.* #    spring监控
      filters: stat,wall,log4j  # 底层开启功能,stat(sql监控),wall(防火墙)
      #  配置监控功能
      stat-view-servlet:
        enabled: true #开启监控
        login-username: admin #账号
        login-password: 123456 #密码
      #     #配置web监控
      web-stat-filter:
        enabled: true #开启
        url-pattern: /* #所有路径
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' #不监控路径
#        对开启的参数修改
      filter:
        stat:    # 对上面filters里面的stat的详细配置
          slow-sql-millis: 1000 #响应吵一秒为慢加载
          logSlowSql: true
          enabled: true #开启sql监控
        wall:
          enabled: true #开启防火墙
          config:
            drop-table-allow: false #不允许修改表

 

即可

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水果不是橙子

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值