Redis5.0.4集群模式搭建

我將在同一臺(tái)Redis節(jié)點(diǎn)上創(chuàng)建三主三從Redis集群

創(chuàng)建并啟動(dòng)redis節(jié)點(diǎn)

  1. 在redis文件夾下創(chuàng)建8000 ~8005總共6個(gè)文件夾,作為存放redis配置文件的目錄
cd redis-5.0.4
mkdir 8000 8001 8002 8003 8004 8005
  1. 將redis目錄下的redis.conf復(fù)制到各個(gè)節(jié)點(diǎn)目錄下
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8000/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8001/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8002/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8003/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8004/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8005/
  1. 修改配置文件,以8000端口為例
protected-mode no
port 8000
cluster-enabled yes
cluster-config-file nodes-8000.conf
cluster-node-timeout 5000
daemonize yes
pidfile /var/run/redis_8000.pid
logfile "8000.log"
dir /redis/data
bind 127.0.0.1
  1. 啟動(dòng)各個(gè)redis節(jié)點(diǎn)
[root@bigdata24 redis-5.0.4]$ src/redis-server 8000/redis.conf 
[root@bigdata24 redis-5.0.4]$ src/redis-server 8001/redis.conf 
[root@bigdata24 redis-5.0.4]$ src/redis-server 8002/redis.conf 
[root@bigdata24 redis-5.0.4]$ src/redis-server 8003/redis.conf 
[root@bigdata24 redis-5.0.4]$ src/redis-server 8004/redis.conf 
[root@bigdata24 redis-5.0.4]$ src/redis-server 8005/redis.conf 
  1. 查看是否正常啟動(dòng)
[root@bigdata24 redis-5.0.4]$ ps -ef|grep redis
root      3778     1  0 14:20 ?        00:00:00 src/redis-server 127.0.0.1:8000 [cluster]
root      3819     1  0 14:21 ?        00:00:00 src/redis-server 127.0.0.1:8001 [cluster]
root      3827     1  0 14:21 ?        00:00:00 src/redis-server 127.0.0.1:8002 [cluster]
root      3834     1  0 14:21 ?        00:00:00 src/redis-server 127.0.0.1:8003 [cluster]
root      3842     1  0 14:21 ?        00:00:00 src/redis-server 127.0.0.1:8004 [cluster]
root      3849     1  0 14:21 ?        00:00:00 src/redis-server 127.0.0.1:8005 [cluster]
root      3858  1598  0 14:21 pts/1    00:00:00 grep --color=auto redis

啟動(dòng)集群

Redis5.0集群管理工具redis-trib.rb已經(jīng)被棄用,所以不用安裝ruby等組件,上面安裝ruby的方法針對(duì)于redis5.0以下使用。redis-trib.rb的功能,現(xiàn)在已經(jīng)集成到了redis-cli中

  1. 通過(guò)以下語(yǔ)句創(chuàng)建集群

redis-cli --cluster create 127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8005 --cluster-replicas 1

[root@bigdata24 src]$ redis-cli --cluster create   127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8005  --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:8004 to 127.0.0.1:8000
Adding replica 127.0.0.1:8005 to 127.0.0.1:8001
Adding replica 127.0.0.1:8003 to 127.0.0.1:8002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 17e2fc02eb1e9961fa36af210397ed8dc1dc360d 127.0.0.1:8000
   slots:[0-5460] (5461 slots) master
M: 860d39006e21ba74052627e899f307d0cfbec0b5 127.0.0.1:8001
   slots:[5461-10922] (5462 slots) master
M: d5882b089f975713c94dcbc1537c11428d5a9e1a 127.0.0.1:8002
   slots:[10923-16383] (5461 slots) master
S: 3606713658cc3cdf7e1c6456d94143b91c7da928 127.0.0.1:8003
   replicates d5882b089f975713c94dcbc1537c11428d5a9e1a
S: fd6db44a16e12d6dc00a70d02472d07a69917acd 127.0.0.1:8004
   replicates 17e2fc02eb1e9961fa36af210397ed8dc1dc360d
S: 93de823d19aa2879c283e47e02a7b0a33ff0e0a3 127.0.0.1:8005
   replicates 860d39006e21ba74052627e899f307d0cfbec0b5
Can I set the above configuration? (type 'yes' to accept): yes
>>> 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 127.0.0.1:8000)
M: 17e2fc02eb1e9961fa36af210397ed8dc1dc360d 127.0.0.1:8000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 3606713658cc3cdf7e1c6456d94143b91c7da928 127.0.0.1:8003
   slots: (0 slots) slave
   replicates d5882b089f975713c94dcbc1537c11428d5a9e1a
M: d5882b089f975713c94dcbc1537c11428d5a9e1a 127.0.0.1:8002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 860d39006e21ba74052627e899f307d0cfbec0b5 127.0.0.1:8001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 93de823d19aa2879c283e47e02a7b0a33ff0e0a3 127.0.0.1:8005
   slots: (0 slots) slave
   replicates 860d39006e21ba74052627e899f307d0cfbec0b5
S: fd6db44a16e12d6dc00a70d02472d07a69917acd 127.0.0.1:8004
   slots: (0 slots) slave
   replicates 17e2fc02eb1e9961fa36af210397ed8dc1dc360d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  1. 通過(guò)以下語(yǔ)句連接到某個(gè)節(jié)點(diǎn)
redis-cli -h bigdata24 -p 8000

使用java連接Redis集群

public class JedisClusterUtil {

        private static volatile JedisCluster cluster;

        private JedisClusterUtil() {}

        public static JedisCluster getInstance() {
            if (cluster == null) {
                synchronized (JedisClusterUtil.class) {
                    if (cluster == null) {
                        Set<HostAndPort> nodes = new LinkedHashSet<>();
                        nodes.add(new HostAndPort("bigdata24", 8000));
                        nodes.add(new HostAndPort("bigdata24", 8001));
                        nodes.add(new HostAndPort("bigdata24", 8002));
                        nodes.add(new HostAndPort("bigdata24", 8003));
                        nodes.add(new HostAndPort("bigdata24", 8004));
                        nodes.add(new HostAndPort("bigdata24", 8005));
                        cluster = new JedisCluster(nodes);
                        return cluster;
                    }
                }
            }
            return cluster;
        }
}
遇到的問(wèn)題

當(dāng)我修改redis配置的時(shí)候,kill掉了所有的redis節(jié)點(diǎn),cluster宕掉了,這個(gè)時(shí)候重新創(chuàng)建集群失敗

[ERR] Node bigdata24:8000 is not empty. 
Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

需要?jiǎng)h除掉redis根目錄下data以及src目錄下的dump.rdb以及nodes-8003.conf
確認(rèn)刪除干凈后一定要重啟各個(gè)redis節(jié)點(diǎn)

之后還遇到一個(gè)問(wèn)題

Node bigdata24:8001 replied with error:
ERR Invalid node address specified: bigdata24:8000

這是因?yàn)槲以谂渲胷edis.conf的時(shí)候使用了如下語(yǔ)句

bind bigdata24

Redis并不能識(shí)別到到IP映射 所以報(bào)錯(cuò) 將其改為IP之后重啟節(jié)點(diǎn)再創(chuàng)建集群就成功了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 Redis介紹1.1 什么是NoSql為了解決高并發(fā)、高可擴(kuò)展、高可用、大數(shù)據(jù)存儲(chǔ)問(wèn)題而產(chǎn)生的數(shù)據(jù)庫(kù)解決方...
    克魯?shù)吕?/span>閱讀 5,717評(píng)論 0 36
  • redis集群分為服務(wù)端集群和客戶(hù)端分片,redis3.0以上版本實(shí)現(xiàn)了集群機(jī)制,即服務(wù)端集群,3.0以下使用客戶(hù)...
    hadoop_null閱讀 1,677評(píng)論 0 6
  • (我是利用已經(jīng)安裝過(guò)redis的主機(jī)上進(jìn)行集群搭建的) 1、要讓集群正常工作至少需要3個(gè)主節(jié)點(diǎn),在這里我們要?jiǎng)?chuàng)建6...
    赫靈閱讀 902評(píng)論 0 0
  • Redis集群 一、Redis集群相關(guān)概念 1.Redis集群介紹 Redis 集群是一個(gè)提供在多個(gè)Redis間節(jié)...
    Java架構(gòu)_師閱讀 643評(píng)論 0 1
  • 首先請(qǐng)注意,寫(xiě)好字和練好書(shū)法是兩個(gè)不同的概念。 題主的這個(gè)問(wèn)題,可能更加偏向于“如何寫(xiě)好字”。 寫(xiě)好字,只是在平時(shí)...
    很久沒(méi)吃飯了閱讀 9,664評(píng)論 10 36

友情鏈接更多精彩內(nèi)容