springboot2配置使用Druid連線池及使用的時候出現的連線問題
阿新 • • 發佈:2018-12-22
最近在springboot2專案中使用了Druid連線池,在此記錄一下,整合過程
首先:引入Maven配置項
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
然後在application.properties核心配置檔案中新增連線配置(本人用的資料庫是mysql)
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true spring.datasource.druid.username=root spring.datasource.druid.password=root spring.datasource.druid.initial-size=5 spring.datasource.druid.max-active=20 spring.datasource.druid.min-idle=10 spring.datasource.druid.max-wait=10 spring.datasource.druid.validation-query= select 'x' spring.datasource.druid.filters=stat,wall spring.datasource.druid.filter.stat.log-slow-sql=true spring.datasource.druid.filter.stat.slow-sql-millis=2000 # Druid WebStatFilter配置 spring.datasource.druid.web-stat-filter.enabled=true spring.datasource.druid.web-stat-filter.url-pattern=/* spring.datasource.druid.web-stat-filter.exclusions=*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/* # Druid StatViewServlet配置 spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* spring.datasource.druid.stat-view-servlet.reset-enable=true spring.datasource.druid.stat-view-servlet.login-username=admin spring.datasource.druid.stat-view-servlet.login-password= spring.datasource.druid.stat-view-servlet.allow= spring.datasource.druid.stat-view-servlet.deny=
以上就已經整合完畢了,所有的所需資訊都在這個配置檔案中完成
可能有時候覺得application.properties太繁瑣或者不美觀,看起來費勁,我們可以用yml檔案來進行配置,但是需要名稱與原檔名相同,就叫做application.yml,配置資訊如下
spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true username: root password: root driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource initialSize: 5 minIdle: 5 maxActive: 20 # 配置獲取連線等待超時的時間 maxWait: 60000 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一個連線在池中最小生存的時間,單位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false # 開啟PSCache,並且指定每個連線上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置監控統計攔截的filters,去掉後監控介面sql無法統計,'wall'用於防火牆,此處是filter修改的地方 filters: stat,wall,log4j #通過connectProperties屬性來開啟mergeSql功能;慢SQL記錄 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 useGlobalDataSourceStat: true
在配置檔案中少了一些資訊,這些資訊需要在自定義的配置類中設定,程式碼是
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
@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", "*");
// IP黑名單(共同存在時,deny優先於allow)
// servletRegistrationBean.addInitParameter("deny", "192.168.0.114");
//控制檯管理使用者
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "test");
//是否能夠重置資料 禁用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;
}
}
yml方式配置Druid連線池已完成,記得加@Configuration的註解,效果與application.properties配置效果相同
出現的問題:一開始我使用的預設.properties方式,並使用了redis做一些熱資料的快取處理,沒什麼問題出現,配置如下
spring.cache.type=redis
spring.cache.redis.cache-null-values=false
spring.cache.redis.time-to-live=3600000
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=1
spring.redis.password=000000
spring.redis.timeout=10000
但是當我把配置檔案換為.yml格式檔案時,就會出現連線不到redis密碼錯誤的問題,配置如下
spring:
cache:
type: redis
redis:
time-to-live: 3600000
cache-null-values: false
redis:
port: 6379
host: localhost
password: 000000
timeout: 60000
database: 1
最後發現是因為密碼處需要加引號,而在.properties檔案中則不需要........這個坑,現改為
spring:
cache:
type: redis
redis:
time-to-live: 3600000
cache-null-values: false
redis:
port: 6379
host: localhost
password: '000000'
timeout: 60000
database: 1
至此所有連線正常
在此發文記錄整合過程,如有不妥請指正