通過持久化功能,Redis 保證了即使在服務器重啟的情況下也不會丟失(或少量丟失)數(shù)據(jù),但是由于數(shù)據(jù)是存儲在一臺服務器上的,如果這臺服務器出現(xiàn)故障,比如硬盤壞了, 也會導致數(shù)據(jù)丟失。
為了避免單點故障,我們需要將數(shù)據(jù)復制多份部署在多臺不同的服務器上,即使有一臺服務器出現(xiàn)故障其他服務器依然可以繼續(xù)提供服務。
這就要求當一臺服務器上的數(shù)據(jù)更新后,自動將更新的數(shù)據(jù)同步到其他服務器上,那該怎么實現(xiàn)呢? Redis 的主從復制。
Redis 提供了復制(replication)功能來自動實現(xiàn)多臺 redis 服務器的數(shù)據(jù)同步(每天19 點 新聞聯(lián)播,基本從 cctv1-8,各大衛(wèi)視都會播放)
我們可以通過部署多臺 redis,并在配置文件中指定這幾臺 redis 之間的主從關系,主負責寫入數(shù)據(jù), 同時把寫入的數(shù)據(jù)實時同步到從機器, 這種模式叫做主從復制, 即master/slave,并且 redis 默認 master 用于寫,slave 用于讀,向 slave 寫數(shù)據(jù)會導致錯誤
方式 1:修改配置文件,啟動時,服務器讀取配置文件,并自動成為指定服務器的從服務器,從而構成主從復制的關系
方式 2: ./redis-server --slaveof <master-ip> <master-port>,在啟動 redis 時指定當前服務成為某個主 Redis 服務的從 Slave
方式 1 的實現(xiàn)步驟:
模擬多 Reids 服務器, 在一臺已經(jīng)安裝 Redis 的機器上,運行多個 Redis 應用模擬多個 Reids 服務器。一個 Master,兩個 Slave.
A、新建三個 Redis 的配置文件
如果 Redis 啟動,先停止。
作為 Master 的 Redis 端口是 6380
作為 Slaver 的 Redis 端口分別是 6382 , 6384
從原有的 redis.conf 拷貝三份,分別命名為 redis6380.conf, redis6382.conf , redis6384.conf
B、 編輯 Master 配置文件
編輯 Master 的配置文件 redis6380.conf : 在空文件加入如下內(nèi)容
include /usr/local/redis-3.2.9/redis.conf
daemonize yes port 6380
pidfile /var/run/redis_6380.pid logfile 6380.log
dbfilename dump6380.rdb
配置項說明:
include : 包含原來的配置文件內(nèi)容。/usr/local/redis-3.2.9/redis.conf 按照自己的目錄設置。
daemonize:yes 后臺啟動應用,相當于 ./redis-server & , &的作用。
port : 自定義的端口號
pidfile : 自定義的文件,表示當前程序的 pid ,進程 id。
logfile:日志文件名
dbfilename:持久化的 rdb 文件名
C、 編輯 Slave 配置文件
編輯 Slave 的配置文件 redis6382.conf 和 redis6384.conf: 在空文件加入如下內(nèi)容
①:redis6382.conf:
include /usr/local/redis-3.2.9/redis.conf
daemonize yes
port 6382
pidfile /var/run/redis_6382.pid logfile 6382.log
dbfilename dump6382.rdb slaveof 127.0.0.1 6380
配置項說明:
slaveof : 表示當前 Redis 是誰的從。當前是 127.0.0.0 端口 6380 這個 Master 的從。
②:redis6384.conf:
include /usr/local/redis-3.2.9/redis.conf daemonize yes
port 6384
pidfile /var/run/redis_6384.pid logfile 6384.log
dbfilename dump6384.rdb
slaveof 127.0.0.1 6380
D、啟動服務器 Master/Slave 都啟動
啟動方式 ./redis-server 配置文件
啟動 Redis,并查看啟動進程
E、 查看配置后的服務信息
命令:
①: Redis 客戶端使用指定端口連接 Redis 服務器
./redis-cli -p 端口
②:查看服務器信息
info replication
登錄到 Master:6380
查看當前服務信息
在客戶端的 Redis 內(nèi)執(zhí)行命令 info replication
Master 服務的查看結(jié)果:
在新的 Xshell 窗口分別登錄到 6382 ,6384 查看信息
6384 也登錄內(nèi)容同 6382
F、 向 Master 寫入數(shù)據(jù)
在 6380 執(zhí)行 flushall 清除數(shù)據(jù),避免干擾的測試數(shù)據(jù)。 生產(chǎn)環(huán)境避免使用。
G、在從 Slave 讀數(shù)據(jù)
6382,6384 都可以讀主 Master 的數(shù)據(jù),不能寫
Slave 寫數(shù)據(jù)失敗
master 上(冷處理:機器掛掉了,再處理)當 Master 服務出現(xiàn)故障,需手動將 slave 中的一個提升為 master, 剩下的 slave 掛至新的
命令:
①:slaveof no one,將一臺 slave 服務器提升為 Master (提升某 slave 為 master)
②:slaveof 127.0.0.1 6381 (將 slave 掛至新的 master 上)
執(zhí)行步驟:
A、將 Master:6380 停止(模擬掛掉)
B、 選擇一個 Slave 升到 Master,其它的 Slave 掛到新提升的 Master
C、 將其他 Slave 掛到新的 Master
在 Slave 6384 上執(zhí)行
現(xiàn)在的主從(Master/Slave)關系:Master 是 6382 , Slave 是 6384
查看 6382:
D、原來的服務器重新添加到主從結(jié)構中
6380 的服務器修改后,從新工作,需要把它添加到現(xiàn)有的Master/Slave 中
先啟動 6380 的 Redis 服務
連接到 6380 端口
當前服務掛到 Master 上
E、 查看新的 Master 信息
在 6382 執(zhí)行:
現(xiàn)在的 Master/Slaver 關系是:
Master: 6382
Slave: 6380
6384
進入客戶端需指定端口:./redis-cli -p 6380
不配置啟動默認都是主 master
info replication 查看 redis 服務器所處角色
A、一個 master 可以有多個 slave
B、slave 下線,讀請求的處理性能下降
C、master 下線,寫請求無法執(zhí)行
D、當 master 發(fā)生故障,需手動將其中一臺 slave 使用 slaveof no one 命令提升為 master,其它 slave 執(zhí)行 slaveof 命令指向這個新的master,從新的master處同步數(shù)據(jù)。
E、主從復制模式的故障轉(zhuǎn)移需要手動操作,要實現(xiàn)自動化處理,這就需要 Sentinel 哨兵,實現(xiàn)故障自動轉(zhuǎn)移。