Redis cluster requires a minimum of 3 primary nodes
# Cd/usr/redis
Create a Directory
# mkdir Cluster
# CD Cluster
1. Copy a configuration file
# CP.. /redis.conf 9001.conf
Modifying a configuration file
# Vim 9001.conf
Port 9001
AppendOnly Yes
cluster-enabled Yes
Cluster-config-file nodes-9001.conf
Cluster-node-timeout 5000
2. Copy two copies of configuration files and modify
# CP 9001.conf 9002.conf
# CP 9001.conf 9003.conf
3. Start 3 Redis Instances
# Redis-server 9001.conf &
# Redis-server 9002.conf &
# Redis-server 9003.conf &
4, enable the cluster (0 means that the replica is not enabled, if set to a numeric value, such as 1, indicating that each host enables a slave, the cluster requires at least 6 nodes)
# redis-trib.rb Create--replicas 0 192.168.77.136:9001 192.168.77.136:9002 192.168.77.136:9003
4.1 error occurs when executing the above command, because it is the Ruby script that is executed and requires the environment of Ruby
Error content:/usr/bin/env:ruby:no such file or directory
So you need to install Ruby's environment, it is recommended to use Yum install Ruby installation
# yum Install Ruby
4.2 Then execute the Create cluster command, also error, hint missing RubyGems component, use Yum to install
Error content:
./redis-trib.rb:24:in ' require ': No such file to load--rubygems (Loaderror)
From./redis-trib.rb:24
# yum Install RubyGems
4.3 execute the cluster creation command again, and will also error, indicating that Redis cannot be loaded because of the lack of Redis and Ruby interfaces, using Gem installation
Error content:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in ' Gem_original_require ': No such file to load--Redis ( Loaderror)
From/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in ' require '
From./redis-trib.rb:25
# yum Install Redis
The installation fails because of the source, and the source information needs to be changed
# Gem Sources--remove https://rubygems.org/
# Gem Source-a https://ruby.taobao.org/
# gem Source-l See if the list below is not only https://ruby.taobao.org/this one source, if not repeatedly executed under.
After the source switch succeeds, re-executes the Yum install Redis
# yum Install Redis
Once the installation is complete, execute the cluster command again and run normally
>>>Creating clusterconnecting to Node192.168.77.136:9001: okconnecting to Node192.168.77.136:9002: okconnecting to Node192.168.77.136:9003: OK>>> performing hash slots allocation on3nodes ... Using3Masters:192.168.77.136:9001192.168.77.136:9002192.168.77.136:9003M:b32e955c04b37d4170860edf8ab1da0275811fe3192.168.77.136:9001Slots:0-5460(5461slots) Masterm:b4d0df0bec6740cf5680d14637aaa1f6f6283adb192.168.77.136:9002Slots:5461-10922(5462slots) masterm:5a834200f28caad158ef1fd5fe4ff4ea4b00622b192.168.77.136:9003Slots:10923-16383(5461slots) Mastercan I set the above configuration? (Type'Yes'to accept):
Enter Yes
>>>Nodes Configuration Updated>>>Assign a different config epoch to each node3605: M ,The -: -:53.112# Configepoch set to1Via CLUSTER set-config-EPOCH3608: M ,The -: -:53.112# Configepoch set to2Via CLUSTER set-config-EPOCH3612: M ,The -: -:53.113# Configepoch set to3Via CLUSTER set-config-EPOCH>>> sending CLUSTER MEET messages toJoinThe cluster3605: M ,The -: -:53.183# IP Address forThis node updated to192.168.77.1363608: M ,The -: -:53.284# IP Address forThis node updated to192.168.77.1363612: M ,The -: -:53.285# IP Address forThis node updated to192.168.77.136Waiting forThe cluster toJoin..>>> performing Cluster Check (using node192.168.77.136:9001) M:b32e955c04b37d4170860edf8ab1da0275811fe3192.168.77.136:9001Slots:0-5460(5461slots) Masterm:b4d0df0bec6740cf5680d14637aaa1f6f6283adb192.168.77.136:9002Slots:5461-10922(5462slots) masterm:5a834200f28caad158ef1fd5fe4ff4ea4b00622b192.168.77.136:9003Slots:10923-16383(5461slots) Master[ok] All nodes agree about slots configuration.>>> Check forOpen Slots ...>>>Check Slots Coverage ... [OK] All16384slots covered. [Email protected] cluster]#3605: M ,The -: -:58.025# Cluster State Changed:ok3612: M ,The -: -:58.125# Cluster State Changed:ok3608: M ,The -: -:58.125# Cluster State Changed:ok
5. Login Cluster
# REDIS-CLI-C-P 9001
5.1 View cluster operation cluster Info
127.0.0.1:9001> ClusterInfocluster_state:okcluster_slots_assigned:16384CLUSTER_SLOTS_OK:16384Cluster_slots_pfail:0Cluster_slots_fail:0Cluster_known_nodes:3cluster_size:3Cluster_current_epoch:3Cluster_my_epoch:1cluster_stats_messages_sent:6275cluster_stats_messages_received:6275
5.2 Viewing cluster nodes cluster nodes
127.0.0.1:9001>Cluster Nodesb32e955c04b37d4170860edf8ab1da0275811fe3192.168.77.136:9001Myself,master-0 0 1Connected0-54605a834200f28caad158ef1fd5fe4ff4ea4b00622b192.168.77.136:9003Master-0 1439094158089 3Connected10923-16383b4d0df0bec6740cf5680d14637aaa1f6f6283adb192.168.77.136:9002Master-0 1439094157080 2Connected5461-10922
5.3 Simple Verification
127.0. 0.1:9001> set a ABC-redirected to slot [15495192.168. 77.136:9003OK192.168. 77.136:9003> get a"ABC"
6 Java Program Connection cluster
ImportJava.util.HashSet;ImportJava.util.Set;ImportRedis.clients.jedis.HostAndPort;ImportRedis.clients.jedis.JedisCluster; Public classTestrediscluster { Public Static voidMain (string[] args) {Set<HostAndPort> jedisclusternodes =NewHashset(); Jedisclusternodes.add (NewHostandport ("192.168.77.136", 9001));
Use any of the nodes in a Redis cluster to Jediscluster Jedis=NewJediscluster (jedisclusternodes); //Remove all content before startingJedis.del ("Java framework"); System.out.println (Jedis.lrange ("Java framework", 0, 1)); //store three data in the key Java framework firstJedis.lpush ("Java framework", "Spring"); Jedis.lpush ("Java framework", "struts"); Jedis.lpush ("Java framework", "Hibernate"); //and then take out all the data jedis.lrange is out by range,//The first one is key, the second is the starting position, the third is the end position, Jedis.llen gets the length-1 means get allSystem.out.println (Jedis.lrange ("Java framework", 0, 1)); Jedis.del ("Java framework"); Jedis.rpush ("Java framework", "Spring"); Jedis.rpush ("Java framework", "struts"); Jedis.rpush ("Java framework", "Hibernate"); System.out.println (Jedis.lrange ("Java framework", 0, 1)); Jedis.close (); }}
127.0.0.1:9001> Cluster Infocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_ Pfail:0cluster_slots_fail:0cluster_known_nodes:3cluster_size:3cluster_current_epoch:3cluster_my_epoch:1cluster _stats_messages_sent:6275cluster_stats_messages_received:6275
Java uses redis8--3.0 clusters