Error creating bean with name ‘enableRedisKeyspaceNotificationsInitializer‘ 报错处理

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/boot/autoconfigure/session/RedisSessionConfiguration$SpringBootRedisHttpSessionConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Unable to configure Redis to keyspace notifications. See https://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository-sessiondestroyedevent报错处理

  • 背景
    • 直接出解决方案
    • 启动发生这种run error的几种原因
    • 解决方案

背景

由于公司从阿里云redis迁移到新的redis地址,导致服务发布报错具体报错内容如下

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-04 10:01:06.617 ERROR 1552 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource [org/springframework/boot/autoconfigure/session/RedisSessionConfiguration$SpringBootRedisHttpSessionConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Unable to configure Redis to keyspace notifications. See https://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository-sessiondestroyedevent
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar!/:2.1.4.RELEASE]
	at com.newclass.NewclassApplication.main(NewclassApplication.java:23) [classes!/:0.0.1-SNAPSHOT]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_275]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_275]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_275]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_275]
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [newclass.jar:0.0.1-SNAPSHOT]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [newclass.jar:0.0.1-SNAPSHOT]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [newclass.jar:0.0.1-SNAPSHOT]
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [newclass.jar:0.0.1-SNAPSHOT]
Caused by: java.lang.IllegalStateException: Unable to configure Redis to keyspace notifications. See https://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository-sessiondestroyedevent
	at org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction.getNotifyOptions(ConfigureNotifyKeyspaceEventsAction.java:83) ~[spring-session-data-redis-2.1.5.RELEASE.jar!/:2.1.5.RELEASE]
	at org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction.configure(ConfigureNotifyKeyspaceEventsAction.java:57) ~[spring-session-data-redis-2.1.5.RELEASE.jar!/:2.1.5.RELEASE]
	at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration$EnableRedisKeyspaceNotificationsInitializer.afterPropertiesSet(RedisHttpSessionConfiguration.java:304) ~[spring-session-data-redis-2.1.5.RELEASE.jar!/:2.1.5.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
	... 24 common frames omitted
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: ERR unknown command 'CONFIG'; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'CONFIG'
	at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:64) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
	at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:41) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
	at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
	at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
	at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:142) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
	at org.springframework.data.redis.connection.jedis.JedisServerCommands.convertJedisAccessException(JedisServerCommands.java:531) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
	at org.springframework.data.redis.connection.jedis.JedisServerCommands.getConfig(JedisServerCommands.java:299) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
	at org.springframework.data.redis.connection.DefaultedRedisConnection.getConfig(DefaultedRedisConnection.java:1197) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
	at org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction.getNotifyOptions(ConfigureNotifyKeyspaceEventsAction.java:76) ~[spring-session-data-redis-2.1.5.RELEASE.jar!/:2.1.5.RELEASE]
	... 28 common frames omitted
Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'CONFIG'
	at redis.clients.jedis.Protocol.processError(Protocol.java:130) ~[jedis-2.9.3.jar!/:na]
	at redis.clients.jedis.Protocol.process(Protocol.java:164) ~[jedis-2.9.3.jar!/:na]
	at redis.clients.jedis.Protocol.read(Protocol.java:218) ~[jedis-2.9.3.jar!/:na]
	at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:341) ~[jedis-2.9.3.jar!/:na]
	at redis.clients.jedis.Connection.getBinaryMultiBulkReply(Connection.java:277) ~[jedis-2.9.3.jar!/:na]
	at redis.clients.jedis.Connection.getMultiBulkReply(Connection.java:270) ~[jedis-2.9.3.jar!/:na]
	at redis.clients.jedis.Jedis.configGet(Jedis.java:2664) ~[jedis-2.9.3.jar!/:na]
	at org.springframework.data.redis.connection.jedis.JedisServerCommands.getConfig(JedisServerCommands.java:297) ~[spring-data-redis-2.1.6.RELEASE.jar!/:2.1.6.RELEASE]
	... 30 common frames omitted

直接出解决方案

如果你的pom文件中有如下dependency

		<dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

直接ctrl+/,看一下项目是否run successful

启动发生这种run error的几种原因

发现问题之后我在网上找了很多资料,有如下几个case

  • 1 SpringBoot中用Jedis来访问Redis,其中Redis是采用集群

我们在SpringBoot中用Jedis来访问Redis,其中Redis是采用集群(单机没有本篇文章的问题)的方式,在启用Redis的时候碰到如上问题。

error的核心信息如下:

Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource

解决方案

经查找,在Java代码中,我们使用了注解:@EnableRedisHttpSession,这个注解是用来开启Redis来集式式管理Session。
而在使用这种方式的时候,是需要Redis开启Keyspace Notifications功能的,默认是关闭的。
这个功能有一个参数来控制它,notify-keyspace-events,值为Egx。
可以通过在Redis.Config中配置。
也可以通过命令行来配置,如下所示:
redis-cli config set notify-keyspace-events Egx
然后重启Redis生效。

  • 2.非springBoot集成的redis 如下

服务器关闭后又重启,在上面启动web应用日志打印报错,Error creating bean with name ‘enableRedisKeyspaceNotificationsInitializer’,在网上搜了一下,发现答案很多都是,springBoot相关Redis注解导致的,还有就是要设置一下Redis。
但问题来了我这个不是SpringBoot,而且不存在Redis设置的情况,因为之前是好的。翻了很久,没一个靠谱的突然想一想会不会问题出在Redis上面,要不先看一下Redis进程情况,
ps -ef | grep Redis
果不其然Redis根本没启动,先打开Redis服务再看看,看会不会与变化,报错会不会改变
找到Redis位置
find -name redis-cli
(如果Redis或者其他服务启动的时候,可以通过 ps -ef | grep XXXX,找到进程号如12345,ll /proc/12345/cwd 找到对应的服务位置。)
启动Redis服务
到对应的路径下面启动服务
redis-server …/redis-conf
(这里要注意redis-server 和 redis-conf 的路径,有的是同一级,有的不在同一级。)
服务启动,web 应用进程杀掉重新启动,无报错。所以这个报错的原因里面和Redis服务是否启动有关。
原文链接:(https://blog.csdn.net/feichangwurao/article/details/103169226)

其中报错信息中还有一句重要这样的话

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: ERR unknown command 'CONFIG'; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'CONFIG'
Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'CONFIG'

error的原因是:

原因是spring-session调用了CONFIG命令来配置Keyspace notifications功能。而线上环境的redis基于权限的考虑,禁止应用服务执行CONFIG命令,这样就导致了应用在初始化时抛出此异常。

解决方案

我的服务pom文件中有如下依赖
case 1

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

注释掉pom文件中的依赖

case 2
发送这个error的根本原因是redis的版本问题。
如果版本太低升级版本。
如果不知道版本的话。。。3.2版本可以解决这个问题、

case 3

代码中取消SpringSeesion对 CONFIG命令的操作

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

完结 撒花

你可能感兴趣的:(Error集结,redis)