9. SpringBoot-druid
9.1 druid簡介
Java程序很大一部分要操作數據庫,為了提高性能操作數據庫,又不得不使用數據庫連接池。
Druid 是阿里巴巴開源平台上一個數據庫連接池實現,結合了 C3P0、DBCP 等 DB 池的優點,同時加入了日志監控。
Druid 可以很好的監控 DB 池連接和 SQL 的執行情況,天生就是針對監控而生的 DB 連接池。
Druid 已經在阿里巴巴部署了超過600個應用,經過一年多生產環境大規模部署的嚴苛考驗。
Spring Boot 2.0 以上默認使用 Hikari 數據源,可以說 Hikari 與 Driud 都是當前 Java Web 上最優秀的數據源,我們來重點看看Spring Boot 如何集成 Druid 數據源,如何實現數據庫監控。
Github地址:https://github.com/alibaba/druid/
com.alibaba.druid.pool.DruidDataSource 基本配置參數如下:
9.2 配置數據源
1、添加Druid數據源依賴。
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
2、切換數據源
Spring Boot 2.0 以上默認使用 com.zaxxer.hikari.HikariDataSource 數據源,但可以通過 spring.datasource.type 指定數據源。
spring:
datasource:
username: root
password: aadzj
#如果報錯是時區問題 加上 serverTimezone=UTC 就OK
url: jdbc:mysql://localhost:3306/userdb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource #把默認數據源切換成druid
3、輸出查看數據源
在測試類中注入 DataSource,然后獲取到它,輸出一看便知是否成功切換
4、druid數據源專有配置
切換成功,我們可以設置數據源連接初始化大小、最大連接數、等待時間、最小連接數 等設置項,可以查看源碼
spring:
datasource:
username: root
password: aadzj
#如果報錯是時區問題 加上 serverTimezone=UTC 就OK
url: jdbc:mysql://localhost:3306/userdb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#druid數據源專有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置監控統計攔截的filters,stat:監控統計、log4j:日志記錄、wall:防御sql注入
#如果允許報錯,java.lang.ClassNotFoundException: org.apache.Log4j.Properity
#則導入log4j 依賴就行
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionoProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
5、導入Log4j 的依賴
<!-- log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
6、DruidDataSource 綁定全局配置文件中的參數
現在需要為DruidDataSource 綁定全局配置文件中的參數,添加到容器中,而不再使用 Spring Boot 的自動生成了
package com.dzj.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
// @Configuration標注為配置類 相當於xml的<bean></bean>標簽
@Configuration
public class DruidConfig {
// @ConfigurationProperties用於把application.yaml中配置好的信息(數據源)關聯起來
// @Bean DruidDataSource注冊為bean,丟到spring容器中去管理,
//前綴為 spring.datasource的屬性值注入到 com.alibaba.druid.pool.DruidDataSource 的同名參數中
@ConfigurationProperties(prefix="spring.datasource")
@Bean
public DataSource druidDataSource(){
return new DruidDataSource();
}
}
7、測試類中測試查看是否成功!
8、配置Druid數據源監控
Druid 數據源具有監控的功能,並提供了一個 web 界面方便用戶查看,類似安裝路由器時,人家也提供了一個默認的 web 頁面。
第一步需要設置 Druid 的后台管理頁面,比如 登錄賬號、密碼 等,配置后台管理
package com.dzj.config;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
// @Configuration標注為配置類 相當於xml的<bean></bean>標簽
@Configuration
public class DruidConfig {
//后台監控 : web.xml,ServletRegistrationBean
//因為springboot內置了servlet容器,所以沒有web.xml,替代方法:ServletRegistrationBean
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
// 這些參數可以在 com.alibaba.druid.support.http.StatViewServlet 的父類 com.alibaba.druid.support.http.ResourceServlet 中找到
//后台需要有人登陸,賬號密碼配置
HashMap<String,String> initParameters = new HashMap<>();
//添加配置
initParameters.put("loginUsername","admin"); //登陸的key是固定的(loginUsername和loginPassword)
initParameters.put("loginPassword","aadzj");
// 允許誰可以訪問
initParameters.put("allow",""); // value值為空代表所有人都可以訪問
//禁止誰訪問
//initParameters.put("dzj","192.168.1.102");
bean.setInitParameters(initParameters); //設置初始化參數
return bean;
}
}
配置完畢后,我們可以選擇訪問 :http://localhost:8080/druid/login.html
輸入設置的賬號密碼登錄進入
9、配置 Druid web 監控 filter 過濾器
package com.dzj.config;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
// @Configuration標注為配置類 相當於xml的<bean></bean>標簽
@Configuration
public class DruidConfig {
//配置 Druid 監控 之 web 監控的 filter
//WebStatFilter:用於配置Web和Druid數據源之間的管理關聯監控統計
//filter,設置過濾器
@Bean
public FilterRegistrationBean webstatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
//可以過濾哪些請求呢?
Map<String,String> initParameters = new HashMap<>();
initParameters.put("exclusions","*.js,*.css,/druid/*"); //這些東西不進行統計
bean.setInitParameters(initParameters);
return bean;
}
}
平時在工作中,按需求進行配置即可,主要用作監控!