string SetMaster(string name, int timeout)
        {
            for (int i = 0; i < _sentinels.Count; i++)
            {
                if (i > 0)
                {
                    Next();
                }

                using (var sentinel = Current())
                {
                    try
                    {
                        if (!sentinel.Connect(timeout))
                        {
                            continue;
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    var master = sentinel.GetMasterAddrByName(name);
                    if (master == null)
                    {
                        continue;
                    }

                    _redisClient            = new RedisClient(master.Item1, master.Item2);
                    _redisClient.Connected += OnConnectionConnected;
                    if (!_redisClient.Connect(timeout))
                    {
                        continue;
                    }

                    var role = _redisClient.Role();
                    if (role.RoleName != "master")
                    {
                        continue;
                    }

                    foreach (var remoteSentinel in sentinel.Sentinels(name))
                    {
                        Add(remoteSentinel.Ip, remoteSentinel.Port);
                    }

                    return(sentinel.Host + ':' + sentinel.Port);
                }
            }
            return(null);
        }
示例#2
0
        string SetSlave(string name, int timeout)
        {
            for (int i = 0; i < _sentinels.Count; i++)
            {
                if (i > 0)
                {
                    Next();
                }

                using (var sentinel = Current())
                {
                    try
                    {
                        if (!sentinel.Connect(timeout))
                        {
                            continue;
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    var slaves = sentinel.Slaves(name);
                    if (slaves == null)
                    {
                        continue;
                    }

                    foreach (var slave in slaves)
                    {
                        if (_redisClient != null)
                        {
                            _redisClient.Dispose();
                        }
                        _redisClient            = new RedisClient(slave.Ip, slave.Port);
                        _redisClient.Connected += OnConnectionConnected;

                        try
                        {
                            if (!_redisClient.Connect(timeout))
                            {
                                continue;
                            }

                            var role = _redisClient.Role();
                            if (role.RoleName != "slave")
                            {
                                continue;
                            }

                            foreach (var remoteSentinel in sentinel.Sentinels(name))
                            {
                                Add(remoteSentinel.Ip, remoteSentinel.Port);
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine(ex.Message);
                            Console.WriteLine(ex.Message);
                            continue;
                        }

                        return(slave.Ip + ':' + slave.Port);
                    }
                }
            }
            return(null);
        }
示例#3
0
        string SetMaster(string name, int timeout)
        {
            for (int i = 0; i < _sentinels.Count; i++)
            {
                if (i > 0)
                {
                    Next();
                }

                using (var sentinel = Current())
                {
                    try
                    {
                        if (!sentinel.Connect(timeout))
                        {
                            continue;
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    var master = sentinel.GetMasterAddrByName(name);
                    if (master == null)
                    {
                        continue;
                    }

                    if (_redisClient != null)
                    {
                        _redisClient.Dispose();
                    }

                    if (SentinelMasterConverter != null)
                    {
                        master = SentinelMasterConverter(master);
                    }

                    _redisClient            = new RedisClient(master.Item1, master.Item2);
                    _redisClient.Connected += OnConnectionConnected;

                    try
                    {
                        if (!_redisClient.Connect(timeout))
                        {
                            continue;
                        }

                        var role = _redisClient.Role();
                        if (role.RoleName != "master")
                        {
                            continue;
                        }

                        //测试 write
                        var testid = Guid.NewGuid().ToString("N");
                        _redisClient.StartPipe();
                        _redisClient.Set(testid, 1);
                        _redisClient.Del(testid);
                        _redisClient.EndPipe();

                        foreach (var remoteSentinel in sentinel.Sentinels(name))
                        {
                            Add(remoteSentinel.Ip, remoteSentinel.Port);
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.Message);
                        Console.WriteLine(ex.Message);
                        continue;
                    }

                    return(master.Item1 + ':' + master.Item2);
                }
            }
            return(null);
        }