摘要:導讀最近一段時間比較忙,以至于很久沒有更新系列文章,恰好最近用到,就將使用作為數據源做一個簡單的介紹。效果圖如下參考資料官方地址使用集成數據庫連接池使用和監控配置方法
導讀
最近一段時間比較忙,以至于很久沒有更新Spring Boot系列文章,恰好最近用到Druid, 就將Spring Boot 使用 Druid作為數據源做一個簡單的介紹。
Druid介紹:Druid是阿里巴巴開源的數據庫連接池,Druid號稱是Java語言中最好的數據庫連接池,并且能夠提供強大的監控和擴展功能,Druid的官方地址
了解更多:
JDBC連接池、監控組件 Druid (oschina)
快速上手:下面來說明如何在 spring Boot 中配置使用Druid ,本例使用的持久層框為Spring Boot JPA。
第一步:添加pom依賴:第二步:配置yml文件 數據源相關信息org.springframework.boot spring-boot-starter-data-jpa com.alibaba druid 1.0.20
spring: datasource: url: jdbc:mysql://localhost:3306/sanji-boot?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver platform: mysql type: com.alibaba.druid.pool.DruidDataSource # 下面為連接池的補充設置,應用到上面所有數據源中 # 初始化大小,最小,最大 initialSize: 1 minIdle: 3 maxActive: 20 # 配置獲取連接等待超時的時間 maxWait: 60000 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一個連接在池中最小生存的時間,單位是毫秒 minEvictableIdleTimeMillis: 30000 validationQuery: select "x" testWhileIdle: true testOnBorrow: false testOnReturn: false # 打開PSCache,并且指定每個連接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置監控統計攔截的filters,去掉后監控界面sql無法統計,"wall"用于防火墻 filters: stat,wall,slf4j # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多個DruidDataSource的監控數據 #useGlobalDataSourceStat: true jpa: show-sql: true properties: hibernate: hbm2ddl: auto: create第三步:配置Druid的監控統計功能
這里有兩種方式 :
第一種方式@Confing注解 配置類:
@Configuration public class DruidConfiguration { private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class); private static final String DB_PREFIX = "spring.datasource"; @Bean public ServletRegistrationBean druidServlet() { logger.info("init Druid Servlet Configuration "); ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); // IP白名單 servletRegistrationBean.addInitParameter("allow", "192.168.2.25,127.0.0.1"); // IP黑名單(共同存在時,deny優先于allow) servletRegistrationBean.addInitParameter("deny", "192.168.1.100"); //控制臺管理用戶 servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "admin"); //是否能夠重置數據 禁用HTML頁面上的“Reset All”功能 servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } //解決 spring.datasource.filters=stat,wall,log4j 無法正常注冊進去 @ConfigurationProperties(prefix = DB_PREFIX) class IDataSourceProperties { private String url; private String username; private String password; private String driverClassName; private int initialSize; private int minIdle; private int maxActive; private int maxWait; private int timeBetweenEvictionRunsMillis; private int minEvictableIdleTimeMillis; private String validationQuery; private boolean testWhileIdle; private boolean testOnBorrow; private boolean testOnReturn; private boolean poolPreparedStatements; private int maxPoolPreparedStatementPerConnectionSize; private String filters; private String connectionProperties; @Bean //聲明其為Bean實例 @Primary //在同樣的DataSource中,首先使用被標注的DataSource public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(url); 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) { System.err.println("druid configuration initialization filter: " + e); } datasource.setConnectionProperties(connectionProperties); return datasource; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public int getInitialSize() { return initialSize; } public void setInitialSize(int initialSize) { this.initialSize = initialSize; } public int getMinIdle() { return minIdle; } public void setMinIdle(int minIdle) { this.minIdle = minIdle; } public int getMaxActive() { return maxActive; } public void setMaxActive(int maxActive) { this.maxActive = maxActive; } public int getMaxWait() { return maxWait; } public void setMaxWait(int maxWait) { this.maxWait = maxWait; } public int getTimeBetweenEvictionRunsMillis() { return timeBetweenEvictionRunsMillis; } public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) { this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; } public int getMinEvictableIdleTimeMillis() { return minEvictableIdleTimeMillis; } public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) { this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; } public String getValidationQuery() { return validationQuery; } public void setValidationQuery(String validationQuery) { this.validationQuery = validationQuery; } public boolean isTestWhileIdle() { return testWhileIdle; } public void setTestWhileIdle(boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; } public boolean isTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } public boolean isTestOnReturn() { return testOnReturn; } public void setTestOnReturn(boolean testOnReturn) { this.testOnReturn = testOnReturn; } public boolean isPoolPreparedStatements() { return poolPreparedStatements; } public void setPoolPreparedStatements(boolean poolPreparedStatements) { this.poolPreparedStatements = poolPreparedStatements; } public int getMaxPoolPreparedStatementPerConnectionSize() { return maxPoolPreparedStatementPerConnectionSize; } public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) { this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize; } public String getFilters() { return filters; } public void setFilters(String filters) { this.filters = filters; } public String getConnectionProperties() { return connectionProperties; } public void setConnectionProperties(String connectionProperties) { this.connectionProperties = connectionProperties; } } }
第二種方式基于注解的配置:
首先在啟動類上加上注解:@ServletComponentScan使Spring能夠掃描到我們自己編寫的servlet和filter。
//druid數據源狀態監控. @WebServlet(urlPatterns = "/druid/*", initParams = { // IP白名單 (沒有配置或者為空,則允許所有訪問) @WebInitParam(name = "allow", value = "192.168.1.72,127.0.0.1"), // IP黑名單 (存在共同時,deny優先于allow) @WebInitParam(name = "deny", value = "192.168.1.73"), // 用戶名 @WebInitParam(name = "loginUsername", value = "admin"), // 密碼 @WebInitParam(name = "loginPassword", value = "admin"), // 禁用HTML頁面上的“Reset All”功能 @WebInitParam(name = "resetEnable", value = "false") } ) public class DruidStatViewServlet extends StatViewServlet { }
// druid過濾器. @WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = { // 忽略資源 @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*") } ) public class DruidStatFilter extends WebStatFilter { }
等應用啟動后,可以訪問地址:http://localhost:8080/druid/,登錄之后,即可查看數據源及SQL統計等。
效果圖如下:
官方地址
Spring Boot 使用 Druid
Spring Boot集成Druid數據庫連接池
SPRING BOOT使用DRUID和監控配置方法
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/67055.html
摘要:看過的一些書上也是多數介紹了這兩種數據庫連接池,自己做的也是使用。參考資料文檔首頁文檔問題阿里學習,號稱最好的數據庫連接池常用數據庫連接池配置說明學習整合,使用連接池使用和監控配置數據源配置如果文章有錯的地方歡迎指正,大家互相交流。 前言 本章節主要講解Druid數據庫連接池,為什么要學Druid數據庫連接池呢?? 我的知識儲備數據庫連接池有兩種->C3P0,DBCP,可是現在看起來并...
摘要:這篇文篇將介紹,如何通過整合數據庫鏈接池實時監控數據庫鏈接信息,為優化數據庫性能提供更好的指導,同樣將通過配置文件形式進行配置方便簡潔。 這篇文篇將介紹,如何通過SpringBoot整合Druid數據庫鏈接池,實時監控數據庫鏈接信息,為優化數據庫性能提供更好的指導,同樣將通過YML配置文件形式進行配置,方便簡潔。 準備工作 環境: windows jdk 8 maven 3.0 IDE...
摘要:當禁用時,所有關聯對象都會即時加載。不同的驅動在這方便表現不同。參考驅動文檔或充分測試兩種方法來決定所使用的驅動。需要適合的驅動。系統默認值是設置字段和類是否支持駝峰命名的屬性。 ??上篇文章我們介紹了SpringBoot和MyBatis的整合,可以說非常簡單快捷的就搭建了一個web項目,但是在一個真正的企業級項目中,可能我們還需要更多的更加完善的框架才能開始真正的開發,比如連接池、分...
閱讀 2171·2020-06-12 14:26
閱讀 2477·2019-08-29 16:41
閱讀 1885·2019-08-29 15:28
閱讀 2448·2019-08-26 13:43
閱讀 753·2019-08-26 13:37
閱讀 2773·2019-08-23 18:13
閱讀 2791·2019-08-23 15:31
閱讀 1014·2019-08-23 14:10