redis偽叢集配置Cluster叢集模式

volc19發表於2019-04-09

下載編譯安裝redis

新建redis安裝目錄

/usr/local/share/redis

[root@ip-172-31-17-64 share]# mkdir redis
# 下載redis安裝包
[root@ip-172-31-17-64 share]# wget http://download.redis.io/releases/redis-3.2.4.tar.gz
[root@ip-172-31-17-64 share]# tar -zxvf redis-3.2.4.tar.gz
[root@ip-172-31-17-64 share]# cd redis-3.2.4/
[root@ip-172-31-17-64 share]# make && make install
複製程式碼

節點配置

# 建立redis_cluster目錄
[root@ip-172-31-17-64 redis]# mkdir redis_cluster
[root@ip-172-31-17-64 redis]# cd redis_cluster
[root@ip-172-31-17-64 redis_cluster]# mkdir 7000 7001  7002 7003 7004 7005
# 進入redis-3.2.4目錄,複製redis.conf到7000 7001 7002 7003 7004 7005中
[root@ip-172-31-17-64 redis-3.2.4]# cp redis.conf /usr/local/share/redis/redis_cluster/7000
[root@ip-172-31-17-64 redis-3.2.4]# cp redis.conf /usr/local/share/redis/redis_cluster/7001
[root@ip-172-31-17-64 redis-3.2.4]# cp redis.conf /usr/local/share/redis/redis_cluster/7002
[root@ip-172-31-17-64 redis-3.2.4]# cp redis.conf /usr/local/share/redis/redis_cluster/7003
[root@ip-172-31-17-64 redis-3.2.4]# cp redis.conf /usr/local/share/redis/redis_cluster/7004
[root@ip-172-31-17-64 redis-3.2.4]# cp redis.conf /usr/local/share/redis/redis_cluster/7005
複製程式碼

修改配置檔案

修改以下配置項

# 修改成對應調的埠號
port 7000
# 註釋掉 bind 配置
# bind 127.0.0.1
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
複製程式碼

啟動redis

分別進入叢集目錄啟動

[root@ip-172-31-17-64 redis_cluster]# cd 7000
# 啟動
[root@ip-172-31-17-64 7000]# redis-server redis.conf
複製程式碼

客戶端連線

redis_a

安裝Ruby

[root@ip-172-31-17-64 7000]# yum install ruby
[root@ip-172-31-17-64 7000]# yum install rubygems
[root@ip-172-31-17-64 7000]# gem install redis -v 3.2.2
複製程式碼
# 進入7000目錄,修改redis.conf配置
[root@ip-172-31-17-64 7000]# 
複製程式碼

叢集節點

# 進入redis根據目錄的src目錄,執行命令
# 52.77.243.30 為伺服器外網ip
[root@ip-172-31-17-64 src]# ./redis-trib.rb create --replicas 1 52.77.243.30:7000 52.77.243.30:7001 52.77.243.30:7002 52.77.243.30:7003 52.77.243.30:7004 52.77.243.30:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
52.77.243.30:7000
52.77.243.30:7001
52.77.243.30:7002
Adding replica 52.77.243.30:7003 to 52.77.243.30:7000
Adding replica 52.77.243.30:7004 to 52.77.243.30:7001
Adding replica 52.77.243.30:7005 to 52.77.243.30:7002
M: 703b3f7c232f3bb99fc0004cef4ab0aac92f0b28 52.77.243.30:7000
   slots:0-5460 (5461 slots) master
M: 2464c0a5dcd7e0adf266951b5d1262871a96c819 52.77.243.30:7001
   slots:5461-10922 (5462 slots) master
M: 774b9a2d5bd214cfac0c6ded72ab5fa5bfcf2112 52.77.243.30:7002
   slots:10923-16383 (5461 slots) master
S: d000b91c9efffa7d2b55ca37cacd915698be22de 52.77.243.30:7003
   replicates 703b3f7c232f3bb99fc0004cef4ab0aac92f0b28
S: c1aae83e0324292254b6b81bfce53faf830ef9c1 52.77.243.30:7004
   replicates 2464c0a5dcd7e0adf266951b5d1262871a96c819
S: a2ecec5c754a73a33e4e14c16ea7cb6fe63b61dc 52.77.243.30:7005
   replicates 774b9a2d5bd214cfac0c6ded72ab5fa5bfcf2112
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 52.77.243.30:7000)
M: 703b3f7c232f3bb99fc0004cef4ab0aac92f0b28 52.77.243.30:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: c1aae83e0324292254b6b81bfce53faf830ef9c1 52.77.243.30:7004
   slots: (0 slots) slave
   replicates 2464c0a5dcd7e0adf266951b5d1262871a96c819
S: a2ecec5c754a73a33e4e14c16ea7cb6fe63b61dc 52.77.243.30:7005
   slots: (0 slots) slave
   replicates 774b9a2d5bd214cfac0c6ded72ab5fa5bfcf2112
M: 2464c0a5dcd7e0adf266951b5d1262871a96c819 52.77.243.30:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: d000b91c9efffa7d2b55ca37cacd915698be22de 52.77.243.30:7003
   slots: (0 slots) slave
   replicates 703b3f7c232f3bb99fc0004cef4ab0aac92f0b28
M: 774b9a2d5bd214cfac0c6ded72ab5fa5bfcf2112 52.77.243.30:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@ip-172-31-17-64 src]# redis-cli -c -p 7000
127.0.0.1:7000> quit
複製程式碼

測試

public void testRedisCluster() throws IOException{
		JedisPoolConfig pool = new JedisPoolConfig();
		// 設定最大的空閒數
		pool.setMaxIdle(2);
		// 設定最大連線數
		pool.setMaxTotal(30);
		Set<HostAndPort> nodes = new HashSet<>();
		nodes.add(new HostAndPort("52.77.243.30", 7001));
		nodes.add(new HostAndPort("52.77.243.30", 7002));
		nodes.add(new HostAndPort("52.77.243.30", 7003));
		nodes.add(new HostAndPort("52.77.243.30", 7004));
		nodes.add(new HostAndPort("52.77.243.30", 7005));
		nodes.add(new HostAndPort("52.77.243.30", 7000));
		JedisCluster jedis = new JedisCluster(nodes,pool );
		String name = jedis.set("name","lisi");
		System.out.println(name);
		jedis.close();
		pool.clone();
	}
複製程式碼

redis_b

Cluster叢集模式缺點

redis-cluster叢集引入了主從模式,一個主節點對應一個或者多個從節點,當主節點當機的時候,就會啟用從節點。當其它主節點ping一個主節點A時,如果半數以上的主節點與A通訊超時,那麼認為主節點A當機了。如果主節點A和它的從節點A1都當機了,那麼該叢集的A和A1就無法再提供服務了,部分資料無法儲存到redis了,叢集不可用。

相關文章