首页新闻找找看学习计划

ServiceStack.Redis连接阿里云redis服务的问题

0
悬赏园豆:30 [已解决问题] 解决于 2017-01-09 11:36

下面的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:密码 

dudu的主页 dudu | 高人七级 | 园豆:41257
提问于:2017-01-07 14:33
< >
分享
最佳答案
0

问题发生在 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;
dudu | 高人七级 |园豆:41257 | 2017-01-09 11:18
其他回答(4)
0

RedisConfig.VerifyMasterConnections = false;
// 支持读写分离,均衡负载
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 20, // “写”链接池链接数
MaxReadPoolSize = 50, // “读”链接池链接数
AutoStart = true,
});

收获园豆:30
嘿嘿果宝 | 园豆:32 (初学一级) | 2017-01-07 16:12
0

入坑。

这样改了,对于Redis是使用集群还是主备有影响吗?我使用的是PooledRedisClientManager链接Redis(ServiceStack.Redis.4.0.62)

黄蛤蟆 | 园豆:206 (菜鸟二级) | 2017-02-07 18:04
0

问题解决,多谢

Howard Lee | 园豆:202 (菜鸟二级) | 2017-09-26 14:38
0

我也碰到个这个情况,用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;
}

易寒321 | 园豆:204 (菜鸟二级) | 2018-04-26 09:56

我也遇到这个问题,一直是{"NOAUTH Authentication required."}这个错,
RedisConfig.VerifyMasterConnections = false;个怎么定义的啊??

支持(0) 反对(0) hslgmq | 园豆:45 (初学一级) | 2019-04-09 22:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册