Azure Redis与Spring Boot连接报错:无法获取初始Redis集群拓扑
Azure Redis连接错误
Cannot obtain initial Redis Cluster topology的原因及修复 核心问题:误用Redis Cluster配置连接非集群Azure Redis
Azure Redis Cache默认提供的是单节点或主从复制架构,不支持Redis Cluster(分片集群)模式。你在代码中使用了RedisClusterConfiguration、ClusterClientOptions等仅适用于Redis Cluster的配置,Lettuce客户端会尝试向Azure Redis请求集群拓扑信息,但Azure Redis没有集群节点数据可返回,因此抛出拓扑获取失败的异常。
额外配置冲突
- SSL设置矛盾:Azure Redis的6380端口强制要求SSL加密,但你的
application.yml中设置spring.redis.ssl: false,同时Java配置里又启用.useSsl(),这种不一致会导致SSL握手失败。 - 节点地址不匹配:
application.yml中的Redis主机是qortex-platform-dev-backoffice-portal.redis.cache.windows.net,但Java配置类中添加的集群节点是myurl.redis.cache.windows.net,地址错误无法定位到正确的Redis实例。
修复步骤
1. 改用单实例Redis配置
删除所有Cluster相关配置,替换为RedisStandaloneConfiguration:
@Bean public RedisStandaloneConfiguration defaultRedisConfig() { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName("qortex-platform-dev-backoffice-portal.redis.cache.windows.net"); configuration.setPort(6380); configuration.setPassword(RedisPassword.of("8RP9lcB7fbiX0ceCwNyF8dOe9pJ33w6YbAzCa")); return configuration; } @Bean @Primary public RedisConnectionFactory redisConnectionFactory(RedisStandaloneConfiguration defaultRedisConfig) { LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder() .useSsl() .commandTimeout(Duration.ofMillis(3000)) .build(); return new LettuceConnectionFactory(defaultRedisConfig, clientConfig); }
2. 统一SSL配置
修正application.yml中的SSL设置为true,和代码配置保持一致:
spring: cache: type: redis redis: ssl: true host: qortex-platform-dev-backoffice-portal.redis.cache.windows.net port: 6380 password: 8RP9lcB7fbiX0ceCwNyF8dOe9pJ33w6YbAzCa
3. 可选:简化为自动配置
如果不需要自定义Lettuce参数,可以直接删除Java配置类,Spring Boot会根据application.yml自动创建连接工厂:
spring: cache: type: redis redis: ssl: true host: qortex-platform-dev-backoffice-portal.redis.cache.windows.net port: 6380 password: 8RP9lcB7fbiX0ceCwNyF8dOe9pJ33w6YbAzCa lettuce: timeout: 3000ms
内容的提问来源于stack exchange,提问作者Dasun
相关产品推荐
相关产品推荐

