Install the Ruby environment

Redis cluster installation itself is very troublesome, through ruby tools, can be very convenient to package a series of commands into a script!

1. Install the Ruby environment:

yum -y install ruby

yum -y install rubygems

2. Install Redis gem

Gem is a plugin for manipulating Redis through Ruby.

Download yourself

gem install --local redis-3.2.0.gem

Redis.conf

Main configuration:

bind 192.168.88.100

port 6379

Daemonize yes # Run in daemon mode

Pidfile /var/run/redis_6379.pid # Run process ID default

Logfile "" # Log path

Prepare six Redis profiles for the cluster

The port numbers are 6380,6381,6382,6383,6384,6385

The configuration of each profile is as follows

1 include /root/redis/base.conf # Master configuration file2 daemonize yes
3 port 6380
4 pidfile "/root/redis/redis_6380.pid"
5 logfile "/root/redis/reds_log_6380.log"
6 dbfilename "dump_6380.rdb"# snapshot backup name, note only file name, cannot add path
7 cluster-enabled yes
8 cluster-config-file nodes-6380Conf # Cluster node setup9 cluster-node-timeout 15000

Redis: directory created by itself

Base. conf: redis.conf that you just prepared

Note: Delete all snapshot files dump

4. Start services separately

1redis-server redis_6380.conf
2redis-server redis_6381.conf
3redis-server redis_6382.conf
4redis-server redis_6383.conf
5redis-server redis_6384.conf
6redis-server redis_6385.conf

 

 

 

 

 

5. Join a cluster

Run the following command in the /opt/redis-3.2.5/ SRC directory

   

/opt/module/redis-3.2.5/src/redis-trib.rb create --replicas 1 192.168.88.100:6380 192.168.88.100:6381 192.168.88.100:6382 192.168.88.100:6383 192.168.88.100:6384 192.168.88.100:6385

 

 

 

 

 

 

Replicas 1 is a mater and a slave. By default, redis has three master nodes

Six, see

1. Check the process

 

 

 

2. View the cluster node

Log in to one of the clients

redis-cli -c -h 192.168.88.100 -p 6380

 

In the REDis-CLI, every time the key is entered or queried, Redis calculates the slot that the key should be sent to. If it is not the server slot corresponding to the client, Redis will report an error and inform the address and port of the Redis instance that should be sent to.

– Parameter c implements automatic redirection. For example, redis-cli -c -p 6379 after login, and then input, query key value pair can be automatically redirected.

Each slot can store a batch of key-value pairs

  

 

 

 

 

 

Myself: The one I log in to

Read key operation

  

 

 

After the hash algorithm is adopted, slots are automatically allocated. Multi-key operations such as Mget and Mset cannot be used for keys that are not in the same slot.

{} defines the concept of a group so that key-value pairs of the same contents in {} key are placed in a slot.

Read data from cluster

1, CLUSTER KEYSLOT

calculates which slot the key should be placed in

  

 

 

2. Cluster countKeysinslot

returns the number of key-value pairs that slot currently contains

  

 

 

3, CLUSTER GETKEYSINSLOT


returns count of the keys in the slot

  

 

Nine, cluster jedis development

1     @Test
2     public void testCluster(){
3         Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>(a);4         jedisClusterNodes.add(new HostAndPort("192.168.88.100".6380));
5         JedisCluster jc = new JedisCluster(jedisClusterNodes);
6         jc.set("foo"."bar");
7         String value = jc.get("foo");
8         System.out.println(value);
9     }

 

You only need to write to any port in the cluster, whether master or slave, to operate the cluster

X. Advantages and disadvantages

Advantages:

Implementation capacity

Share the pressure

The no-center configuration is relatively simple

Disadvantages:

Multi-key operations are not supported

Multi-key Redis transactions are not supported (because slots may be different). Lua scripts are not supported.

Due to the late emergence of the cluster scheme, many companies have adopted other cluster schemes, and the agent or client sharding scheme needs to be migrated to Redis Cluster as a whole rather than a gradual transition, which is quite complicated.