Download, compile and install Redis

Create the redis installation directory

/usr/local/share/redis

[root@ip-172-31-17-64 share]# mkdir redis
Download the Redis installation package
[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
Copy the code

Node configuration

# Create directory 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
# Go to redis-3.2.4 and copy redis.conf to 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
Copy the code

Modifying a Configuration File

Go to the cluster directory and modify the redis.conf configuration

Modify the following configuration items

# Change to the port number of the corresponding tone
port 7000
Comment out the bind configuration
# bind 127.0.0.1
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
Copy the code

Start the redis

Enter the cluster directory to start each cluster

[root@ip-172-31-17-64 redis_cluster]# cd 7000
# start
[root@ip-172-31-17-64 7000]# redis-server redis.conf
Copy the code

Client connection

Install 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
Copy the code

Cluster nodes

< redis SRC > redis SRC > redis SRC > redis SRC
# 52.77.243.30 is the Internet IP address of the server
[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 52.77.243.30:7004:7005
>>> Creating cluster
>>> Performing hashslots 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:7002m: 703b3f7c232f3bb99fc0004CEF4ab0aAC92f0b28 52.77.243.30: slots 0-5460 (5461 slots) master M: 2464 c0a5dcd7e0adf266951b5d1262871a96c819 52.77.243.30:7001 slots: 5461-10922 (5462 slots) master M: 774 b9a2d5bd214cfac0c6ded72ab5fa5bfcf2112 52.77.243.30:7002 slots: 10923-16383 (5461 slots) master S: 703 b3f7c232f3bb99 d000b91c9efffa7d2b55ca37cacd915698be22de 52.77.243.30:7003 replicatesfc0004cef4ab0aac92f0b28 S: C1aae83e0324292254b6b81bfce53faf830ef9c1 52.77.243.30:7004 replicates 2464 c0a5dcd7e0adf266951b5d1262871a96c819 S: 774 b9a2d5bd214cfac0c6ded72ab5fa5bfcf2112 a2ecec5c754a73a33e4e14c16ea7cb6fe63b61dc 52.77.243.30:7005 replicates Can Iset 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: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: 2464 c0a5dcd7e0adf266951b5d1262871a96c819 52.77.243.30:7001 slots: 5461-10922 (5462 slots) master 1 additional up (s) S: d000b91c9efffa7d2b55ca37cacd915698be22de 52.77.243.30:7003 slots: (0 slots) slave 703 b3f7c232f3bb99 replicatesfc0004cef4ab0aac92f0b28 M: 774 b9a2d5bd214cfac0c6ded72ab5fa5bfcf2112 52.77.243.30:7002 slots: 10923-16383 (5461 slots) master 1 additional up (s)  [OK] All nodes agree about slots configuration. >>> Checkfor open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@ip-172-31-17-64 src]# redis-cli -c -p 7000127.0.0.1:7000 > quitCopy the code

test

public void testRedisCluster(a) throws IOException{
		JedisPoolConfig pool = new JedisPoolConfig();
		// Set the maximum number of idle
		pool.setMaxIdle(2);
		// Set the maximum number of connections
		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();
	}
Copy the code

Cluster Cluster mode disadvantages

A primary node corresponds to one or more secondary nodes. When the primary node is down, the secondary nodes are enabled. When other primary nodes ping one primary node A, if the communication between more than half of the primary nodes and A times out, the primary node A is considered to be down. If the primary node A and its secondary node A1 are down, then A and A1 of the cluster can no longer provide services, and some data cannot be saved to Redis, and the cluster is unavailable.