ServiceStack.Redis连接阿里云redis服务的问题
下面的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;
其他回答(3)
RedisConfig.VerifyMasterConnections = false;
// 支持读写分离,均衡负载
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 20, // “写”链接池链接数
MaxReadPoolSize = 50, // “读”链接池链接数
AutoStart = true,
});
入坑。
这样改了,对于Redis是使用集群还是主备有影响吗?我使用的是PooledRedisClientManager链接Redis(ServiceStack.Redis.4.0.62)
问题解决,多谢
*本文参考https://q.cnblogs.com/q/89946/,仅用于学习。