一、前戲
下面的列表清楚的解釋了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服務器掛了,負載均衡的策略會不會自動的修改,這些問題以后會慢慢探討,先把問題提出來。