溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

springboot如何配置DRUID數(shù)據(jù)源

發(fā)布時(shí)間:2021-12-07 11:19:13 來(lái)源:億速云 閱讀:205 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)springboot如何配置DRUID數(shù)據(jù)源的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

具體如下:

druid 是阿里開(kāi)源的數(shù)據(jù)庫(kù)連接池。

開(kāi)發(fā)時(shí)整合 druid 數(shù)據(jù)源過(guò)程。

1.修改pom.xml

<dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId></dependency><dependency>  <groupId>com.alibaba</groupId>  <artifactId>druid</artifactId>  <version>1.0.26</version></dependency>

增加上面的配置。

2.編輯配置數(shù)據(jù)源代碼

package com.neo.conf;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 org.springframework.context.annotation.PropertySource;import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.support.http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter;@Configuration@ConfigurationProperties(value = "classpath:druid.properties")public class DruidConfiguration {  @Bean(destroyMethod = "close", initMethod = "init")  @ConfigurationProperties(prefix = "spring.datasource")  public DataSource druidDataSource() {    DruidDataSource druidDataSource = new DruidDataSource();    return druidDataSource;  }  /**   * 注冊(cè)一個(gè)StatViewServlet   * @return   */  @Bean  public ServletRegistrationBean druidStatViewServlet(){    //org.springframework.boot.context.embedded.ServletRegistrationBean提供類(lèi)的進(jìn)行注冊(cè).    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");    //添加初始化參數(shù):initParams    //白名單:    servletRegistrationBean.addInitParameter("allow","127.0.0.1,192.168.31.77");    //IP黑名單 (存在共同時(shí),deny優(yōu)先于allow) : 如果滿(mǎn)足deny的話(huà)提示:Sorry, you are not permitted to view this page.    servletRegistrationBean.addInitParameter("deny","192.168.1.73");    //登錄查看信息的賬號(hào)密碼.    servletRegistrationBean.addInitParameter("loginUsername","admin");    servletRegistrationBean.addInitParameter("loginPassword","123456");    //是否能夠重置數(shù)據(jù).    servletRegistrationBean.addInitParameter("resetEnable","false");    return servletRegistrationBean;  }  /**   * 注冊(cè)一個(gè):filterRegistrationBean   * @return   */  @Bean  public FilterRegistrationBean druidStatFilter(){    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());    //添加過(guò)濾規(guī)則.    filterRegistrationBean.addUrlPatterns("/*");    //添加不需要忽略的格式信息.    filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");    return filterRegistrationBean;  }}

3.編寫(xiě)配置文件 druid.properties

spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/mycine?useUnicode=true&characterEncoding=utf8&autoReconnect=truespring.datasource.username=rootspring.datasource.password=root#--------------------------# 下面為連接池的補(bǔ)充設(shè)置,應(yīng)用到上面所有數(shù)據(jù)源中# 初始化大小,最小,最大spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=50# 配置獲取連接等待超時(shí)的時(shí)間spring.datasource.maxWait=60000# 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒spring.datasource.timeBetweenEvictionRunsMillis=60000# 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1spring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=false# 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小spring.datasource.poolPreparedStatements=false#spring.datasource.maxPoolPreparedStatementPerConnectionSize=20# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻#spring.datasource.filters=stat,wall,log4jspring.datasource.filters=stat# 通過(guò)connectProperties屬性來(lái)打開(kāi)mergeSql功能;慢SQL記錄spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)#spring.datasource.useGlobalDataSourceStat=true

感謝各位的閱讀!關(guān)于“springboot如何配置DRUID數(shù)據(jù)源”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI