springboot redis 订阅报错

本文详细介绍了在使用Spring Session Redis时遇到的CONFIG命令被禁用问题,提供了通过开启notify-keyspace-events和自定义ConfigureRedisAction为NO_OP的解决方案。

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

在对接一个投资项目时,启动项目出现如下错误,本来想直接问对接人,但他们没有出现这种情况,只能看一下。

由下面异常可以看出,Redis security建议禁用CONFIG命令,以便远程用户无法重新配置实例。

RedisHttpSessionConfiguration需要在初始化期间访问它。托管的Redis服务,在默认情况下禁用此命令,并且没有重新启用该命令的选项。

而且spring-session-redis需要使用键空间通知(keyspace notification)这个功能。

 

解决方式: 1. 配置notify-keyspace-events 2. 设置ConfigureRedisAction为NO_OP不加载CONFIG

其中在https://github.com/spring-projects/spring-session/issues/124,作者给出了比较详细的答案

下面看我的解决方式。

1. 开启notify-keyspace-events

2. 自定义配置加载ConfigureRedisAction为NO_OP

@Bean
public static ConfigureRedisAction configureRedisAction() {
   return ConfigureRedisAction.NO_OP;
}

 

下面是错误

2018-12-09 00:28:19.245  WARN 16648 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is com.lambdaworks.redis.RedisCommandExecutionException: ERR unknown command 'CONFIG'
2018-12-09 00:28:21.487  INFO 16648 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-12-09 00:28:21.496  INFO 16648 --- [ost-startStop-2] o.s.c.n.zuul.ZuulFilterInitializer       : Stopping filter initializer context listener
2018-12-09 00:28:21.516  WARN 16648 --- [ost-startStop-2] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
 com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2018-12-09 00:28:21.531  INFO 16648 --- [           main] utoConfigurationReportLoggingInitializer : 

 

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is com.lambdaworks.redis.RedisCommandExecutionException: ERR unknown command 'CONFIG'
    

### 解决Spring Boot集成Redis时遇到的错误 #### 一、概述 在Spring Boot应用中集成了Redis之后,可能会遇到各种各样的异常情况。这些异常可能源于配置不当或是版本兼容性等问题。 #### 二、常见问题及其解决方案 ##### Maven依赖引入不正确引发的问题 对于某些开发者来说,在构建基于Maven管理的应用程序过程中,如果仅简单地添加`<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>`这样的依赖项,则可能导致无法正常工作的情况发生[^2]。为了确保能够顺利使用Redis功能模块,建议确认使用的Spring Data Redis库版本与当前项目的其他组件相匹配,并且遵循官方文档推荐的最佳实践来进行设置。 ##### 连接认证失败(NOAUTH Authentication required) 当尝试连接到设置了访问权限控制列表(Access Control List, ACL)或启用了身份验证机制的安全模式下的Redis实例时,若客户端未提供合法凭证则会触发此类警告信息:“NOAUTH Authentication required”。此时应当核查应用程序属性文件(application.properties 或 application.yml),看是否存在针对目标主机设定的有效登录凭据字段,比如`spring.redis.password=your_redis_password_here`;另外还需注意区分大小写敏感度以及特殊字符转义规则等因素的影响[^4]。 ##### 不支持的操作(UnsupportedOperationException) 此类型的报错往往是因为操作命令超出了所选驱动器所能处理的能力范围之外所致。例如试图在一个单机版而非集群环境中执行只适用于后者场景下特有的指令就会抛出该类异常。因此务必依据实际部署架构合理选用相应的API接口调用方式——如果是分布式存储系统的话,请参照相关资料调整pom.xml中的插件声明部分以加载额外的支持包。 ```xml <!-- Example of adding Lettuce as the preferred connection factory --> <dependency> <groupId>io.lettuce.core</groupId> <artifactId>lettuce-core</artifactId> </dependency> ``` #### 三、总结 通过以上几个方面的排查可以有效减少大部分由集成过程引起的故障现象。当然除了上述提到的内容外还有很多其它潜在因素也可能造成相似的结果,所以在具体实施之前最好先仔细阅读官方指南并结合自身业务需求做出适当的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值