Hello大家好,本章我們將預設資料庫連線池換成Druid連線池。有問題可以聯絡我mr_beany@163.com。另求各路大神指點,感謝
一:引入Druid依賴
開啟上篇文章→從頭開始搭建自己的springboot後臺框架(一)中提到的pom.xml
找到<dependencies></dependencies>
標籤,在標籤中新增Druid依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>複製程式碼
剛新增時 <version>1.0.29</version> 顯示紅色
然後滑鼠右鍵選擇Maven→Reimport進行依賴下載
下載成功後 <version>1.0.29</version>
顯示黑色
二:配置資料來源連線池資訊
在application.properties檔案中新增如下配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#連線池配置
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
#連線等待超時時間
spring.datasource.maxWait=60000
#配置隔多久進行一次檢測(檢測可以關閉的空閒連線)
spring.datasource.timeBetweenEvictionRunsMillis=60000
#配置連線在池中的最小生存時間
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 開啟PSCache,並且指定每個連線上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置監控統計攔截的filters,去掉後監控介面sql無法統計,`wall`用於防火牆
spring.datasource.filters=stat,wall,slf4j
# 通過connectProperties屬性來開啟mergeSql功能;慢SQL記錄
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000複製程式碼
三:新增配置檔案
在core→configurer下建立DruidDataSourceConfigurer.java 和DruidMonitorConfigurer.java
內容如下:
DruidDataSourceConfigurer
package com.example.demo.core.configurer;
import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* @author 張瑤
* @Description:
* @time 2018/4/18 15:16
*/
@Configuration
public class DruidDataSourceConfigurer {
private Logger logger = LoggerFactory.getLogger(DruidDataSourceConfigurer.class);
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
private String filters;
@Value("{spring.datasource.connectionProperties}")
private String connectionProperties;
@Bean
public DataSource getDataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
//configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}
複製程式碼
DruidMonitorConfigurer
package com.example.demo.core.configurer;
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.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
* @author 張瑤
* @Description:Druid監控配置
* @time 2018/4/18 15:30
*/
@Configuration
public class DruidMonitorConfigurer {
/**
* 註冊ServletRegistrationBean
* @return
*/
@Bean
public ServletRegistrationBean registrationBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
/** 初始化引數配置,initParams**/
//白名單
bean.addInitParameter("allow", "127.0.0.1");//多個ip逗號隔開
//IP黑名單 (存在共同時,deny優先於allow) : 如果滿足deny的話提示:Sorry, you are not permitted to view this page.
//bean.addInitParameter("deny", "192.168.1.110");
//登入檢視資訊的賬號密碼.
bean.addInitParameter("loginUsername", "admin");
bean.addInitParameter("loginPassword", "123456");
//是否能夠重置資料.
bean.addInitParameter("resetEnable", "false");
return bean;
}
/**
* 註冊FilterRegistrationBean
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
//新增過濾規則.
bean.addUrlPatterns("/*");
//新增不需要忽略的格式資訊.
bean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return bean;
}
}複製程式碼
四:啟動伺服器,訪問資料連線池管理頁面
開啟瀏覽器,輸入http://localhost:8080/druid 出現以下頁面,為配置成功
賬號為admin,密碼為123456
可以檢視資料來源,SQL監控,URL監控等資訊
專案地址
碼雲地址: gitee.com/beany/mySpr…
GitHub地址: github.com/MyBeany/myS…
寫文章不易,如對您有幫助,請幫忙點下star
結尾
springboot整合Druid已完成,後續功能接下來陸續更新,有問題可以聯絡我mr_beany@163.com。另求各路大神指點,感謝大家。