You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Webflux连接Azure Redis Cache失败问题求助

问题原因及解决步骤

1. 配置文件空格导致参数失效(核心问题)

你的配置项中等号后存在空格,比如spring.redis.host= somename.redis.cache.windows.net,Spring Boot读取配置时会将空格包含在参数值内,导致实际使用的host是带前缀空格的无效地址,最终应用自动 fallback 到默认的localhost,引发连接拒绝错误。

修复方法:移除所有配置项等号后的空格:

# Specify the DNS URI of your Redis cache.
spring.redis.host=somename.redis.cache.windows.net
# Specify the port for your Redis cache.
spring.redis.port=6380
# Specify the access key for your Redis cache.
spring.redis.password=something
spring.redis.ssl=true

2. Azure Redis的端口与SSL强制要求

Azure Redis默认禁用非SSL端口(6379),仅开放SSL加密端口(6380),且强制要求SSL连接。你之前配置的spring.redis.ssl=false和port=6379不符合Azure Redis的默认规则,即使配置生效也无法建立连接。

修复方法:将端口改为6380,开启SSL(如上配置所示)。

3. 适配Webflux的非阻塞Redis模板

你使用的StringRedisTemplate是阻塞式实现,与Webflux的非阻塞模型冲突,建议替换为响应式的ReactiveStringRedisTemplate,修改Controller代码如下:

@Autowired
private ReactiveStringRedisTemplate reactiveTemplate;

@GetMapping("/hello")
public Mono<String> hello() {
    String key = "greeting";
    // 先尝试从缓存读取,不存在则写入并返回默认值
    return reactiveTemplate.opsForValue().get(key)
            .switchIfEmpty(
                reactiveTemplate.opsForValue().set(key, "Hello World!")
                    .then(Mono.just("Hello World!"))
            );
}

验证配置是否生效

启动应用后,查看日志中是否输出类似内容,确认Redis连接参数已正确加载:

ReactiveRedisConnectionFactory using default settings: host='somename.redis.cache.windows.net', port=6380

内容的提问来源于stack exchange,提问作者user1354825

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 16:37:24