Springboot中如何集成Druid
Springboot 集成Druid
Druid是Java語(yǔ)言中最好的數(shù)據(jù)庫(kù)連接池。Druid能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能。本篇主要講解一下 Springboot中如何集成 Druid !

1.添加Druid依賴(lài)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>有不少教程建議的依賴(lài)是
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>其實(shí)Druid和Springboot官方已經(jīng)提供了很好的集成,雖然二者都可以使用,但是druid-spring-boot-starter不需要編寫(xiě)配置類(lèi),簡(jiǎn)化了配置。
2.添加配置application.yaml
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
type: com.alibaba.druid.pool.DruidDataSource
# 數(shù)據(jù)源連接池配置
druid:
# 數(shù)據(jù)源其他配置
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
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.logSlowSql=true;druid.stat.slowSqlMillis=1000;
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻
filters: stat,wall,slf4j
#配置監(jiān)控屬性: 在druid-starter的: com.alibaba.druid.spring.boot.autoconfigure.stat包下進(jìn)行的邏輯配置
# WebStatFilter配置,
web-stat-filter:
#默認(rèn)為false,表示不使用WebStatFilter配置,就是屬性名去短線(xiàn)
enabled: true
#攔截該項(xiàng)目下的一切請(qǐng)求
url-pattern: /*
#對(duì)這些請(qǐng)求放行
exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
session-stat-enable: true
principal-session-name: session_name
principal-cookie-name: cookie_name
# StatViewServlet配置
stat-view-servlet:
#默認(rèn)為false,表示不使用StatViewServlet配置,就是屬性名去短線(xiàn)
enabled: true
#配置DruidStatViewServlet的訪(fǎng)問(wèn)地址。后臺(tái)監(jiān)控頁(yè)面的訪(fǎng)問(wèn)地址
url-pattern: /druid/*
#禁用HTML頁(yè)面上的“重置”功能,會(huì)把所有監(jiān)控的數(shù)據(jù)全部清空,一般不使用
reset-enable: false
#監(jiān)控頁(yè)面登錄的用戶(hù)名
login-username: admin
#監(jiān)控頁(yè)面登錄的密碼
login-password: 123456
#白名單
allow:
#黑名單
deny:
#Spring監(jiān)控配置,說(shuō)明請(qǐng)參考Druid Github Wiki,配置_Druid和Spring關(guān)聯(lián)監(jiān)控配置
aop-patterns: com.zzp.*有幾個(gè)地方需要注意下:
- 想要在日志中輸出慢日志除了配置connectionProperties,還需要在filters屬性中加入項(xiàng)目使用的日志框架如log4j或者slf4j,并引入對(duì)應(yīng)依賴(lài),否則日志中不會(huì)輸出慢sql日志;
- 如果需要Spring監(jiān)控,除了配置aop-patterns,還需要加入spring-boot-starter-aop依賴(lài),否則Spring監(jiān)控頁(yè)面空白。
3.查看監(jiān)控頁(yè)面
訪(fǎng)問(wèn)http://ip:port/druid/login.html,輸入賬號(hào)密碼,如果數(shù)據(jù)源頁(yè)面各項(xiàng)參數(shù)和配置的參數(shù)一樣,則說(shuō)明集成成功。

調(diào)用接口,查看SQL監(jiān)控和Spring監(jiān)控頁(yè)面是否正常顯示。
4.去除廣告
監(jiān)控頁(yè)面下方有阿里云的廣告,可以通過(guò)代碼去除。
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.util.Utils;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import java.io.IOException;
/**
* Druid廣告配置
*
* @author zzp
*/
@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(
name = "spring.datasource.druid.stat-view-servlet.enabled",
havingValue = "true",
matchIfMissing = true)
public class DruidAdConfig {
/**
* 去除監(jiān)控頁(yè)面底部廣告
*
* @param properties
* @return org.springframework.boot.web.servlet.FilterRegistrationBean
*/
@Bean
public FilterRegistrationBean removeDruidAdFilterRegistrationBean(
DruidStatProperties properties) {
// 獲取web監(jiān)控頁(yè)面的參數(shù)
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
// 提取common.js的配置路徑
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
final String filePath = "support/http/resources/js/common.js";
// 創(chuàng)建filter進(jìn)行過(guò)濾
Filter filter =
new Filter() {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(
ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
// 重置緩沖區(qū),響應(yīng)頭不會(huì)被重置
response.resetBuffer();
// 獲取common.js
String text = Utils.readFromResource(filePath);
// 正則替換banner, 除去底部的廣告信息
text = text.replaceAll("<a.*?banner\"></a><br/>", "");
text = text.replaceAll("powered.*?shrek.wang</a>", "");
response.getWriter().write(text);
}
@Override
public void destroy() {}
};
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns(commonJsPattern);
return registrationBean;
}
}完結(jié)!
到此這篇關(guān)于Springboot 集成Druid的文章就介紹到這了,更多相關(guān)Springboot 集成Druid內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中http請(qǐng)求之restTemplate配置超時(shí)時(shí)間問(wèn)題解決
這篇文章主要介紹了java中http請(qǐng)求之restTemplate配置超時(shí)時(shí)間,本文給大家分享三種解決方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
springboot使用Thymeleaf報(bào)錯(cuò)常見(jiàn)的幾種解決方案
這篇文章主要介紹了springboot使用Thymeleaf報(bào)錯(cuò)常見(jiàn)的幾種解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Java純代碼實(shí)現(xiàn)導(dǎo)出pdf合并單元格
這篇文章主要為大家詳細(xì)介紹了Java如何純代碼實(shí)現(xiàn)導(dǎo)出pdf與合并單元格功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
Java使用easyExcel導(dǎo)出數(shù)據(jù)及單元格多張圖片
除了平時(shí)簡(jiǎn)單的數(shù)據(jù)導(dǎo)出需求外,我們也經(jīng)常會(huì)遇到一些有固定格式或者模板要求的數(shù)據(jù)導(dǎo)出,下面這篇文章主要給大家介紹了關(guān)于Java使用easyExcel導(dǎo)出數(shù)據(jù)及單元格多張圖片的相關(guān)資料,需要的朋友可以參考下2023-05-05
Java經(jīng)典設(shè)計(jì)模式之觀察者模式原理與用法詳解
這篇文章主要介紹了Java經(jīng)典設(shè)計(jì)模式之觀察者模式,簡(jiǎn)單分析了觀察者模式的概念、原理并結(jié)合實(shí)例形式給出了java觀察者模式的具體用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
基于批處理腳本bat打造一個(gè)高效Java版本切換工具
本文介紹了作者開(kāi)發(fā)的一款用于Windows系統(tǒng)多Java版本自動(dòng)化管理的批處理工具,該工具具備自動(dòng)掃描JDK目錄、智能識(shí)別版本號(hào)、權(quán)限管理、雙重設(shè)置、清理重復(fù)路徑、實(shí)時(shí)驗(yàn)證等功能,需要的朋友可以參考下2026-01-01
SpringBoot同一接口多個(gè)實(shí)現(xiàn)類(lèi)配置的實(shí)例詳解
這篇文章主要介紹了SpringBoot同一接口多個(gè)實(shí)現(xiàn)類(lèi)配置的實(shí)例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11

