当前位置 博文首页 > blackball1998的博客:整合Druid连接池

    blackball1998的博客:整合Druid连接池

    作者:[db:作者] 时间:2021-06-09 09:15

    整合Druid连接池

    Spring Boot默认使用Hikari作为数据库连接池,这是一款优秀的高性能连接池

    在这里插入图片描述

    但是由于Druid连接池在监控和防火墙方面的突出优势,在开发中往往使用Druid连接池

    导入Druid连接池

    使用Druid连接池,首先需要导入Druid官方的启动器

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.2.6</version>
    </dependency>
    

    配置数据源还是按照之前的方式

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/test_db?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
        username: root
        password: 12345
        driver-class-name: com.mysql.cj.jdbc.Driver
    

    或者在中配置

    spring:
      datasource:
        druid:
          url: jdbc:mysql://localhost:3306/test_db?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
          username: root
          password: 12345
          driver-class-name: com.mysql.cj.jdbc.Driver
    

    启动项目,可以看到使用的连接池已经替换成Druid连接池

    在这里插入图片描述

    配置监控页面

    需要打开Druid连接池的监控功能,需要做以下的配置

    spring:
      datasource:
        druid:
          stat-view-servlet:
            enabled: true #配置监控页功能
            login-username: admin #配置登录账号
            login-password: admin #配置登录密码
            resetEnable: false #配置重置按钮失效
    

    然后使用项目下的/druid/login.html路径即可打开监控页面

    在这里插入图片描述

    使用配置的账号密码登录

    在这里插入图片描述

    配置Sql监控

    需要打开Druid连接池的Sql监控功能,需要做以下的配置

    spring:
      datasource:
        druid:
          filters: stat #底层开启功能,stat(sql监控),wall(防火墙)
          filter:
            stat:
              enabled: true #开启Sql监控
              slow-sql-millis: 1000 #慢Sql时间
              logSlowSql: true #打印慢Sql
    

    配置防火墙

    需要打开Druid连接池的防火墙功能,需要做以下的配置

    spring:
      datasource:
        druid:
          filters: wall #底层开启功能,stat(sql监控),wall(防火墙)
          filter:
            wall:
              enabled: true #开启防火墙
              config:
                drop-table-allow: false #禁止删除表
    

    配置监控SpringBean

    需要打开Druid连接池的监控SpringBean功能,需要做以下的配置

    spring:
      datasource:
        druid:
          aop-patterns: com.blackball.admin.* #监控的SpringBean
    

    配置监控web

    需要打开Druid连接池的监控web功能,需要做以下的配置

    spring:
      datasource:
        druid:
          web-stat-filter:
            enabled: true #开启监控web
            urlPattern: /* #监控的uri
            exclusions: '/index' #过滤的uri
    

    配置日志打印

    需要打开Druid连接池的slfj日志打印功能,需要做以下的配置

    spring:
      datasource:
        druid:
          filters: slfj
    
    下一篇:没有了