How to configure Redis Cluster

来自koorka知识分享
跳转至: 导航搜索
  • Install software
 yum install redis
yum install rubygems
gem install redis
  • Enable redis cluster mode.
The following is a minimal Redis cluster configuration file:
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
  • Create cluster with reds cluster utils:

Download the redis source code:

wget http://download.redis.io/redis-stable.tar.gz

tar -zxvf redis-stable.tar.gz

cd redis-stable/src

./redis-trib.rb create --replicas 1 192.168.1.241:6379 192.168.1.242:6379 192.168.1.243:6379 192.168.1.244:6379 192.168.1.245:6379 192.168.1.246:6379

The option --replicas 1 means thanodet we want a slave for every master created. 

  • Use the cluster with jedis:
            Set<HostAndPort> nodes= new HashSet<>();
            nodes.add(new HostAndPort("192.168.1.241",6379));
            nodes.add(new HostAndPort("192.168.1.242",6379));
            nodes.add(new HostAndPort("192.168.1.243",6379));
            nodes.add(new HostAndPort("192.168.1.244",6379));
            nodes.add(new HostAndPort("192.168.1.245",6379));
            nodes.add(new HostAndPort("192.168.1.246",6379));
            JedisCluster cluster=new JedisCluster(nodes);
            cluster.get("keyName");
            cluster.set("keyName","value")