REDIS-springboot向redis中写数据报错

springboot-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异常关闭,快照不能进行持久化

临时解决方案:
修改redis.conf中stop-writes-on-bgsave-error配置,设置为no

stop-writes-on-bgsave-error:
配置说明如下:
# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.

翻译:
# 默认情况下,如果 redis 最后一次的后台保存失败,redis 将停止接受写操作,
# 这样以一种强硬的方式让用户知道数据不能正确的持久化到磁盘,
# 否则就会没人注意到灾难的发生。
#
# 如果后台保存进程重新启动工作了,redis 也将自动的允许写操作。
#
# 然而你要是安装了靠谱的监控,你可能不希望 redis 这样做,那你就改成 no 好了。
Spring Boot 中使用 Redis 插入对象时可能会遇到一些错误,常见的原因有: 1. **数据转换异常**:如果 Redis 需要存储的对象不是 String 类型,而 Spring Boot 没有配置相应的序列化策略,如 Jackson 或 JdkSerialization,插入对象时会抛出异常。需要确保在 RedisTemplate 的配置中设置正确的序列化和反序列化策略。 ```java @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); // 添加序列化和反序列化的支持 Jackson2JsonRedisSerializer<Object> jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jsonSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setValueSerializer(jsonSerializer); // 其他配置... return template; } ``` 2. **实体类未正确标记为 @RedisHash 或 @RedisObject**:如果你正在尝试将 Java 对象作为 Hash 存储,需要使用 Spring Data Redis 提供的注解,如 `@RedisHash` 和 `@RedisKey`,以便自动序列化和存储。 3. **并发问题**:如果多个线程同时对同一个对象进行操作,可能出现数据竞争导致的异常。确保在操作 Redis 之前获取到合适的锁。 4. **连接问题**:检查 Redis 服务是否运行正常,网络连接是否畅通,Spring Boot 客户端的连接配置是否正确。 当你遇到此类错误时,可以按照上述建议排查,查看具体的错误日志信息以确定问题所在。如果还有问题,可以提供具体的错误堆栈或代码片段以便更好地诊断
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值