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

Spring Data Redis是否支持为ACL功能传递Sentinel用户名?

问题背景

Redis采用Sentinel部署的HA模式运行,Redis与Sentinel实例均配置了各自的ACL用户且已关闭默认用户。

Spring Data Redis 2.5.4 Sentinel模式配置支持情况
  • 已支持传入的配置项:
    • redis username
    • redis password
    • redis sentinel password
  • 暂不支持传入的配置项:
    • sentinel username
问题根因

问题出在JedisConnectionFactory类的createSentinelClientConfig方法实现,对应代码如下:

private JedisClientConfig createSentinelClientConfig(SentinelConfiguration sentinelConfiguration){
    return createClientConfig(0,null,sentinelConfiguration.getSentinelPassword());
}

上述代码直接为Sentinel用户名传入了null值,且没有对应的setter方法可用于填充Sentinel用户名,最终导致无法连接要求提供Sentinel用户名才能建立连接的Sentinel模式Redis服务。

解决方案

方案1:版本升级

将Spring Data Redis升级到2.7.0及以上版本,该版本已经原生支持Sentinel用户名配置,直接在SentinelConfiguration实例中调用setSentinelUsername方法传入对应ACL用户名即可正常连接。

方案2:低版本自定义扩展

如果受项目约束无法升级版本,可以通过继承JedisConnectionFactory重写createSentinelClientConfig方法的方式自定义注入Sentinel用户名,参考实现如下:

public class CustomJedisConnectionFactory extends JedisConnectionFactory {
    private String sentinelUsername;

    public CustomJedisConnectionFactory(SentinelConfiguration sentinelConfig, String sentinelUsername) {
        super(sentinelConfig);
        this.sentinelUsername = sentinelUsername;
    }

    @Override
    protected JedisClientConfig createSentinelClientConfig(SentinelConfiguration sentinelConfiguration) {
        // 传入自定义的sentinel用户名替代原逻辑的null
        return createClientConfig(0, sentinelUsername, sentinelConfiguration.getSentinelPassword());
    }
}

在项目配置中将默认的JedisConnectionFactory替换为上述自定义实现类即可。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 07:21:01