SpringBoot应用无法连接Redis集群:求原因分析与解决方案
分布式Redis集群连接失败问题排查与解决
问题场景
三台服务器部署Redis集群,SpringBoot应用启动时抛出连接失败异常,核心报错为org.redisson.client.RedisConnectionException: Can't connect to servers!
报错日志
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authenticationTokenFilter' defined in class path resource [com/hccx/archives/framework/security/config/ArchivesSecurityAutoConfiguration.class]: Unsatisfied dependency expressed through method 'authenticationTokenFilter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2TokenApiImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2TokenServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2AccessTokenRedisDAO': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stringRedisTemplate' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Unsatisfied dependency expressed through method 'stringRedisTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redissonConnectionFactory' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Unsatisfied dependency expressed through method 'redissonConnectionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisson' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'redisson' threw exception; nested exception is org.redisson.client.RedisConnectionException: Can't connect to servers! at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:165) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:175) at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:155) at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:97) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:174) at org.apache.catalina.core.StandardContext.callServletContainerInitializers(StandardContext.java:5766) at com.sun.enterprise.web.WebModule.callServletContainerInitializers(WebModule.java:609) at org.apache.catalina.core.StandardContext.start(StandardContext.java:5662) at com.sun.enterprise.web.WebModule.start(WebModule.java:526) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:885) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:868) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:644) at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2079) at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1694) at com.sun.enterprise.web.WebApplication.start(WebApplication.java:83) at com.apusic.aas.internal.data.EngineRef.start(EngineRef.java:98) at com.apusic.aas.internal.data.ModuleInfo.start(ModuleInfo.java:267) at com.apusic.aas.internal.data.ApplicationInfo.start(ApplicationInfo.java:358) at com.apusic.enterprise.v10.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:479) at com.apusic.enterprise.v10.server.ApplicationLifecycle.enable(ApplicationLifecycle.java:2163) at com.apusic.enterprise.v10.server.ApplicationLifecycle.enable(ApplicationLifecycle.java:90) at com.apusic.enterprise.v10.server.ApplicationConfigListener.enableApplication(ApplicationConfigListener.java:251) at com.apusic.enterprise.v10.server.ApplicationConfigListener.handleAppEnableChange(ApplicationConfigListener.java:183) at com.apusic.enterprise.v10.server.ApplicationConfigListener.transactionCommited(ApplicationConfigListener.java:144) at org.jvnet.hk2.config.Transactions$TransactionListenerJob.process(Transactions.java:343) at org.jvnet.hk2.config.Transactions$TransactionListenerJob.process(Transactions.java:334) at org.jvnet.hk2.config.Transactions$ListenerNotifier$1.call(Transactions.java:190) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at org.jvnet.hk2.config.Transactions$Notifier$1$1.run(Transactions.java:144) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:842) Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authenticationTokenFilter' defined in class path resource [com/hccx/archives/framework/security/config/ArchivesSecurityAutoConfiguration.class]: Unsatisfied dependency expressed through method 'authenticationTokenFilter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2TokenApiImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2TokenServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2AccessTokenRedisDAO': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stringRedisTemplate' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Unsatisfied dependency expressed through method 'stringRedisTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redissonConnectionFactory' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Unsatisfied dependency expressed through method 'redissonConnectionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisson' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'redisson' threw exception; nested exception is org.redisson.client.RedisConnectionException: Can't connect to servers! at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:541) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:213) at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:212) at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:175) at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:170) at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:155) at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:87) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:262) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:236) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:193) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:162) ... 38 common frames omitted Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2TokenApiImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2TokenServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OAuth2AccessTokenRedisDAO': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'stringRedisTemplate' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Unsatisfied dependency expressed through method 'stringRedisTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redissonConnectionFactory' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Unsatisfied dependency expressed through method 'redissonConnectionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisson' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'redisson' threw exception; nested exception is org.redisson.client.RedisConnectionException: Can't connect to servers! at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:332) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ... 56 common frames omitted
相关配置
SpringBoot application.yml配置
spring: redis: cluster: nodes: - 192.168.0.120:6379 - 192.168.0.121:6379 - 192.168.0.122:6379 max-redirects: 3 database: 1 password: admin123
Redis集群节点redis.conf配置(以192.168.0.121为例)
bind 0.0.0.0 pidfile /var/run/redis_6379.pid dir /data/app/redis-data/data/ port 6379 masterauth admin123 requirepass admin123 timeout 300 daemonize yes logfile "/data/app/redis-log/redis.log" cluster-enabled yes cluster-config-file nodes-1.conf cluster-node-timeout 5000 appendonly yes cluster-announce-ip 192.168.0.121 protected-mode no databases 16
原因分析
- 集群节点公告IP配置不一致:从提供的redis.conf来看,当前节点配置了
cluster-announce-ip 192.168.0.121,但另外两台节点(120、122)大概率未对应修改各自的公告IP,导致Redisson客户端获取集群节点信息时,所有节点都指向121,无法正常连接其他节点。 - Redis集群状态异常:集群可能未正确初始化,节点之间未完成握手,或部分节点未启动。
- 网络连通性问题:应用服务器与三台Redis节点的6379端口可能存在防火墙/安全组拦截,导致无法通信。
- Redisson配置适配问题:SpringBoot使用Redisson作为客户端时,默认配置可能未正确传递集群密码到所有节点。
解决方法
1. 统一集群节点公告IP
分别登录三台Redis服务器,修改各自的redis.conf文件:
- 192.168.0.120节点:设置
cluster-announce-ip 192.168.0.120 - 192.168.0.121节点:保持
cluster-announce-ip 192.168.0.121 - 192.168.0.122节点:设置
cluster-announce-ip 192.168.0.122
修改后重启所有Redis节点,执行redis-cli -a admin123 cluster info检查集群状态,确认cluster_state:ok。
2. 验证集群状态
在任意Redis节点上执行redis-cli -a admin123 cluster nodes,确认所有节点在线,且16384个槽位已完整分配。
3. 检查网络连通性
在应用服务器上执行以下命令,确认端口可访问:
telnet 192.168.0.120 6379 telnet 192.168.0.121 6379 telnet 192.168.0.122 6379
同时检查Redis服务器的防火墙/安全组,确保6379端口对应用服务器开放。
4. 调整Redisson配置适配
若默认Redis配置未生效,可补充Redisson专属配置,确保密码传递到所有节点:
spring: redis: cluster: nodes: - 192.168.0.120:6379 - 192.168.0.121:6379 - 192.168.0.122:6379 max-redirects: 3 password: admin123 database: 1 redisson: config: | clusterServersConfig: password: admin123 nodeAddresses: - "redis://192.168.0.120:6379" - "redis://192.168.0.121:6379" - "redis://192.168.0.122:6379" scanInterval: 2000
内容的提问来源于stack exchange,提问作者JamTree Cui
相关产品推荐
相关产品推荐

