Druid是Java語言中最好的數據庫鏈接池。Druid可以提供強大的監控和擴展功能。Druid內置提供了一個StatViewServlet用於展現Druid的統計信息。css
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.5</version> </dependency>
#數據源配置 spring.datasource.url=jdbc:mysql://192.168.0.131:3306/hongone?useAffectedRows=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #druid鏈接池 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 鏈接池的配置信息 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置獲取鏈接等待超時的時間 spring.datasource.maxWait=30000 # 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 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 # 經過connectProperties屬性來打開mergeSql功能;慢SQL記錄 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=2000
注意 : 這裏的配置根據項目須要去進行自定義,好比 超時鏈接時間 、慢 sql 查詢時間 等html
package com.hone.system.utils.druid; 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 org.springframework.context.annotation.Primary; import javax.sql.DataSource; import java.sql.SQLException; @Configuration public class DruidDBConfig{ private Logger logger = LoggerFactory.getLogger(DruidDBConfig.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.driver-class-name}") 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 聲明,DataSource 對象爲 Spring 容器所管理; * @Primary 表示這裏定義的DataSource將覆蓋其餘來源的DataSource。 * StatFilter,用於統計監控信息。StatFilter的別名是stat。 * 統計SQL信息,合併統計。mergeStat是的MergeStatFilter縮寫。 * 經過 DataSource 的屬性<property name="filters" value="mergeStat" /> 或者 * connectProperties屬性來打開mergeSql功能 <property name="connectionProperties" value="druid.stat.mergeSql=true" /> * StatFilter屬性slowSqlMillis用來配置SQL慢的標準 * * @return */ @Bean @Primary public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(dbUrl); datasource.setDriverClassName(driverClassName); datasource.setUsername(username); datasource.setPassword(password); //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 { /** * 設置StatFilter,用於統計監控信息。 * StatFilter的別名是stat * */ datasource.setFilters(filters); } catch (SQLException e) { logger.error("druid configuration initialization filter : {0}",e); } datasource.setConnectionProperties(connectionProperties); return datasource; } }
import com.alibaba.druid.support.http.StatViewServlet; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; /** * StatViewServlet用於展現Druid的統計信息。 * 提供監控信息展現的html頁面 * 提供監控信息的JSON API * * 內置監控頁面的首頁是/druid/index.html * * loginUsername、loginPassword 登陸用戶名和密碼 */ /** * @Webservlet * 有兩個屬性能夠用來表示Servlet的訪問路徑,分別是value和urlPatterns。value和urlPatterns都是數組形式, * 表示咱們能夠把一個Servlet映射到多個訪問路徑,可是value和urlPatterns不能同時使用。 * */ @WebServlet( urlPatterns = {"/druid/*"}, initParams = { @WebInitParam(name = "loginUsername", value = "admin"), @WebInitParam(name = "loginPassword", value = "admin"), @WebInitParam(name = "resetEnable", value = "false") } ) public class DruidStatViewServlet extends StatViewServlet { }
import com.alibaba.druid.support.http.WebStatFilter; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; /** * WebStatFilter用於採集web-jdbc關聯監控的數據。 * 屬性filterName聲明過濾器的名稱,可選 * 屬性urlPatterns指定要過濾 的URL模式,也可以使用屬性value來聲明.(指定要過濾的URL模式是必選屬性) */ @WebFilter( urlPatterns = "/*", initParams = { @WebInitParam(name = "exclusions",value = "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*") } ) public class DruidStatFilter extends WebStatFilter { }
@SpringBootApplication @MapperScan("com.hone.dao") //開啓mapper掃描 @ServletComponentScan //開啓servlet掃描 public class HongoneApplication {.....}
最後,啓動項目進行測試 在瀏覽器中輸入 http://localhost:8080/druid/index.htmljava
(注意: 若是你的項目設置了訪問前綴 , 訪問地址問 http://localhost:8080/訪問前綴/druid/index.html )mysql