如何使用StackExchange.Redis连接到哨兵集群?

时间:2018-11-27 11:24:49

标签: c# redis stackexchange.redis sentinel

Stackexchange.Redis (C#)是否支持连接到最新版本的哨兵集群以实现高可用性?我发现redis的这一重要功能没有得到适当的记录,或者根本没有任何示例,这有点奇怪。不胜感激。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您可以通过检查StackExchange.Redis的测试来查看如何连接到哨兵。具体来说,这一个https://github.com/StackExchange/StackExchange.Redis/blob/master/tests/StackExchange.Redis.Tests/Sentinel.cs

基本上,您需要查看的代码在这里:

   public Sentinel(ITestOutputHelper output) : base(output)
    {
        ConnectionLog = new StringWriter();

        Skip.IfNoConfig(nameof(TestConfig.Config.SentinelServer), TestConfig.Current.SentinelServer);
        Skip.IfNoConfig(nameof(TestConfig.Config.SentinelSeviceName), TestConfig.Current.SentinelSeviceName);

        var options = new ConfigurationOptions()
        {
            CommandMap = CommandMap.Sentinel,
            EndPoints = { { TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort } },
            AllowAdmin = true,
            TieBreaker = "",
            ServiceName = TestConfig.Current.SentinelSeviceName,
            SyncTimeout = 5000
        };
        Conn = ConnectionMultiplexer.Connect(options, ConnectionLog);
        Thread.Sleep(3000);
        Assert.True(Conn.IsConnected);
        Server = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort);
    }

然后在这里:

   [Fact]
    public void SentinelGetMasterAddressByNameTest()
    {
        var endpoint = Server.SentinelGetMasterAddressByName(ServiceName);
        Assert.NotNull(endpoint);
        var ipEndPoint = endpoint as IPEndPoint;
        Assert.NotNull(ipEndPoint);
        Log("{0}:{1}", ipEndPoint.Address, ipEndPoint.Port);
    }