C#Redis 主从复制

一、前戏

 下面的列表清楚的解释了Redis Replication的特点和优势。
    1). 同一个Master可以同步多个Slaves。
    2). Slave同样可以接受其它Slaves的连接和同步请求,这样可以有效的分载Master的同步压力。因此我们可以将Redis的Replication架构视为图结构。
    3). Master Server是以非阻塞的方式为Slaves提供服务。所以在Master-Slave同步期间,客户端仍然可以提交查询或修改请求。
    4). Slave Server同样是以非阻塞的方式完成数据同步。在同步期间,如果有客户端提交查询请求,Redis则返回同步之前的数据。
    5). 为了分载Master的读操作压力,Slave服务器可以为客户端提供只读操作的服务,写服务仍然必须由Master来完成。即便如此,系统的伸缩性还是得到了很大的提高。
    6). Master可以将数据保存操作交给Slaves完成,从而避免了在Master中要有独立的进程来完成此操作。

二、理论

 在Slave启动并连接到Master之后,它将主动发送一个SYNC命令。此后Master将启动后台存盘进程,同时收集所有接收到的用于修改数据集的命令,在后台进程执行完毕后,Master将传送整个数据库文件到Slave,以完成一次完全同步。而Slave服务器在接收到数据库文件数据之后将其存盘并加载到内存中。此后,Master继续将所有已经收集到的修改命令,和新的修改命令依次传送给Slaves,Slave将在本次执行这些数据修改命令,从而达到最终的数据同步。
    如果Master和Slave之间的链接出现断连现象,Slave可以自动重连Master,但是在连接成功之后,一次完全同步将被自动执行。

三、实战

上面基本把理论讲了一下,下面就是实验。关于主从复制这个我也搞了好几天,为什么呢?就是无法在一台window实例化多个redis服务。自己在网上也找了好多,也没找到办法。以往周末都是会打麻将的,今天正好没打,就在思考这个问题。前面下载的是3.0版的redis,文件结构如下图,我一直使用redis.windows-service.conf这个配置文件来启动,今天想着用redis.windows.conf来配置一下,因为看其他博客的都是使用redis.conf,所以我也把文件名也改了一下,这一改没想到真是见证奇迹了,成功了。

这里采用一主一从,端口6379的是Master主,6380的是从。

1.先修改redis.windows.conf文件名改为redis.conf,默认就是6379.

2.复制redis.windows.conf文件并改名为redis6380.conf,同时配置文件里面的端口号为6380

# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6380

3.为redis6380的绑定主服务

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition slaves automatically try to reconnect to masters
#    and resynchronize with them.
#
# slaveof <masterip> <masterport>
slaveof 127.0.0.1 6379

4.设置从redis可写因为从2.6版本默认是只读

# Since Redis 2.6 by default slaves are read-only.
#
# Note: read only slaves are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only slave exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
slave-read-only yes

5.cmd启动6379端口的服务

6.cmd启动6380端口的服务

7.cmd启动7379的客户端,并设置两个key ,mykey、mykey1

8.cmd启动6380的客户端,获取两个key的值

9.从8截图的最后一行可以看到不能删除mykey1,因为我还没设置slave-read-only yes,当设置为可读写操作的就可以对其删除了。

四、总结

到目前为止,虽然是主从复制,但还不能称的上集群,包括前面的nignx负载均衡,都是简单的场景,并没考虑复杂场景,比如redis的主服务挂了怎么办,会不会自动任命一个新的主服务,同样nginx也是,如果一个web服务器挂了,负载均衡的策略会不会自动的修改,这些问题以后会慢慢探讨,先把问题提出来。

原文地址:https://www.cnblogs.com/5ishare/p/6442920.html