gpt4 book ai didi

c# - 从 Sentinel C# 获取 Redis Master 地址

转载 作者:IT王子 更新时间:2023-10-29 06:06:43 25 4
gpt4 key购买 nike

我正在尝试使用 sentinel 来获取我的 master 的连接地址,问题是 sentinel 仅在故障转移时发送地址,但是如果我的 master 已关闭并且 slave 被提升为 master 并且我的应用程序刚刚启动它不会知道也不会收到原来的master宕机的消息,有没有办法和sentinel沟通,问他认为master是谁用的C# servicestack redis client?

最佳答案

我已经编写了一些测试代码来获取主机的 IP 和端口 --- 通过 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();
}
}
}

关于c# - 从 Sentinel C# 获取 Redis Master 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com