下面的C#代码可以正常连接:
_cache = new RedisClient(_options.Host, _options.Port, _options.Password);
下面的C#代码连接时
var host = $"{_options.Password}@{_options.Host}:{_options.Port}"; var manager = new RedisManagerPool(host); var client = manager.GetClient();
报错:
ServiceStack.Redis.RedisResponseException: command role not support for normal user at ServiceStack.Redis.RedisNativeClient.ReadComplexResponse() in /opt/lib/teamcity-agent/work/8dcfdcec54d0f21a/src/ServiceStack.Redis/RedisNativeClient_Utils.cs:line 1031
_options.Host 的值是 实例ID.redis.rds.aliyuncs.com
_options.Password 的值是 实例ID:密码
问题发生在 ServiceStack.Redis 的 RedisResolver 中下面的代码:
public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master) { var client = ClientFactory(config); if (master && RedisConfig.VerifyMasterConnections) { var role = client.GetServerRole(); //.. } return client; }
而阿里云的 redis 服务目前不支持 redis 的 role 命令。
解决方法是将 RedisConfig.VerifyMasterConnections 的值设置为 false 。
RedisConfig.VerifyMasterConnections = false;
RedisConfig.VerifyMasterConnections = false;
// 支持读写分离,均衡负载
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 20, // “写”链接池链接数
MaxReadPoolSize = 50, // “读”链接池链接数
AutoStart = true,
});
入坑。
这样改了,对于Redis是使用集群还是主备有影响吗?我使用的是PooledRedisClientManager链接Redis(ServiceStack.Redis.4.0.62)
问题解决,多谢
我也碰到个这个情况,用ServiceStack访问Redis连接池无权限的问题,报异常NOAUTH Authentication required.正如楼上所说,在 _pooledRedisClientManager.GetClient();的前面加上一句RedisConfig.VerifyMasterConnections = false;就可以了。
把基础代贴出来:
/// <summary>
/// 创建链接池管理对象
/// </summary>
private static void CreateManager()
{
if (_pooledRedisClientManager == null)
{
lock (lockObject)
{
if (_pooledRedisClientManager == null)
{
string[] ReadWriteHosts = SplitString(_redisConfig.ReadWriteHosts, ",");
string[] ReadOnlyHosts = SplitString(_redisConfig.ReadOnlyHosts, ",");
RedisClientManagerConfig managerConfig = new RedisClientManagerConfig
{
MaxWritePoolSize = _redisConfig.MaxWritePoolSize,
MaxReadPoolSize = _redisConfig.MaxReadPoolSize,
AutoStart = _redisConfig.AutoStart
};
RedisConfig.VerifyMasterConnections = false; //加上这一句,否则调用 _pooledRedisClientManager.GetClient()会报错
_pooledRedisClientManager = new PooledRedisClientManager(ReadWriteHosts, ReadOnlyHosts, managerConfig);
// _pooledRedisClientManager = new PooledRedisClientManager(ReadWriteHosts);
}
}
}
}
private static string[] SplitString(string strSource, string split)
{
return strSource.Split(split.ToArray());
}
/// <summary>
/// 客户端缓存操作对象
/// </summary>
/// <param name="databaseIndex">数据库索引</param>
/// <returns></returns>
public static IRedisClient GetClient(int databaseIndex = 0)
{
IRedisClient redisClient;
if (_pooledRedisClientManager == null)
{
CreateManager();
}
redisClient = _pooledRedisClientManager.GetClient();
//redisClient = _pooledRedisClientManager.GetReadOnlyClient();
if (!string.IsNullOrEmpty(_redisConfig.Password))
{
redisClient.Password = _redisConfig.Password;
}
redisClient.Db = databaseIndex;
return redisClient;
}
我也遇到这个问题,一直是{"NOAUTH Authentication required."}这个错,
RedisConfig.VerifyMasterConnections = false;个怎么定义的啊??