spring boot 连接redis报错 org.springframework.data.redis.RedisConnectionFailureException

本文详细介绍了如何在SpringBoot不同版本中正确配置Redis连接,包括低版本(1.5.11)和高版本的配置示例。针对连接失败问题,提供了检查配置、关闭Redis保护模式、设置防火墙等解决方案。

spring boot 连接redis报错 org.springframework.data.redis.RedisConnectionFailureException

检查配置

application.properties 配置

(spring boot 低版本 本人1.5.11 版本配置)

spring.redis.host=192.168.59.131
spring.redis.port=6379
#spring.redis.password=root #根据需要
# Redis数据库索引(默认为0)
spring.redis.database=0
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

(spring boot 高版本配置)
spring.redis.jedis.pool.max-active=8 或者 spring.redis.lettuce.pool.max-active=8
引自: https://blog.youkuaiyun.com/qq_33326449/article/details/80457571


# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)

注意:关闭redis的保护模式
这里写图片描述
注释掉绑定的默认ip
这里写图片描述

检查虚拟机防火墙是否关闭, 本人CentOS 7

这里写图片描述
表示防火墙开启, 可以选择关闭防火墙或者添加开启端口

开启端口

这里写图片描述

关闭防火墙

这里写图片描述

然后就可以享受redis了, 困扰了好久的问题终于解决

Windows ping 远程端口

这里写图片描述

引自:https://www.cnblogs.com/hltswd/p/6223824.html

### 三级标题:常见报错与解决方法 #### 1. `RedisSerializer.serialize` 方法调用时出现 `NullPointerException` 当调用 `RedisTemplate` 的操作方法时,如果出现如下异常: ``` java.lang.NullPointerException: Cannot invoke "org.springframework.data.redis.serializer.RedisSerializer.serialize(Object)" because the return value of "org.springframework.data.redis.core.AbstractOperations.keySerializer()" is null ``` 这通常是因为 `RedisTemplate` 没有正确配置序列化器(`RedisSerializer`)。`RedisTemplate` 默认使用 `JdkSerializationRedisSerializer`,但如果未显式配置,某些情况下可能会导致 `keySerializer` 或 `valueSerializer` 为 `null`。 **解决方法:** 在配置类中显式设置 `RedisTemplate` 的键和值的序列化器: ```java @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); template.afterPropertiesSet(); return template; } } ``` #### 2. `RedisTemplate.class` 类版本冲突问题 如果出现如下错误: ``` java: 无法访问org.springframework.data.redis.core.RedisTemplate 错误的类文件: ... 类文件具有错误的版本 61.0, 应为 52.0 ``` 这表示当前项目中引入的 `spring-data-redis` 版本是使用 Java 17 编译的(类版本 61),而当前项目使用的是 Java 8(类版本 52)。 **解决方法:** - 确保使用的 `spring-data-redis` 版本与当前 JDK 版本兼容。 - 如果使用的是 Java 8,建议使用 `spring-data-redis` 2.6.x 或更早版本。 - 清理本地 Maven 仓库中不兼容的 JAR 文件,重新下载对应版本。 ```bash rm -rf ~/.m2/repository/org/springframework/data/spring-data-redis ``` 然后更新 `pom.xml` 中的版本号: ```xml <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.6.13</version> <!-- Java 8 兼容版本 --> </dependency> ``` #### 3. `RedisTemplate` Bean 类型不匹配问题 Spring Boot 2.x 以上版本会自动配置一个默认的 `RedisTemplate<String, Object>` Bean。如果在注入时使用的是 `RedisTemplate<Object, Object>`,Spring 容器将无法确定要注入哪个类型的 Bean,从而抛出异常。 **解决方法:** - 显式声明注入的 `RedisTemplate` 泛型类型为 `<String, Object>`,以匹配默认 Bean 的类型。 - 或者自定义配置类并定义一个 `RedisTemplate<Object, Object>` Bean,同时指定其名称,然后在注入时使用 `@Qualifier` 注解指定名称。 ```java @Autowired @Qualifier("customRedisTemplate") private RedisTemplate<Object, Object> redisTemplate; ``` #### 4. Spring Boot 3.x 配置路径调整 从 Spring Boot 3.x 开始,Redis 的配置路径由 `spring.redis` 调整为 `spring.data.redis`。 **解决方法:** 在 `application.yml` 或 `application.properties` 中调整配置路径: ```yaml spring: data: redis: host: localhost port: 6379 ``` ### 三级标题:最佳实践建议 - **统一序列化策略**:推荐使用 `StringRedisSerializer` 作为键的序列化器,使用 `GenericJackson2JsonRedisSerializer` 或 `GenericFastJsonRedisSerializer` 作为值的序列化器,避免乱码问题。 - **版本一致性**:确保 Spring Boot 版本与 `spring-data-redis`、JDK 等组件版本兼容。 - **避免 Bean 冲突**:如果项目中存在多个 `RedisTemplate` Bean,务必使用 `@Qualifier` 注解明确指定注入的 Bean 名称。 ###
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值