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