C#Redis 主从复制

1、前戏nginx

 下面的列表清楚的解释了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中要有独立的进程来完成此操做。web

2、理论redis

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

3、实战windows

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

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

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

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的绑定主服务socket

# 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,当设置为可读写操做的就能够对其删除了。

4、总结

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

相关文章
相关标签/搜索