CentOS7 配置 Redis Sentinel主从集群配置

Redis Sentinel主从集群

  1. 环境、准备
  2. slave配置
  3. sentinel配置
  4. 测试
  5. C#连接Redis Sentinel

1、环境、准备

单实例3台CentOS7服务器,IP地址、:

192.168.31.167  #M1 S1
192.168.31.168  #R2 S2
192.168.31.169  #R3 S3
#部署结构示意
       +----+
       | M1 |
       | S1 |
       +----+
          |
+----+    |    +----+
| R2 |----+----| R3 |
| S2 |         | S3 |
+----+         +----+

Configuration: quorum = 2

2、slave配置

将168、169机器两个实例/usr/local/redis/etc/6379.conf文件配置为167的从。

slaveof 192.168.31.167 6379

配置后重启3台服务器上的Redis。

3、sentinel配置

将/usr/local/src/redis/sentinel.conf复制到/usr/local/redis/etc/sentinel.conf修改如下配置:

protected-mode no
daemonize yes
logfile "/usr/local/redis/logs/sentinel.log"
sentinel monitor mymaster 192.168.31.167 6379 2
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000

配置完后启动3台服务器上的redis-sentinel

redis-sentinel /usr/local/redis/etc/sentinel.conf

在167查看主从信息,从也可通过info方式查看。

redis-cli -h 192.168.31.167
192.168.31.167:6379> info

# Replication
role:master
connected_slaves:2
slave0:ip=192.168.31.168,port=6379,state=online,offset=6381,lag=0
slave1:ip=192.168.31.169,port=6379,state=online,offset=6524,lag=0
master_repl_offset:6524
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:6523

测试

在167 Master写入。

192.168.31.167:6379> set name ddrsql
OK
192.168.31.167:6379> keys *
1) "name"
192.168.31.167:6379> get name
"ddrsql"

在168 Slave中查看结果如下,169 Slave和168一样。

192.168.31.168:6379> keys *
1) "name"
192.168.31.168:6379> get name
"ddrsql"

将167 Master停止,也可直接将167关机。

192.168.31.167:6379> shutdown
not connected> 

在168、169中查看,168提升为主。

192.168.31.168:6379> info replication

# Replication
role:master
connected_slaves:1
slave0:ip=192.168.31.169,port=6379,state=online,offset=503694,lag=0
master_repl_offset:503837
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:503836

192.168.31.169:6379> info replication

# Replication
role:slave
master_host:192.168.31.168
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:520680
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

C#连接Redis Sentinel

使用StackExchange.Redis
参考:
https://github.com/StackExchange/StackExchange.Redis/blob/master/StackExchange.Redis.Tests/Sentinel.cs

private const string IP = "192.168.31.167";
private const int Port = 26379;
private const string ServiceName = "mymaster";

private static readonly ConnectionMultiplexer Conn = GetConn();
//实际使用需要telnet验证通过的IP:Port地址来GetServer
private static readonly IServer Server = Conn.GetServer(IP, Port);

public static ConnectionMultiplexer GetConn()
{
    // create a connection
    var options = new ConfigurationOptions()
    {
        CommandMap = CommandMap.Sentinel,
        EndPoints = { { IP, Port } },
        AllowAdmin = true,
        TieBreaker = "",
        ServiceName = ServiceName,
        SyncTimeout = 5000
    };
    //配置了多个哨兵
    options.EndPoints.Add("192.168.31.169", Port);
    options.EndPoints.Add("192.168.31.169", Port);
    var connection = ConnectionMultiplexer.Connect(options, Console.Out);
    Thread.Sleep(3000);
    Assert.IsTrue(connection.IsConnected);
    return connection;
}

转载于:https://www.cnblogs.com/ddrsql/p/6686282.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值