【发布时间】:2016-04-21 04:38:12
【问题描述】:
我有一个主从配置,有两个奴隶和三个哨兵。如果我尝试使用命令行的命令一切正常。 但是我在为 C# 配置它时遇到了一些问题。 我正在使用 StackExchange.Redis 库。但我不明白我应该怎么做才能从主人那里获取/设置钥匙。我应该手动找到主人然后获取或设置密钥还是自动完成?最好的方法是什么?每次我想获取/设置密钥时,我应该检查谁是主人? 现在我只做了这个,我留下了我的另一个代码,因为它只有一个主人。
private static string ServiceName = "mymaster";
private static string IP { get { return "127.0.0.1"; } }
private static int Port = 26379;
public static readonly ConnectionMultiplexer Connection = GetConn();
public static readonly IServer Server = Connection.GetServer(IP, Port);
public static ConnectionMultiplexer GetConn()
{
try
{
// create a connection
var options = new ConfigurationOptions()
{
CommandMap = CommandMap.Sentinel,
EndPoints = { { IP, Port } },
AllowAdmin = true,
TieBreaker = "",
ServiceName = ServiceName,
SyncTimeout = 5000,
AbortOnConnectFail = true,
Ssl = false
};
var connection = ConnectionMultiplexer.Connect(options, Console.Out);
return connection;
}
catch (Exception ex)
{
ASLog.LogError(ex, 1);
return null;
}
}
我在 26379 端口有三个哨兵之一。 抱歉,我有点困惑如何在 C# 中使用它。
【问题讨论】:
-
仅仅四年,我们就可以开箱即用stackoverflow.com/a/64177201/1426685
标签: c# stackexchange.redis redis-sentinel