Dotnet
连接哨兵模式
ConfigurationOptions sentinelConfig = new ConfigurationOptions
{
ServiceName = "mymaster",
Password = "mypassword",
DefaultDatabase = 2,
AllowAdmin = true,
AbortOnConnectFail = true,
TieBreaker = "",//这行在sentinel模式必须加上
};
sentinelConfig.EndPoints.Add("192.168.0.123", 26380);
sentinelConfig.EndPoints.Add("192.168.0.123", 26381);
sentinelConfig.EndPoints.Add("192.168.0.123", 26382);
//sentinelConfig.CommandMap = CommandMap.Sentinel;
//哨兵模式, EXCLUDE a few commands
sentinelConfig.CommandMap = StackExchange.Redis.CommandMap.Create(new HashSet<string>
{
//"INFO",
"CONFIG"
, "CLUSTER"
, "PING"
, "ECHO"
, "CLIENT"
}, available: false);
- 注意上面的CommandMap 的 INFO,如果进行移除,当master宕机后,识别不出主从的问题,导致写入的时候,出现写从服务器readonly的错误
- 如果使用 sentinelConfig.CommandMap = CommandMap.Sentinel,则会出现无法读写的问题
上面的配置可以使用连接字符串
public static string ConfigurationForSentinel(int defaultDatabase = 2)
{
return $"192.168.0.123:26380,192.168.0.123:26381,192.168.0.123:26382,password=mypassword,DefaultDatabase={defaultDatabase},serviceName=mymaster,allowAdmin=true,tiebreaker=,abortConnect=false";
}
这个在使用了StackExchange.Redis的组件均可使用,如
- RedLock.net
- Senparc.Weixin.Cache.Redis