溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Redis 的四種模式

發(fā)布時間:2020-08-03 09:27:40 來源:網(wǎng)絡(luò) 閱讀:556 作者:xinsir999 欄目:系統(tǒng)運(yùn)維

主從模式

redis主從模式,顧名思義。就是一主一從或一主多從。配置很簡單,只需要在從的配置文件中寫一個參數(shù)即可。

slaveof?<masterip>?<masterport>
#?例如
#?slaveof?192.168.1.197?6379

優(yōu)點(diǎn):

????配置簡單,使用靈活,數(shù)據(jù)安全性較高。

缺點(diǎn):

????無法實(shí)現(xiàn)故障切換,當(dāng)master down后,slave無法自動切換為主接替后續(xù)任務(wù)。

哨兵模式

?哨兵模式部署步驟也很簡單,redis.conf的配置文件就不在闡述,跟普通redis配置文件一樣,cp一份就行。

//?vim?sentinel.conf

#?后臺運(yùn)行
protected-mode?yes??
#?綁定的服務(wù)器ip,如果不寫ip默認(rèn)為127.0.0.1,只能本地連接
bind?10.10.10.137
#?后臺運(yùn)行
daemonize?yes
#?開啟端口,如果主從都在同一臺機(jī)器,記得端口要改一下,別沖突
port?26380
#?需要監(jiān)控的主的ip地址和端口,后面的1表示有一個哨兵發(fā)現(xiàn)master?down后,就切換slave為?master
sentinel?monitor?mymaster?127.0.0.1?6379?1
#?sentinel會向master發(fā)送心跳PING來確認(rèn)master是否存活,如果master在“一定時間范圍”內(nèi)不回應(yīng)PING?或者是回復(fù)了一個錯誤消息,
#?那么這個sentinel會主觀地(單方面地)認(rèn)為這個master已經(jīng)不可用了(subjectively?down,?也簡稱為SDOWN)。
#?而這個down-after-milliseconds就是用來指定這個“一定時間范圍”的,單位是毫秒。
sentinel?down-after-milliseconds?mymaster?3000
#?failover過期時間。當(dāng)failover開始后,在此時間內(nèi)仍然沒有觸發(fā)任何failover操作,當(dāng)前sentinel將會認(rèn)為此次failoer失敗。??
sentinel?failover-timeout?mymaster?1800

分別啟動redis進(jìn)程和sentinel進(jìn)程。

./redis-server?redis.conf
./redis-server?sentinel.conf

高可用集群分片模式

重要講解redis的官方集群安裝方式是如何操作的。

需要用到的軟件

redis-4.0.9.tar.gz 下載 (3.0開始支持集群功能)

ruby-2.5.7.tar.gz 下載 (一般我們選擇2.4或2.5就行,盡量不要用yum安裝,因?yàn)閥um安裝的ruby是2.0.0的,gem install redis ruby的版本必須是2.2.2以上)

redis-4.0.0.gem 下載

zlib-1.2.11.tar.gz? 自行網(wǎng)上查找

openssl-1.1.1a.tar.gz 自行網(wǎng)上查找

啟動端口:7001 7002 7003 7004 7005 7006

配置文件:/app/redis/redis-cluster

本文當(dāng)中,我們在一臺Linux上搭建6個節(jié)點(diǎn)的Redis集群(實(shí)際生產(chǎn)環(huán)境,需要3臺Linux服務(wù)器分布存放3個Master)

使用root用戶安裝,因?yàn)槠胀ㄓ脩魶]有創(chuàng)建目錄及編譯權(quán)限,需要授權(quán)

以下只講述離線安裝步驟

????安裝

第一步:安裝gcc環(huán)境

查看gcc版本,如果沒有安裝則需要進(jìn)行安裝

[root@localhost?~]#?gcc?-v
使用內(nèi)建?specs。
目標(biāo):x86_64-redhat-linux
配置為:../configure?--prefix=/usr?--mandir=/usr/share/man?--infodir=/usr/share/info?--with-bugurl=http://bugzilla.redhat.com/bugzilla?--enable-bootstrap?--enable-shared?--enable-threads=posix?--enable-checking=release?--with-system-zlib?--enable-__cxa_atexit?--disable-libunwind-exceptions?--enable-gnu-unique-object?--enable-languages=c,c++,objc,obj-c++,java,fortran,ada?--enable-java-awt=gtk?--disable-dssi?--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre?--enable-libgcj-multifile?--enable-java-maintainer-mode?--with-ecj-jar=/usr/share/java/eclipse-ecj.jar?--disable-libjava-multilib?--with-ppl?--with-cloog?--with-tune=generic?--with-arch_32=i686?--build=x86_64-redhat-linux
線程模型:posix
gcc?版本?4.4.7?20120313?(Red?Hat?4.4.7-23)?(GCC)

使用yum命令在線安裝

yum?-y?install?gcc?gcc-c++

第二步:解壓redis并編譯安裝

cd?/app/redis/
tar?-xvf?redis-4.0.9.tar.gz
cd?redis-4.0.9/
make
/app/redis/redis-4.0.9/src
make?install

安裝成功提示

Hint:?It’s?a?good?idea?to?run?‘make?test’?;)
INSTALL?install
INSTALL?install
INSTALL?install
INSTALL?install
INSTALL?install

redis安裝完之后會在/usr/local/bin目錄下多幾個文件

[root@localhost?~]#?ll?/usr/local/bin
總用量?35500
-rwxr-xr-x.?1?root?root?5600238?5月???6?20:54?redis-benchmark
-rwxr-xr-x.?1?root?root?8331277?5月???6?20:54?redis-check-aof
-rwxr-xr-x.?1?root?root?8331277?5月???6?20:54?redis-check-rdb
-rwxr-xr-x.?1?root?root?5739834?5月???6?20:54?redis-cli
lrwxrwxrwx.?1?root?root??????12?5月???6?20:54?redis-sentinel?->?redis-server
-rwxr-xr-x.?1?root?root?8331277?5月???6?20:54?redis-server

第三步:解壓ruby并編譯安裝

cd?/app/redis
tar?-xvf?ruby-2.5.1.tar.gz
cd?ruby-2.5.1/
./configure?--prefix=/usr/local/ruby??--prefix是將ruby安裝到指定目錄,也可以自定義
make?&&?make?install

安裝成功后可以看到在ruby目錄下多了四個目錄

[root@localhost?~]#?ll?/usr/local/ruby
總用量?16
drwxr-xr-x.?2?root?root?4096?10月?10?11:59?bin
drwxr-xr-x.?3?root?root?4096?10月?10?11:59?include
drwxr-xr-x.?4?root?root?4096?10月?10?11:59?lib
drwxr-xr-x.?5?root?root?4096?10月?10?11:59?share

配置ruby環(huán)境變量

vi?/etc/profile
export?PATH=$PATH:/usr/local/ruby/bin
:wq
source?/etc/profile

echo?$PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/openssh-7.5p1/bin:/root/bin:/usr/local/ruby/bin

查看ruby版本號

[root@localhost?~]#?ruby?-v
ruby?2.5.7p206?(2019-10-01?revision?67816)?[x86_64-linux]

第四步:創(chuàng)建redis-cluster集群目錄并拷貝redis的gem包以及在src下將redis-trib.rb 集群管理工具拷貝到集群目錄

cd?/app/redis
mkdir?redis-cluster
cp?redis-4.0.0.gem?redis-cluster/
/app/redis/redis-4.0.9/src
cp?redis-trib.rb?/app/redis/redis-cluster/

第五步:使用gem安裝redis的gem包

cd?/app/redis/redis-cluster
gem?install?redis-4.0.0.gem

如果安裝沒有任何問題會出現(xiàn)以下提示

Successfully?installed?redis-4.0.0
Parsing?documentation?for?redis-4.0.0
Installing?ri?documentation?for?redis-4.0.0
Done?installing?documentation?for?redis?after?1?seconds
1?gem?installed

如在第五步報(bào)錯需要zlib或者openssl

? ? 解決zlib報(bào)錯問題

cd?/app/redis/ruby-2.5.1/ext/zlib
ruby?extconf.rb????--成功會出現(xiàn)creating?Makefile
creating?Makefile????--如果沒有出現(xiàn)creating?Makefile,執(zhí)行下面的命令
ruby?extconf.rb?--with-zlib-dir=/usr/local/zlib/
vi?Makefile
將?????zlib.o:?$(top_srcdir)/include/ruby.h
修改為?zlib.o:?../../include/ruby.h
make
#?成功顯示
linking?shared-object?zlib.so
make?install
#?成功顯示
/usr/bin/install?-c?-m?0755?zlib.so?/usr/local/ruby/lib/ruby/site_ruby/2.5.0/x86_64-linux
如果上面再執(zhí)行make之前不修改Makefile,將會報(bào)下面的錯誤
make:?*?No?rule?to?make?target?/include/ruby.h',?needed?byzlib.o’.?Stop.

????解決openssl報(bào)錯問題

首先要安裝openssl,如果系統(tǒng)安裝了,本步驟可以省略

cd?/app/redis
tar?-zxvf?openssl-1.1.1a.tar.gz
./config?--prefix=/usr/local/openssl?&&?make?&&?make?install

安裝完成后繼續(xù)解決openssl報(bào)錯問題

cd?/app/redis/ruby-2.5.1/ext/openssl
ruby?extconf.rb????--成功會出現(xiàn)creating?Makefile

如發(fā)現(xiàn)沒有出現(xiàn)creating Makefile,請執(zhí)行下面命令

vi?Makefile
將所有的$(top_srcdir)修改為?../..$(top_srcdir)??#?注意(top_srcdir)不止有一個,所有的都要改
make
#?成功安裝輸出
linking?shared-object?openssl.so
make?install
#?成功安裝輸出
/usr/bin/install?-c?-m?0755?openssl.so?/usr/local/ruby/lib/ruby/site_ruby/2.5.0/x86_64-linux
installing?default?openssl?libraries
如果上面再執(zhí)行make之前不修改Makefile,將會報(bào)下面的錯誤
make:?*?No?rule?to?make?target?/include/ruby.h',?needed?byossl.o’.?Stop.

再次使用gem安裝redis的gem包

#?不出意外可以順利安裝?
cd?/app/redis/redis-cluster
gem?install?redis-4.0.0.gem

四.創(chuàng)建集群

之前講到是我們需要6個節(jié)點(diǎn)的Redis作為集群,所以我們需要創(chuàng)建6個文件夾,分別存放6個節(jié)點(diǎn)的配置信息,6個節(jié)點(diǎn)需要對應(yīng)6個端口號,比如7001~7006,這個端口號我們可以自行定義。

/app/redis/redis-cluster/
mkdir?700{1,2,3,4,5,6}????--批量創(chuàng)建六個文件夾

將原先redis安裝目錄下的配置文件redis.conf拷貝到新創(chuàng)建的六個文件夾下面

cd?/app/redis/redis-4.0.9
cp?redis.conf?/app/redis/redis-cluster/7001/
cp?redis.conf?/app/redis/redis-cluster/7002/
cp?redis.conf?/app/redis/redis-cluster/7003/
cp?redis.conf?/app/redis/redis-cluster/7004/
cp?redis.conf?/app/redis/redis-cluster/7005/
cp?redis.conf?/app/redis/redis-cluster/7006/

將redis安裝之后生成的服務(wù)端與客戶端拷貝到新創(chuàng)建的六個文件夾下面

cd?/usr/local/bin/
cp?redis-server?redis-cli?/app/redis/redis-cluster/7001/
cp?redis-server?redis-cli?/app/redis/redis-cluster/7002/
cp?redis-server?redis-cli?/app/redis/redis-cluster/7003/
cp?redis-server?redis-cli?/app/redis/redis-cluster/7004/
cp?redis-server?redis-cli?/app/redis/redis-cluster/7005/
cp?redis-server?redis-cli?/app/redis/redis-cluster/7006/

修改新創(chuàng)建的六個文件夾下面的配置文件redis.conf的部分參數(shù)

cd?/app/redis/redis-cluster/
修改7001-7006中的redis.conf
bind?192.168.5.104????連入主機(jī)的ip地址,不修改外部無法連入你的redis緩存服務(wù)器中
port?700X????x為文件夾名稱,你在700幾就填幾
daemonize?yes????開啟守護(hù)進(jìn)程模式。在該模式下,redis會在后臺運(yùn)行,并將進(jìn)程pid號寫入至redis.conf選項(xiàng)pidfile設(shè)置的文件中,此時redis將一直運(yùn)行,除非手動kill該進(jìn)程。
pidfile?/app/redis/redis-cluster/700x/redis_700x.pid????x為文件夾名稱,你在700幾就填幾
cluster-enabled?yes????開啟集群模式

cd /app/redis/redis-cluster

寫個批處理 vim start-all.sh,如果vim不支持就用vi

vim start-all.sh

cd?7001
??./redis-server?redis.conf
cd?..
cd?7002
??./redis-server?redis.conf
cd?..
cd?7003
??./redis-server?redis.conf
cd?..
cd?7004
??./redis-server?redis.conf
cd?..
cd?7005
??./redis-server?redis.conf
cd?..
cd?7006
??./redis-server?redis.conf
cd?..
并執(zhí)行?chmod?+x?start-all.sh?命令進(jìn)行授權(quán)

啟動實(shí)例之前在根路徑下配置多種環(huán)境

yum?-y?install?gcc?gcc-c++

啟動集群實(shí)例

./start-all.sh
[root@localhost?redis]#?ps?-ef|grep?redis
root?????14253?????1??0?Oct14??????????00:03:27?./redis-server?192.168.5.104:7001?[cluster]
root?????14262?????1??0?Oct14??????????00:03:38?./redis-server?192.168.5.104:7006?[cluster]
root?????18571?13962??0?15:37?pts/0????00:00:00?grep?redis
root?????30364?????1??0?Oct10??????????00:15:55?./redis-server?192.168.5.104:7002?[cluster]
root?????30372?????1??0?Oct10??????????00:15:51?./redis-server?192.168.5.104:7003?[cluster]
root?????30374?????1??0?Oct10??????????00:15:31?./redis-server?192.168.5.104:7004?[cluster]
root?????30379?????1??0?Oct10??????????00:15:17?./redis-server?192.168.5.104:7005?[cluster]

配置集群

cd?/app/redis/redis-cluster
#?創(chuàng)建了三個主節(jié)點(diǎn),三個從節(jié)點(diǎn)。其中—replicas1?表示每個主節(jié)點(diǎn)下面有1個從節(jié)點(diǎn),從節(jié)點(diǎn)可以是任意多個。
./redis-trib.rb?create?--replicas?1?192.168.4.212:7001?192.168.4.212:7002?192.168.4.212:7003?192.168.4.212:7004?192.168.4.212:7005?192.168.4.212:7006
>>>?Creating?cluster
>>>?Performing?hash?slots?allocation?on?6?nodes...
Using?3?masters:
192.168.5.104:7001????--主節(jié)點(diǎn)
192.168.5.104:7002????--主節(jié)點(diǎn)
192.168.5.104:7003????--主節(jié)點(diǎn)
Adding?replica?192.168.5.104:7005?to?192.168.5.104:7001????-主節(jié)點(diǎn)對應(yīng)的從節(jié)點(diǎn)
Adding?replica?192.168.5.104:7006?to?192.168.5.104:7002????-主節(jié)點(diǎn)對應(yīng)的從節(jié)點(diǎn)
Adding?replica?192.168.5.104:7004?to?192.168.5.104:7003????-主節(jié)點(diǎn)對應(yīng)的從節(jié)點(diǎn)
>>>?Trying?to?optimize?slaves?allocation?for?anti-affinity
[WARNING]?Some?slaves?are?in?the?same?host?as?their?master
M:?ee942ff5cf8cd0efddcf25b0ff21b5bc1c259589?192.168.5.104:7001
???slots:0-5460?(5461?slots)?master????????????????????????--主節(jié)點(diǎn)分配的hash槽
M:?3d48e5f1dbb2640d96b4522ece9b8be430bca721?192.168.5.104:7002
???slots:5461-10922?(5462?slots)?master????????????????????--主節(jié)點(diǎn)分配的hash槽
M:?dfa039f4cb9fed60b44e223a7afa462e34f904f7?192.168.5.104:7003
???slots:10923-16383?(5461?slots)?master????????????????????--主節(jié)點(diǎn)分配的hash槽
S:?4027313648b8615cec31b2b5c51e25bfc02ade59?192.168.5.104:7004
???replicates?3d48e5f1dbb2640d96b4522ece9b8be430bca721??????--從節(jié)點(diǎn)沒有hash槽
S:?57331ee1000d4ea652c7eda84b472a38bcd2e21d?192.168.5.104:7005
???replicates?dfa039f4cb9fed60b44e223a7afa462e34f904f7??????--從節(jié)點(diǎn)沒有hash槽
S:?8ca8eeba19b0acba4fd6f1e9f7be3c4bdb5ea1e5?192.168.5.104:7006
???replicates?ee942ff5cf8cd0efddcf25b0ff21b5bc1c259589??????--從節(jié)點(diǎn)沒有hash槽
Can?I?set?the?above?configuration??(type?'yes'?to?accept):?yes???--選擇yes,?意思是服從這種主從分配方式,我們也可以通過配置文件自己指定slave
>>>?Nodes?configuration?updated
>>>?Assign?a?different?config?epoch?to?each?node
>>>?Sending?CLUSTER?MEET?messages?to?join?the?cluster
Waiting?for?the?cluster?to?join....
>>>?Performing?Cluster?Check?(using?node?192.168.5.104:7001)????????--以下是詳細(xì)的主從節(jié)點(diǎn)分布
M:?ee942ff5cf8cd0efddcf25b0ff21b5bc1c259589?192.168.5.104:7001
???slots:0-5460?(5461?slots)?master
???1?additional?replica(s)
S:?4027313648b8615cec31b2b5c51e25bfc02ade59?192.168.5.104:7004
???slots:?(0?slots)?slave
???replicates?3d48e5f1dbb2640d96b4522ece9b8be430bca721
S:?8ca8eeba19b0acba4fd6f1e9f7be3c4bdb5ea1e5?192.168.5.104:7006
???slots:?(0?slots)?slave
???replicates?ee942ff5cf8cd0efddcf25b0ff21b5bc1c259589
M:?3d48e5f1dbb2640d96b4522ece9b8be430bca721?192.168.5.104:7002
???slots:5461-10922?(5462?slots)?master
???1?additional?replica(s)
M:?dfa039f4cb9fed60b44e223a7afa462e34f904f7?192.168.5.104:7003
???slots:10923-16383?(5461?slots)?master
???1?additional?replica(s)
S:?57331ee1000d4ea652c7eda84b472a38bcd2e21d?192.168.5.104:7005
???slots:?(0?slots)?slave
???replicates?dfa039f4cb9fed60b44e223a7afa462e34f904f7
[OK]?All?nodes?agree?about?slots?configuration.
>>>?Check?for?open?slots...
>>>?Check?slots?coverage...
[OK]?All?16384?slots?covered

五.集群測試

????測試主從高可用

????停止7001(master),登錄到7001的從上,查看發(fā)現(xiàn)狀態(tài)為master,使用命令 info Replication

cd?/app/redis/redis-cluster/7001
./redis-cli?-c?-h?192.168.5.104?-p?7001
./redis-cli?-c?-h?192.168.5.104?-p?7001?shutdown?//關(guān)閉7001節(jié)點(diǎn),如果沒有-h參數(shù),默認(rèn)連接127.0.0.1,如果沒有-p參數(shù),默認(rèn)連接6379端口(所有如果用默認(rèn)的,就沒有-h?-p)
說明:-h+host??-p+端口號??-c是要連接集群,注意坑,不加會報(bào)錯的

????當(dāng)主master down后,重新上線后,原先的slave依舊是master,原先的master由原先的master變?yōu)閟lave了(親測)

????測試集群的分片性

????通過java代碼對3對redis進(jìn)行循環(huán)寫入,查看3個master上的是否都有數(shù)據(jù)(親測所有master都有數(shù)據(jù))

????下面列出java連接redis的代碼

?/*******************集群模式***********************/
??JedisPoolConfig?poolConfig?=?new?JedisPoolConfig();
?????//?最大連接數(shù)
?????poolConfig.setMaxTotal(50);
?????//?最大空閑數(shù)
?????poolConfig.setMaxIdle(10);
?????//?最大允許等待時間,如果超過這個時間還未獲取到連接,則會報(bào)JedisException異常:
?????//?Could?not?get?a?resource?from?the?pool
?????poolConfig.setMaxWaitMillis(1000);
?????Set<HostAndPort>?nodes?=?new?LinkedHashSet<HostAndPort>();
?????nodes.add(new?HostAndPort("192.168.5.104",??Integer.parseInt("7001")));
?????nodes.add(new?HostAndPort("192.168.5.104",??Integer.parseInt("7002")));
?????nodes.add(new?HostAndPort("192.168.5.104",??Integer.parseInt("7003")));
?????nodes.add(new?HostAndPort("192.168.5.104",??Integer.parseInt("7004")));
?????nodes.add(new?HostAndPort("192.168.5.104",??Integer.parseInt("7005")));
?????nodes.add(new?HostAndPort("192.168.5.104",??Integer.parseInt("7006")));
?????@SuppressWarnings("resource")
??JedisCluster?cluster?=?new?JedisCluster(nodes,?poolConfig);
?????for(int?i=0;i<1000;i++){
???try?{
???Thread.sleep(500);
???System.out.println(i+"-------"+cluster.set("wc_"+i,?"xinxin"+i));
???}catch?(Exception?e)?{
????//?TODO?Auto-generated?catch?block
????e.printStackTrace();
????continue;
???}
??}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI