我們在SpringBoot中用Jedis來訪問Redis,其中Redis是采用集群(單機沒有本篇文章的問題)的方式,在啟用Redis的時候碰到如上問題。
錯誤的核心信息如下:
Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource
完整信息如下:

2017/11/11 下午4:41:39 . ____ _ __ _ _ 2017/11/11 下午4:41:39 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 2017/11/11 下午4:41:39( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 2017/11/11 下午4:41:39 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 2017/11/11 下午4:41:39 ' |____| .__|_| |_|_| |_\__, | / / / / 2017/11/11 下午4:41:39 =========|_|==============|___/=/_/_/_/ 2017/11/11 下午4:41:39 :: Spring Boot :: (v1.5.1.RELEASE) 2017/11/11 下午4:41:39 2017/11/11 下午4:41:40Nov 11, 2017 4:41:40 PM com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig readBoolean 2017/11/11 下午4:41:40INFO: profiler.jdk.http.param=true 2017/11/11 下午4:41:40Nov 11, 2017 4:41:40 PM com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig readBoolean 2017/11/11 下午4:41:40INFO: profiler.jdk.http.param=true 2017/11/11 下午4:41:40Nov 11, 2017 4:41:40 PM com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig readBoolean 2017/11/11 下午4:41:40INFO: profiler.jdk.http.param=true 2017/11/11 下午4:41:52org.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.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 2017/11/11 下午4:41:52 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) 2017/11/11 下午4:41:52 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) 2017/11/11 下午4:41:52 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 2017/11/11 下午4:41:52 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 2017/11/11 下午4:41:52 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 2017/11/11 下午4:41:52 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 2017/11/11 下午4:41:52 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 2017/11/11 下午4:41:52 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) 2017/11/11 下午4:41:52 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) 2017/11/11 下午4:41:52 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) 2017/11/11 下午4:41:52 at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) 2017/11/11 下午4:41:52 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) 2017/11/11 下午4:41:52 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) 2017/11/11 下午4:41:52 at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) 2017/11/11 下午4:41:52 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) 2017/11/11 下午4:41:52 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) 2017/11/11 下午4:41:52 at com.zte.Application.main(Application.java:27) 2017/11/11 下午4:41:52 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 2017/11/11 下午4:41:52 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 2017/11/11 下午4:41:52 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 2017/11/11 下午4:41:52 at java.lang.reflect.Method.invoke(Method.java:498) 2017/11/11 下午4:41:52 at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) 2017/11/11 下午4:41:52 at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) 2017/11/11 下午4:41:52 at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) 2017/11/11 下午4:41:52 at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) 2017/11/11 下午4:41:52Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 2017/11/11 下午4:41:52 at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:204) 2017/11/11 下午4:41:52 at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:348) 2017/11/11 下午4:41:52 at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration$EnableRedisKeyspaceNotifica
經查找,在Java代碼中,我們使用了注解:@EnableRedisHttpSession,這個注解是用來開啟Redis來集式式管理Session。
而在使用這種方式的時候,是需要Redis開啟Keyspace Notifications功能的,默認是關閉的。
這個功能有一個參數來控制它,notify-keyspace-events,值為Egx。
可以通過在Redis.Config中配置。
也可以通過命令行來配置,如下所示:
redis-cli config set notify-keyspace-events Egx
然后重啟Redis生效。
參考文檔:
https://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository-sessiondestroyedevent
http://blog.csdn.net/xiaoyu411502/article/details/51248980