搭建redis集群总结

1.配置文件中,修改的地方:

daemonize yes
port 9001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

2.分别启动三个节点的redis

3.在redis的src下执行

./redis-trib.rb create --replicas 0 192.168.1.150:6379 192.168.1.151:6379 192.168.1.152:6379

如果报错,则执行一些ruby的安装

yum install ruby

yum install rubygems

gem install redis

执行完了以后,则可以测试集群是否成功

redis-cli -c -h 192.168.1.150 -p 6379

其中 -c等必须加上,否则报错  (error) MOVED xxx

4.楼主的测试环境中,由于关机重启,导致重启以后,集群不可用

-解决办法:

ERR Slot 8620 is already busy (Redis::CommandError)

启动三个集群,然后分别删除redis.conf中配置的cluster-config-file 的参数文件

然后分别进入到三个节点中,执行flushdb

然后再执行 ./redis-trib.rb create --replicas 0 192.168.1.150:6379 192.168.1.151:6379 192.168.1.152:6379

5.删除节点

 ./redis-trib.rb del-node 192.168.1.150:6380 'b0057986957645aa22e89659ea2c696c9b8e13c7'

6.添加节点

./redis-trib.rb add-node 192.168.1.150:6380 192.168.1.150:6379

则节点添加成功

接下来设置slot

redis-trib.rb reshard 10.10.34.14:6386
#根据提示选择要迁移的slot数量(ps:这里选择500)
How many slots do you want to move (from 1 to 16384)? 500
#选择要接受这些slot的node-id
What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf
#选择slot来源:
#all表示从所有的master重新分配,
#或者数据要提取slot的master节点id,最后用done结束
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:all
#打印被移动的slot后,输入yes开始移动slot以及对应的数据.
#Do you want to proceed with the proposed reshard plan (yes/no)? yes
#结束

7.添加从节点

首先保证从节点是空的,如果不是,则在cli中执行flushdb,然后看看是不是存在cluster-config-file 中的参数文件,如果存在,则删除

接下来执行  ./redis-trib.rb add-node --slave 192.168.1.150:6380 192.168.1.150:6379,显示如下

8:redis cluster 客户端(Jedis)

8.1:客户端基本操作使用

 

 private static BinaryJedisCluster jc;
  static {
       //只给集群里一个实例就可以
        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6380));
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6381));
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6382));
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6383));
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6384));
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7380));
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7381));
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7382));
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7383));
        jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7384));
        jc = new BinaryJedisCluster(jedisClusterNodes);
    }
@Test
    public void testBenchRedisSet() throws Exception {
        final Stopwatch stopwatch = new Stopwatch();
        List list = buildBlogVideos();
        for (int i = 0; i < 1000; i++) {
            String key = "key:" + i;
            stopwatch.start();
            byte[] bytes1 = protostuffSerializer.serialize(list);
            jc.setex(key, 60 * 60, bytes1);
            stopwatch.stop();
        }
        System.out.println("time=" + stopwatch.toString());
    }

  

8.2:redis-cluster客户端的一些坑.

1)cluster环境下slave默认不接受任何读写操作,在slave执行readonly命令后,可执行读操作

2)client端不支持多key操作(mget,mset等),但当keys集合对应的slot相同时支持mget操作见:hash_tag

3)不支持多数据库,只有一个db,select 0。

4)JedisCluster 没有针对byte[]的API,需要自己扩展

 

部分参考网址:

http://www.cnblogs.com/zhaoguihua/p/redis-006.html

http://hot66hot.iteye.com/blog/2050676

 

posted on 2016-06-11 03:33  wonder2636  阅读(396)  评论(0编辑  收藏  举报

导航