Windows版本redis高可用方案探究 (4)

具体投票过程这里不做具体分析。
当选出新的主redis,哨兵会对redis配置进行更新,将主redis的slaveof XXX XXX配置删除,将从的slaveof XXX XXX设置为主的地址。同时哨兵会监控redis28380。当28380恢复后会将slaveof XXX XXX添加到它的配置中。

连接到28382查看

127.0.0.1:28382> info Replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=28381,state=online,offset=193307,lag=1 slave1:ip=127.0.0.1,port=28380,state=online,offset=193441,lag=1 master_repl_offset:193575 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:193574 动态新增从库

若我们此时动态新增一个从redis,端口为28383,则复制一个28380的配置进行修改,将优先级改为97。并启动服务即可。

redis-28383日志如下

[14048] 05 Jan 12:55:20.421 * Redis 3.2.100 (00000000/0) 64 bit, standalone mode, port 28383, pid 14048 ready to start. [14048] 05 Jan 12:55:20.422 # Server started, Redis version 3.2.100 [14048] 05 Jan 12:55:20.423 * The server is now ready to accept connections on port 28383 [14048] 05 Jan 12:55:20.424 * Connecting to MASTER 127.0.0.1:28382 [14048] 05 Jan 12:55:20.426 * MASTER <-> SLAVE sync started [14048] 05 Jan 12:55:20.427 * Non blocking connect for SYNC fired the event. [14048] 05 Jan 12:55:20.428 * Master replied to PING, replication can continue... [14048] 05 Jan 12:55:20.428 * Partial resynchronization not possible (no cached master) [14048] 05 Jan 12:55:20.432 * Full resync from master: 2c344529e4acdc44cd311818fe8179825877a9f4:241347 [14048] 05 Jan 12:55:20.653 * MASTER <-> SLAVE sync: receiving 107 bytes from master [14048] 05 Jan 12:55:20.655 * MASTER <-> SLAVE sync: Flushing old data [14048] 05 Jan 12:55:20.655 * MASTER <-> SLAVE sync: Loading DB in memory [14048] 05 Jan 12:55:20.656 * MASTER <-> SLAVE sync: Finished with success [14048] 05 Jan 12:55:20.660 * Background append only file rewriting started by pid 29392 [14048] 05 Jan 12:55:20.827 * AOF rewrite child asks to stop sending diffs. [14048] 05 Jan 12:55:20.927 # fork operation complete [14048] 05 Jan 12:55:20.928 * Background AOF rewrite terminated with success [14048] 05 Jan 12:55:20.929 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB) [14048] 05 Jan 12:55:20.931 * Background AOF rewrite finished successfully

再看下28382的主从连接信息

127.0.0.1:28382> info Replication # Replication role:master connected_slaves:3 slave0:ip=127.0.0.1,port=28381,state=online,offset=268634,lag=0 slave1:ip=127.0.0.1,port=28380,state=online,offset=268768,lag=0 slave2:ip=127.0.0.1,port=28383,state=online,offset=268634,lag=0 master_repl_offset:268768 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:268767

哨兵也能检测到新的从库连接
[22332] 05 Jan 12:55:25.420 * +slave slave 127.0.0.1:28383 127.0.0.1 28383 @ master 127.0.0.1 28382

通过上述配置,就完成了redis高可用方案。

程序连接redis高可用

我使用的是StackExchange.Redis连接redis。

它本身就支持主从连接,在建立连接的时候输入多个连接地址接口。由于从库不允许写入。因此它能辨别出哪个是主哪个是从。主从切换后写入数据的时候重新判定哪个是主库。

通过代码设置redis地址

ConfigurationOptions options = ConfigurationOptions.Parse("password=test1"); options.EndPoints.Add("127.0.0.1", 28380); options.EndPoints.Add("127.0.0.1", 28381); options.EndPoints.Add("127.0.0.1", 28382);

通过配置字符串设置

ConfigurationOptions options = ConfigurationOptions.Parse("127.0.0.1:28380,127.0.0.1:28381,127.0.0.1:28382,keepAlive=5,password=test1"); 总结

通过该篇文章详细的探究了window下的redis高可用方案如何实现。本文对具体配置没有做深入探究,仅仅为了抓住重点,具体配置其他的文档都介绍的比较详细,但是一些细节并没有说明,通过该片文章将reids高可用的坑都填满。若有错误,欢迎指正。

参考文档

redis sentinel配置(windows环境)

Redis 复制、Sentinel的搭建和原理说明

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzwjzw.html