从Sentinel C#获取Redis主地址 [英] Getting Redis Master address from Sentinel C#

查看:263
本文介绍了从Sentinel C#获取Redis主地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用哨兵来获取我的主服务器的连接地址,问题是哨兵仅在故障转移时发送该地址,但是如果我的主服务器关闭并且从属服务器被提升为主服务器并且我的应用程序刚刚启动它不会知道也不会收到有关原始主服务器已关闭的消息,是否有任何方法可以与哨兵进行通信,并询问他认为主服务器正在使用C#servicestack redis客户端的人?

I am trying to use the sentinel to get the connection address of my master, the problem is that the sentinel sends the address only on failover, but if my master is down and the slave was promoted master and my application is just booted it would not know and won't get the message that the original master is down, is there any way to communicate with the sentinel and ask him who he thinks the master is using C# servicestack redis client?

推荐答案

很难做到这一点,我使用下一个代码段模仿redis-cli命令:(剩下的就是解析响应结果)

Had to do it the hard way, I imitate the redis-cli command using the next code snippet: (all is left is to parse the result from the response)

   public string GetMasterFromSentinel(string sentinelAddress)
    {
        TcpClient server;

        try
        {
            var splittedAddress = sentinelAddress.Split(':');
            server = new TcpClient(splittedAddress[0], splittedAddress[1].ParseInt());
        }
        catch (SocketException)
        {
            _log.Error("Unable to connect to server");
            return string.Empty;
        }
        NetworkStream ns = server.GetStream();
        var payload = new byte[] { 0x2a, 0x32, 0x0d, 0x0a, 0x24, 0x38, 0x0d, 0x0a, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x6e, 0x65, 0x6c, 
                0x0d, 0x0a, 0x24, 0x37, 0x0d, 0x0a, 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x73, 0x0d, 0x0a };
        ns.Write(payload, 0, payload.Length);
        ns.Flush();
        var data = new byte[1024];
        ns.Read(data, 0, data.Length);
        var recv = ns.Read(data, 0, data.Length);

        ns.Close();
        server.Close();
        return ParseResponse(data);
    }

这篇关于从Sentinel C#获取Redis主地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆