Spring Boot集成Redis Cloud遭遇连接拒绝问题求助
排查Spring Boot与Redis Cloud连接失败问题
报错信息
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis] with root cause
java.net.ConnectException: Connection refused
相关配置与代码
application.properties
# Redis Configuration spring.cache.type=redis spring.redis.host=redis-14218.c301.ap-south-1-1.ec2.cloud.redislabs.com spring.redis.port=14218 spring.redis.password=*** spring.redis.database=0
Application.java
package com.serverless.serverlessapi; import java.time.Duration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair; @SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public RedisCacheConfiguration cacheConfiguration() { return RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofMinutes(60)) .disableCachingNullValues() .serializeValuesWith(SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); } @Bean public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() { return (builder) -> builder .withCacheConfiguration("leads", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10))) .withCacheConfiguration("leadsCahce2", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5))); } }
RestController代码
package com.serverless.serverlessapi.rest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.serverless.serverlessapi.services.LeadsService; @RestController @RequestMapping("/api") public class LeadsRestController { @Autowired private LeadsService leadsService; @Cacheable(value = "leads") @GetMapping("/leads2") public String getAllLeads2(@RequestParam(name = "limit") int limit, @RequestParam(name = "skip") int skip){ try { return leadsService.getAllLeads2(limit,skip); } catch (Exception e) { e.printStackTrace(); } return "data not found, please contact to admin"; } }
已确认Redis数据库处于启用状态
排查与解决步骤
1. 验证网络连通性
执行命令测试目标主机和端口是否可达:
telnet redis-14218.c301.ap-south-1-1.ec2.cloud.redislabs.com 14218
或使用nc命令:
nc -zv redis-14218.c301.ap-south-1-1.ec2.cloud.redislabs.com 14218
如果连接失败,说明网络层面存在限制,需检查防火墙、安全组规则。
2. 检查Redis Cloud IP白名单
Redis Cloud默认仅允许白名单内IP访问:
- 登录Redis Cloud控制台,找到对应实例的Access Control设置
- 将应用部署服务器IP或本地测试IP添加到白名单
- 保存设置后等待1-2分钟再尝试连接
3. 核对配置参数准确性
- 确认
spring.redis.host和spring.redis.port与Redis Cloud控制台显示的实例端点完全一致 - 检查密码是否输入正确,注意区分大小写和特殊字符
- 数据库编号默认使用0即可,无需修改(除非控制台做了特殊配置)
4. 启用连接日志排查细节
在application.properties中添加日志配置,获取更详细的连接报错信息:
logging.level.org.springframework.data.redis=DEBUG logging.level.io.lettuce.core=DEBUG
根据日志判断是否存在SSL配置缺失等问题。
5. 强制启用SSL连接(若需)
部分Redis Cloud实例默认要求SSL连接,添加以下配置:
spring.redis.ssl=true
内容的提问来源于stack exchange,提问作者Nandan Dubey
相关产品推荐
相关产品推荐

