Hello你們好,本章咱們將默認數據庫鏈接池換成Druid鏈接池。有問題能夠聯繫我mr_beany@163.com。另求各路大神指點,感謝
打開上篇文章→從頭開始搭建本身的springboot後臺框架(一)中提到的pom.xmlcss
找到<dependencies></dependencies>
標籤,在標籤中添加Druid依賴java
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>複製代碼
剛添加時 <version>1.0.29</version> 顯示紅色git
而後鼠標右鍵選擇Maven→Reimport進行依賴下載github
下載成功後 <version>1.0.29</version>
顯示黑色web
在application.properties文件中添加以下配置spring
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 sql
內容以下:數據庫
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
springboot
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。另求各路大神指點,感謝你們。