ServiceStack.Redis身份验证Redis Sentinel+Redis



问题:

如何正确使用进行身份验证并不明显

  • 哨兵
  • redis实例本身

使用ServiceStack.Redis解决方案时。

根据文档,为redis/ssentinel提供密码的正确方法是通过URL以以下方式设置密码:

var sentinel = new RedisSentinel(new[] {"password@localhost:26381"})

这很好,但我如何为redis连接本身提供密码?我在URL中指定的密码不会被复制到redis连接中。

我找到了一个变通方法,似乎有效,但我不确定它是否真的能涵盖所有可能的情况:

//Get the original factory
var clientFactory = RedisConfig.ClientFactory;
//Decorate it with the logic that sets a password. 
RedisConfig.ClientFactory = c =>
{
c.Password = "password";
return clientFactory.Invoke(c);
};
//Continue to initialize sentinels
var sentinel = new RedisSentinel(new[] {"password@localhost:26379"});

看起来这实际上会解决问题,并为redis实例(而不是哨兵(的连接提供密码。

问题是:这是推荐的方法吗?或者有更好的方法吗?

您可以使用RedisStinel HostFilter自定义sentinel连接到的各个主机的连接字符串,例如:

sentinel.HostFilter = host => $"password@{host}";

最新更新