SpringBoot整合Redis啟動失敗的常見錯誤以及解決方法
一、Redis服務(wù)配置問題
1. Redis服務(wù)未啟動
報錯內(nèi)容:
Unable to connect to Redis server: 127.0.0.1/127.0.0.1:6379
原因:Redis服務(wù)未啟動
2. Redis配置文件錯誤(bind和protected-mode)
報錯內(nèi)容:
org.springframework.data.redis.connection.RedisConnectionFailureException: Unable to connect to Redis server: 127.0.0.1/127.0.0.1:6379
原因:
- Redis配置文件中
bind 127.0.0.1未注釋 protected-mode未設(shè)置為no
解決方案:
- 注釋
bind 127.0.0.1行 - 將
protected-mode yes改為protected-mode no
二、配置文件錯誤
1. 連接參數(shù)配置錯誤
報錯內(nèi)容:
Caused by: org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1:6379
原因:
- 配置文件中的host、port或password與實(shí)際Redis服務(wù)不匹配
- YAML配置縮進(jìn)錯誤
正確配置:
spring:
redis:
host: 127.0.0.1
port: 6379
password: password
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 1s
三、依賴問題
1. 缺少必要依賴
報錯內(nèi)容:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConnectionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]: Cannot create a Redis connection factory for the given configuration.
原因:缺少spring-boot-starter-data-redis依賴
解決方案:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. 依賴版本不兼容
報錯內(nèi)容:
Caused by: java.lang.NoClassDefFoundError: org.springframework.data.redis.connection.RedisConnectionFactory
原因:Jedis與spring-boot-starter-data-redis版本不兼容
解決方案:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.1</version> <!-- 與Spring Boot版本匹配 -->
</dependency>
四、Redis集群配置錯誤
1. 客戶端配置為集群模式,但Redis未開啟集群
報錯內(nèi)容:
ERR This instance has cluster support disabled
原因:
- 配置了
spring.redis.cluster.nodes但Redis未配置為集群模式
解決方案:
- 如果使用單節(jié)點(diǎn),修改為單節(jié)點(diǎn)配置:
spring:
redis:
host: 127.0.0.1
port: 6379
- 如果需要集群,確保Redis已正確配置集群
五、密碼配置問題
1. 密碼不匹配
報錯內(nèi)容:
org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: ERR Client sent AUTH, but no password is set
原因:
- 配置了password但Redis服務(wù)器未設(shè)置密碼
- 配置的password與Redis實(shí)際密碼不匹配
解決方案:
- 確保Redis配置文件中設(shè)置了正確的密碼:
requirepass your_password
- 確保配置文件中password與Redis設(shè)置一致
六、網(wǎng)絡(luò)與防火墻問題
1. 防火墻阻止端口訪問
報錯內(nèi)容:
org.springframework.data.redis.connection.RedisConnectionFailureException: Unable to connect to Redis server: 127.0.0.1/127.0.0.1:6379
原因:防火墻阻止了6379端口的訪問
解決方案:
# CentOS firewall-cmd --zone=public --add-port=6379/tcp --permanent firewall-cmd --reload
七、連接池配置問題
1. 連接池參數(shù)配置不當(dāng)
報錯內(nèi)容:
org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to XXX.XXX.XXX:6379
原因:連接池配置不合理,導(dǎo)致連接被耗盡
解決方案:
spring:
redis:
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 1s
八、依賴沖突問題
1. 依賴版本沖突
報錯內(nèi)容:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.data.repository.config.RepositoryConfigurationSource
原因:依賴版本沖突,特別是Spring Data Commons版本不匹配
解決方案:
- 移除顯式指定的依賴版本,讓Spring Boot自動管理版本
- 確保所有依賴與Spring Boot版本兼容
九、Redis服務(wù)端問題
1. Redis服務(wù)端端口被占用
報錯內(nèi)容:
org.springframework.data.redis.connection.RedisConnectionFailureException: Unable to connect to Redis server: 127.0.0.1/127.0.0.1:6379
原因:6379端口被其他進(jìn)程占用
解決方案:
# 檢查端口占用 netstat -anp | grep 6379 # 結(jié)束占用進(jìn)程 kill -9 <PID>
總結(jié)
- 確認(rèn)Redis服務(wù)已啟動:
redis-cli ping應(yīng)返回PONG - 檢查配置文件:確保host、port、password正確
- 檢查依賴:確保包含
spring-boot-starter-data-redis和Jedis - 檢查Redis配置:注釋bind 127.0.0.1,設(shè)置protected-mode no
- 檢查防火墻:確保6379端口開放
- 檢查連接池配置:合理配置連接池參數(shù)
- 檢查版本兼容性:確保Spring Boot與Redis客戶端版本兼容
以上就是SpringBoot整合Redis啟動失敗的常見錯誤以及解決方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Redis啟動失敗的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解SpringCloud微服務(wù)架構(gòu)之Hystrix斷路器
本篇文章主要介紹了詳解SpringCloud微服務(wù)架構(gòu)之Hystrix斷路器,Hystrix是一個庫,通過添加延遲容差和容錯邏輯來幫助您控制這些分布式服務(wù)之間的交互,有興趣的可以了解一下2018-01-01
Java觀察者設(shè)計模式(Observable和Observer)
這篇文章主要介紹了 Java觀察者設(shè)計模式(Observable和Observer)的相關(guān)資料,需要的朋友可以參考下2015-12-12
Java實(shí)現(xiàn)圖片轉(zhuǎn)換PDF文件的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)圖片轉(zhuǎn)換PDF文件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Spring @Transaction 注解執(zhí)行事務(wù)的流程
這篇文章主要介紹了Spring @Transaction 注解執(zhí)行事務(wù)的流程,Spring 是如何開啟事務(wù)的?又是如何進(jìn)行提交事務(wù)和關(guān)閉事務(wù)的,本文給大家詳細(xì)介紹,需要的朋友可以參考下2021-06-06

