在spring-boot-data-redis上踩得坑

本文记录了使用Spring Boot整合Redis过程中遇到的四个常见问题及解决方案,包括配置错误、静态注入缺失@Component注解、Redis持久化配置问题及空指针异常等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

突然想体验一下用markdown写一下博客,果然还是应该有一颗追求新鲜事物的心哦。而且貌似作为一个程序员来说(伪程序员)来说,貌似熟悉一下markdown百利而无一害。

带着这样的心态,以极不严谨的操作,在spring-boot-data-redis上疯狂踩坑。
介绍一下,上面这个包spring-boot-data-redis整合springboot的时候,可以直接通过配置文件配置,而不用自己装配beans。原理的话是@SpringBootApplication 注解默认开启了@EnableAutoConfiguration,大家可以去查看源码。

1.第一个坑
都快被自己蠢哭了。 url要求是包含端口的完整访问连接,在这里由于是当前主机,应该用host : localhost。错误配置的结果就是springboot初始化数据源出错, 我在debug的时候一直认为是我的关系型数据库出错了, 在多次确认之后,又怀疑是lettuce(springboot 2.0以前默认redis连接池是jedis,springboot2.0以后就是lettuce)。蠢哭!

spring:
redis:
database: 0
#host:localhost
url: localhost
password: 260813
port: 6379
lettuce:
pool:
max-wait: -1ms
max-idle: 8
max-active: 8
min-idle: 0

2.第二个坑

静态注入:

private static MailEntity mailEntity;
@Autowired
private MailEntity mailEntityAutowired;
@PostConstruct
public void init(){
MaillUtil.mailEntity = mailEntityAutowired;
}
网上都差不都是这样的,但是不要忘记注入的本质是spring容器,所以要记住类头上添加@component注解。

3.第三个坑

在向redis添加数据的时候,抛出的错误。

MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.

通用的解决办法是:

连接redis-cli,运行config set stop-writes-on-bgsave-error no命令;
或者 设置配置文件配置项stop-writes-on-bgsave-error 为no 。

但是这就好像掩耳盗铃一样,redis还是无法持久化(快照存储),就是说每次关机数据就会清空,那是多么的恐怖呀,在这个数据就是金钱的时代。

我暂时还没有解决,记录一下:

echo ‘vm.overcommit_memory = 1’ >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1

https://www.linuxidc.com/Linux/2012-07/66079.htm
https://stackoverflow.com/questions/19581059/misconf-redis-is-configured-to-save-rdb-snapshots

4.第四个坑

我都快受不了自己了,到底是世界有意捉弄我,还是我心态不够好,解决到最后还遇到了一个空指针异常。
这里建议一下,遇到空指针异常,如果日志里没法找到空指针产生原因的话,直接打个断点debug,比起到处写sout代码来的快的多。

比较奇怪的是,在上一篇博客里面提到的java重载版本并不是‘唯一的’,往往只能确定一个“更加合适的“的版本。

public static ResultVo success(Integer code, String msg, T object){
ResultVo resultVo = new ResultVo();
resultVo.setCode(code);
resultVo.setMsg(msg);
resultVo.setData(object);
return resultVo;
}
public static ResultVo success(T object){
return success(200,“成功”,object);
}
public static ResultVo success(){
//在这里重载版本为下面的一个方法,而不是上面的方法,所以会抛出空指针异常
return success(null);
}
public static ResultVo success(ResultEnum resultEnum){
return success(resultEnum.getCode(),resultEnum.getMsg(),null);
}

在使用 `spring-boot-starter-data-redis` 时,如果遇到该依赖不存在的问题,可能是由于版本管理、仓库配置或项目构建错误导致的。以下是可能的原因分析和解决方案。 ### 原因分析 1. **版本不匹配**:Spring Boot 的不同版本可能会对 `spring-boot-starter-data-redis` 的版本进行更新或调整,若手动指定版本号而未与 Spring Boot 版本兼容,可能导致依赖无法解析。 2. **Maven 仓库问题**:如果 Maven 配置中没有正确设置中央仓库或 Spring 官方仓库,会导致依赖无法下载。 3. **拼写错误或错误的 artifactId**:常见的拼写错误如 `spring-boot-starter-data-redis` 被误写为 `spring-boot-starter-redis` 或其他形式,也可能导致找不到依赖。 4. **网络问题或代理限制**:某些环境下(如企业内部网络),Maven 可能无法访问远程仓库,从而无法下载相关依赖。 ### 解决方案 1. **确认依赖配置是否正确** ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 确保 `groupId` 和 `artifactId` 没有拼写错误,并且不要手动指定版本号,除非你明确知道要使用的版本与 Spring Boot 兼容[^3]。 2. **检查 Spring Boot 版本兼容性** 不同版本的 Spring Boot 对应不同的 `spring-boot-starter-data-redis` 版本。可以通过查看 [Spring Boot Release Notes](https://docs.spring.io/spring-boot/docs/) 来确认当前使用的 Spring Boot 版本是否支持该 starter。 3. **确保 Maven 配置了正确的仓库** 在 `pom.xml` 或 `settings.xml` 中添加以下仓库配置: ```xml <repositories> <repository> <id>central</id> <url>https://repo1.maven.org/maven2</url> </repository> <repository> <id>spring-releases</id> <url>https://repo.spring.io/release</url> </repository> </repositories> ``` 4. **尝试使用替代库** - **Lettuce**:`spring-boot-starter-data-redis` 默认使用 Lettuce 作为 Redis 客户端,可以直接引入 Lettuce 的依赖并手动配置连接池[^2]。 ```xml <dependency> <groupId>io.lettuce.core</groupId> <artifactId>lettuce-core</artifactId> <version>6.1.4</version> </dependency> ``` - **Redisson**:另一个流行的 Redis Java 客户端,提供了丰富的功能(如分布式锁、集合操作等)。 ```xml <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.16.6</version> </dependency> ``` 注意:使用 Redisson 时需确保其与 Spring Data Redis 的兼容性,避免出现 `NoClassDefFoundError` 等异常[^4]。 - **Jedis**:虽然不如 Lettuce 流行,但 Jedis 是一个轻量级的 Redis 客户端,适用于简单场景。 ```xml <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>4.0.1</version> </dependency> ``` 5. **清除本地 Maven 缓存并重新下载** 有时本地 Maven 缓存损坏也会导致依赖解析失败,可以尝试删除本地缓存目录后重新构建项目: ```bash rm -rf ~/.m2/repository/org/springframework/boot/spring-boot-starter-data-redis mvn clean install ``` 6. **使用 Gradle 用户可执行以下命令** 如果使用的是 Gradle 构建工具,可以尝试以下方式添加依赖: ```groovy implementation 'org.springframework.boot:spring-boot-starter-data-redis' ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来日可期1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值