起因
將springboot項目的properties配置文件改爲yml之後redis死活連不上了。
找問題
springboot的配置文件有兩種方式:properties和yml,之前properties時候是沒有任何問題的,那麼來看一下yml的配置:
spring:
# Redis數據庫索引(默認爲0)
redis:
#數據庫索引
database: 0
host: 127.0.0.1
port: 6379
password: 123456789
jedis:
pool:
#最大連接數
max-active: 8
#最大阻塞等待時間(負數表示沒限制)
max-wait: -1ms
#最大空閒
max-idle: 8
#最小空閒
min-idle: 0
timeout: 300s
# THYMELEAF (ThymeleafAutoConfiguration)
spring:
thymeleaf:
cache: false
check-template: true
check-template-location: true
enabled: true
encoding: utf-8
#去掉thymeleaf的嚴格的模板校驗
mode: LEGACYHTML5
prefix: classpath:/templates/
suffix: .html
excluded-view-names:
spring:
groovy:
template:
cache: false
看起來貌似也沒什麼問題,bug,debug跟源碼代碼發現redis配置均未起作用,但是有個神奇的地方,如果把下面的thymeleaf和groovy都刪掉,redis配置就起作用了,推測肯定是某個地方衝突了,仔細瞅,上面配置文件中有三個“spring:”,刪掉下面兩個“spring:”,果然一切ok。
解決
保證不能有重複的一級節點。
也許只是簡單的知識點,但是隻有踩過,才知道坑深,此坑爬了三個小時,希望小夥伴們不要再爬此坑······
正確配置如下
# THYMELEAF (ThymeleafAutoConfiguration)
spring:
thymeleaf:
cache: false
check-template: true
check-template-location: true
enabled: true
encoding: utf-8
#去掉thymeleaf的嚴格的模板校驗
mode: LEGACYHTML5
prefix: classpath:/templates/
suffix: .html
excluded-view-names:
groovy:
template:
cache: false
# Redis數據庫索引(默認爲0)
redis:
#數據庫索引
database: 0
host: 127.0.0.1
port: 6379
password: 123456789
jedis:
pool:
#最大連接數
max-active: 8
#最大阻塞等待時間(負數表示沒限制)
max-wait: -1ms
#最大空閒
max-idle: 8
#最小空閒
min-idle: 0
timeout: 300s