首页 存档 技术 查看内容

C#Redis 主从复制

2018-3-30 13:00 |来自: 互联网 362 0

摘要: (点击上方蓝字,可快速关注我们) 来源:社会主义**人 cnblogs.com/5ishare/p/6442920.html 一、前言 下面的列表清楚的解释了Redis Replication的特点和优势。 1). 同一个Master可以同步多个Slaves。 2). Sl ...

(点击上方蓝字,可快速关注我们)


来源:社会主义**人

cnblogs.com/5ishare/p/6442920.html


一、前言


下面的列表清楚的解释了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

声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

相关分类

返回顶部