Druid連接池是阿里巴巴開源的數據庫連接池項目,后來貢獻給Apache開源;
Druid的作用是負責分配、管理和釋放數據庫連接,它允許應用程序重復使用一個現有的數據庫連接,而不是再重新建立一個;
Druid連接池內置強大的監控功能,其中的StatFilter功能,能采集非常完備的連接池執行信息,方便進行監控,而監控特性不影響性能。
Druid連接池內置了一個監控頁面,提供了非常完備的監控信息,可以快速診斷系統的瓶頸。
SpringBoot 1.x版本默認使用的的tomcat的jdbc連接池,由於jdbc性能,穩定性,監控能力都不不太好,所以SpringBoot 2.x版本后 默認連接池已經替換成了HikariCP,HikariCP性能強、速度快、口碑好、代碼少和穩定,暫時不推薦替換成成其他連接池。
這里記錄springboot項目整合druid數據庫連接池中間件:
資源准備及版本說明
編程工具:IDEA
JDK版本:1.8
Maven版本:Apache Maven 3.6.3
springboot版本:2.4.4
mybatis版本:1.3.2
mysql版本:5.1.48
druid版本:1.1.21
創建mavem項目
通過IDEA創建很便捷,參考《IDEA創建SpringBoot的maven項目》,springboot項目整合mybatis參考《springboot項目整合mybatis》。
配置pom.xml
druid的pom依賴有兩個版本,一個需要編寫配置文件,一個是自動配置的,這里選擇自動配置版本
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
完整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.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>springboot-druid</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<mybatis.version>1.3.2</mybatis.version>
<mysql.version>5.1.48</mysql.version>
<druid.version>1.1.9</druid.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid</artifactId>-->
<!-- <version>${druid.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置application.yml
application.yml配置文件中需要配置druid的相關信息
配置說明如下:

完整application.yml配置如下:
server:
port: 8888
spring:
application:
name: springboot-druid
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/spring-boot-test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource # 數據庫連接池類別
druid:
initial-size: 5 # 初始化大小
min-idle: 10 # 最小連接數
max-active: 20 # 最大連接數
max-wait: 60000 # 獲取連接時的最大等待時間
min-evictable-idle-time-millis: 300000 # 一個連接在池中最小生存的時間,單位是毫秒
time-between-eviction-runs-millis: 60000 # 多久才進行一次檢測需要關閉的空閑連接,單位是毫秒
filters: stat,wall # 配置擴展插件:stat-監控統計,log4j-日志,wall-防火牆(防止SQL注入),去掉后,監控界面的sql無法統計
validation-query: SELECT 1 # 檢測連接是否有效的 SQL語句,為空時以下三個配置均無效
test-on-borrow: true # 申請連接時執行validationQuery檢測連接是否有效,默認true,開啟后會降低性能
test-on-return: true # 歸還連接時執行validationQuery檢測連接是否有效,默認false,開啟后會降低性能
test-while-idle: true # 申請連接時如果空閑時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效,默認false,建議開啟,不影響性能
stat-view-servlet:
enabled: true # 是否開啟 StatViewServlet
allow: 127.0.0.1 # 訪問監控頁面 白名單,默認127.0.0.1
deny: 192.168.56.1 # 訪問監控頁面 黑名單
login-username: admin # 訪問監控頁面 登陸賬號
login-password: admin # 訪問監控頁面 登陸密碼
filter:
stat:
enabled: true # 是否開啟 FilterStat,默認true
log-slow-sql: true # 是否開啟 慢SQL 記錄,默認false
slow-sql-millis: 5000 # 慢 SQL 的標准,默認 3000,單位:毫秒
merge-sql: false # 合並多個連接池的監控數據,默認false
# mybatis配置
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.dandelion.model
# 輸出sql語句日志
logging:
level:
com:
springboot:
dao: debug
訪問druid監控中心
啟動項目:

在瀏覽器中輸入http://IP:端口號/druid/index.html訪問監控中心
如果有配置登錄賬號密碼,則需要進行登錄:


定義測試接口查詢數據庫:

監控中心記錄訪問情況

