我正在尝试使用sentinel来获取我的主服务器的连接地址,问题是Sentinel仅在故障转移时发送地址,但是如果我的主服务器已关闭且从服务器被提升为master并且我的应用程序刚启动它不知道也不会得到原始主人失败的消息,有没有办法与哨兵沟通,并问他认为主人谁在使用C#servicestack redis客户端?
答案 0 :(得分:1)
不得不这么做,我使用下一个代码片段模仿redis-cli命令:(剩下的就是解析响应中的结果)
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);
}
答案 1 :(得分:0)
我已经编写了一些测试代码,用于通过Sentinel的IP和端口获取Master的IP和端口---。 此代码使用StackExchange.Redis中的NuGet包
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test
{
public class Sentinel
{
private ConnectionMultiplexer Conn { get; }
private IServer Server { get; }
protected StringWriter ConnectionLog { get; }
public Sentinel()
{
var options = new ConfigurationOptions()
{
CommandMap = CommandMap.Sentinel,
EndPoints = { { "192.168.1.64", 26379 } }, //IP and Port of Sentinel
AllowAdmin = true,
TieBreaker = "",
SyncTimeout = 5000
};
Conn = ConnectionMultiplexer.Connect(options, ConnectionLog);
Server = Conn.GetServer("192.168.1.64", 26379); //IP and Port of Sentinel
}
public void SentinelGetMasterAddressByNameTest(string nameOfMaster)
{
var endpoint = Server.SentinelGetMasterAddressByName(nameOfMaster);
var ipEndPoint = endpoint as IPEndPoint;
Console.WriteLine("The Master's <IP:Port>: {0}:{1}", ipEndPoint.Address, ipEndPoint.Port);
}
}
class Program
{
static void Main(string[] args)
{
var sentinel = new Sentinel();
sentinel.SentinelGetMasterAddressByNameTest("redis-test"); //Passing name of the master
Console.ReadLine();
}
}
}