SpringBoot配置 Druid 三種方式(包括純配置文件配置)
記錄一下在項(xiàng)目中用純 YML(application.yml 或者 application.properties)文件、Java 代碼配置 Bean 和注解三種方式配置 Alibaba Druid 用于監(jiān)控或者查看 SQL 狀況:
1. 純配置文件 .yml 或者 .properties
(1)pom.xml 添加相關(guān)依賴
<!-- SPRINGBOOT WEB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SPRINGBOOT DRUID -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- SPRINGBOOT JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>${mysql-connector-java.version}</scope>
</dependency>
<!-- LOG4J -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
(2)配置application.yml 或者 application.properties 文件,本次配置文件為application.yml
spring:
# 數(shù)據(jù)源配置
datasource:
type: com.alibaba.druid.pool.DruidDataSource # MYSQL 5 驅(qū)動(dòng):com.mysql.jdbc.Driver,MYSQL 6+ 驅(qū)動(dòng):com.mysql.cj.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
# 連接池配置
druid:
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
# 配置獲取連接等待超時(shí)的時(shí)間
max-wait: 60000
# 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位毫秒
time-between-eviction-runs-millis: 60000
# 配置一個(gè)連接在池中最小生存時(shí)間
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM sys_user
test-while-idle: true
test-on-borrow: false
test-on-return: false
# 打開 PSCache,并且指定每個(gè)連接上 PSCache 的大小
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置監(jiān)控統(tǒng)計(jì)攔截的 Filter,去掉后監(jiān)控界面 SQL 無法統(tǒng)計(jì),wall 用于防火墻
filters: stat,wall,log4j
# 通過 connection-properties 屬性打開 mergeSql 功能;慢 SQL 記錄
connection-properties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
# 配置 DruidStatFilter
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: .js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
# 配置 DruidStatViewServlet
stat-view-servlet:
url-pattern: /druid/*
# IP 白名單,沒有配置或者為空,則允許所有訪問
allow: 127.0.0.1
# IP 黑名單,若白名單也存在,則優(yōu)先使用
deny: 192.168.31.253
# 禁用 HTML 中 Reset All 按鈕
reset-enable: false
# 登錄用戶名/密碼
login-username: root
login-password: 123
(3)啟動(dòng) SpringBoot 項(xiàng)目訪問地址http://localhost:8080/druid/login.html進(jìn)行登錄,若出現(xiàn)一下界面則配置成功

2.Java 代碼配置 Bean
(1)pom.xml 依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
(2)在配置文件 application.yml 中添加 Druid 配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
# 數(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
# 配置監(jiān)控統(tǒng)計(jì)攔截的 filters,去掉后監(jiān)控界面 sql 無法統(tǒng)計(jì),'wall'用于防火墻
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
(3)添加 DruidConfig.java 配置文件
import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
@Configuration
public class DruidConfig {
@Bean
// 將所有前綴為spring.datasource下的配置項(xiàng)都加載DataSource中
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
@Bean
public ServletRegistrationBean<Servlet> druidServlet() {
// 進(jìn)行 druid 監(jiān)控的配置處理
ServletRegistrationBean<Servlet> srb = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/**");
// 白名單
srb.addInitParameter("allow", "127.0.0.1");
// 黑名單
srb.addInitParameter("deny", "192.168.31.253");
// 用戶名
srb.addInitParameter("loginUsername", "root");
// 密碼
srb.addInitParameter("loginPassword", "root");
// 是否可以重置數(shù)據(jù)源
srb.addInitParameter("resetEnable", "false");
return srb;
}
@Bean
public FilterRegistrationBean<Filter> filterRegistrationBean() {
FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
frb.setFilter(new WebStatFilter());
// 所有請(qǐng)求進(jìn)行監(jiān)控處理
frb.addUrlPatterns("/*");
// 排除名單
frb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");
return frb;
}
}
(4)啟動(dòng) SpringBoot 項(xiàng)目訪問地址http://localhost:8080/druid/login.html進(jìn)行查看登錄
3.注解
(1)pom.xml 文件中添加 Druid 依賴(同方式二)
(2)配置文件添加信息(同方式二)
(3)配置 WebServlet
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import com.alibaba.druid.support.http.StatViewServlet;
@WebServlet(urlPatterns = "/druid/*",
initParams={
@WebInitParam(name="allow",value="192.168.16.110,127.0.0.1"),// IP白名單 (沒有配置或者為空,則允許所有訪問)
@WebInitParam(name="deny",value="192.168.16.111"),// IP黑名單 (存在共同時(shí),deny優(yōu)先于allow)
@WebInitParam(name="loginUsername",value="admin"),// 用戶名
@WebInitParam(name="loginPassword",value="admin"),// 密碼
@WebInitParam(name="resetEnable",value="false")// 禁用HTML頁面上的“Reset All”功能
})
public class DruidServlet extends StatViewServlet {
private static final long serialVersionUID = 1L;
}
(4)配置 WebFilter
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略資源
})
public class DruidFilter extends WebStatFilter {
}
(5)配置啟動(dòng)文件SpringBootApplication 用于掃描我們配置的類
添加 @ServletComponentScan 注解進(jìn)行掃描
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan("com.uy.servlet")
public class SpringBootApplication{
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args); } }
(6)啟動(dòng) SpringBoot 項(xiàng)目訪問地址http://localhost:8080/druid/login.html進(jìn)行查看登錄
到此這篇關(guān)于SpringBoot配置 Druid 三種方式(包括純配置文件配置)的文章就介紹到這了,更多相關(guān)SpringBoot配置 Druid內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能
本文主要介紹了JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
詳解spring封裝hbase的代碼實(shí)現(xiàn)
本篇文章主要介紹了詳解spring封裝hbase的代碼實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
SpringBoot中的定時(shí)任務(wù)和異步調(diào)用詳解
這篇文章主要介紹了SpringBoot中的定時(shí)任務(wù)和異步調(diào)用詳解,SpringBoot 定時(shí)任務(wù)是一種在SpringBoot應(yīng)用中自動(dòng)執(zhí)行任務(wù)的機(jī)制,通過使用Spring框架提供的@Scheduled注解,我們可以輕松地創(chuàng)建定時(shí)任務(wù),需要的朋友可以參考下2023-10-10
sin(x)如何求解的java代碼實(shí)現(xiàn)方法
這篇文章主要為大家介紹了sin(x)如何求解的java代碼實(shí)現(xiàn)方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
解讀System.getProperty("ENM_HOME")中的值從哪獲取的
這篇文章主要介紹了解讀System.getProperty("ENM_HOME")中的值從哪獲取的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

