springboot項目整合druid數(shù)據(jù)庫連接池
作者 | 蒲公英不是夢
來源 | urlify.cn/ARFnqq
Druid連接池是阿里巴巴開源的數(shù)據(jù)庫連接池項目,后來貢獻(xiàn)給Apache開源;
Druid的作用是負(fù)責(zé)分配、管理和釋放數(shù)據(jù)庫連接,它允許應(yīng)用程序重復(fù)使用一個現(xiàn)有的數(shù)據(jù)庫連接,而不是再重新建立一個;
Druid連接池內(nèi)置強(qiáng)大的監(jiān)控功能,其中的StatFilter功能,能采集非常完備的連接池執(zhí)行信息,方便進(jìn)行監(jiān)控,而監(jiān)控特性不影響性能。
Druid連接池內(nèi)置了一個監(jiān)控頁面,提供了非常完備的監(jiān)控信息,可以快速診斷系統(tǒng)的瓶頸。
SpringBoot 1.x版本默認(rèn)使用的的tomcat的jdbc連接池,由于jdbc性能,穩(wěn)定性,監(jiān)控能力都不不太好,所以SpringBoot 2.x版本后 默認(rèn)連接池已經(jīng)替換成了HikariCP,HikariCP性能強(qiáng)、速度快、口碑好、代碼少和穩(wěn)定,暫時不推薦替換成成其他連接池。
這里記錄springboot項目整合druid數(shù)據(jù)庫連接池中間件:
資源準(zhǔn)備及版本說明
編程工具: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
創(chuàng)建mavem項目
通過IDEA創(chuàng)建很便捷,參考《IDEA創(chuàng)建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的相關(guān)信息
配置說明如下:
完整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 # 數(shù)據(jù)庫連接池類別
druid:
initial-size: 5 # 初始化大小
min-idle: 10 # 最小連接數(shù)
max-active: 20 # 最大連接數(shù)
max-wait: 60000 # 獲取連接時的最大等待時間
min-evictable-idle-time-millis: 300000 # 一個連接在池中最小生存的時間,單位是毫秒
time-between-eviction-runs-millis: 60000 # 多久才進(jìn)行一次檢測需要關(guān)閉的空閑連接,單位是毫秒
filters: stat,wall # 配置擴(kuò)展插件:stat-監(jiān)控統(tǒng)計,log4j-日志,wall-防火墻(防止SQL注入),去掉后,監(jiān)控界面的sql無法統(tǒng)計
validation-query: SELECT 1 # 檢測連接是否有效的 SQL語句,為空時以下三個配置均無效
test-on-borrow: true # 申請連接時執(zhí)行validationQuery檢測連接是否有效,默認(rèn)true,開啟后會降低性能
test-on-return: true # 歸還連接時執(zhí)行validationQuery檢測連接是否有效,默認(rèn)false,開啟后會降低性能
test-while-idle: true # 申請連接時如果空閑時間大于timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測連接是否有效,默認(rèn)false,建議開啟,不影響性能
stat-view-servlet:
enabled: true # 是否開啟 StatViewServlet
allow: 127.0.0.1 # 訪問監(jiān)控頁面 白名單,默認(rèn)127.0.0.1
deny: 192.168.56.1 # 訪問監(jiān)控頁面 黑名單
login-username: admin # 訪問監(jiān)控頁面 登陸賬號
login-password: admin # 訪問監(jiān)控頁面 登陸密碼
filter:
stat:
enabled: true # 是否開啟 FilterStat,默認(rèn)true
log-slow-sql: true # 是否開啟 慢SQL 記錄,默認(rèn)false
slow-sql-millis: 5000 # 慢 SQL 的標(biāo)準(zhǔn),默認(rèn) 3000,單位:毫秒
merge-sql: false # 合并多個連接池的監(jiān)控數(shù)據(jù),默認(rèn)false
# mybatis配置
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.dandelion.model
# 輸出sql語句日志
logging:
level:
com:
springboot:
dao: debug
訪問druid監(jiān)控中心
啟動項目:
在瀏覽器中輸入http://IP:端口號/druid/index.html訪問監(jiān)控中心
如果有配置登錄賬號密碼,則需要進(jìn)行登錄:

定義測試接口查詢數(shù)據(jù)庫:
監(jiān)控中心記錄訪問情況

最近給大家找了 JVM學(xué)習(xí)視頻
資源,怎么領(lǐng)取?
掃二維碼,加我微信,回復(fù):JVM
注意,不要亂回復(fù) 沒錯,不是機(jī)器人 記得一定要等待,等待才有好東西
