---
title: "redis集群配置"
author: "stephen830"
date: "2016-04-25T14:12:37Z"
tags: []
categories: ["开源软件"]
slug: "redis"
draft: false
lastmod: "2016-04-25T14:43:25Z"
id: 2293733
url: "https://stephen830.iteye.com/blog/2293733"
---

redis集群配置

 

首先需要安装redis，可以参考此文档[redis3.0.7安装指南 http://stephen830.iteye.com/blog/2289017。](/blog/2289017)

 

安装完毕后，将解压出来的redis-3.0.7目录复制3份（为什么是3份呢？因为redis做集群至少要3个），将着3份的目录分别取名为redis-3.0.7\_01、redis-3.0.7\_02、redis-3.0.7\_03 ，目录名可以按自己喜好定义。

 

这样，我们就有了3份的redis，然后分别修改redis-3.0.7\_01、redis-3.0.7\_02、redis-3.0.7\_03目录下的redis.conf配置文件。

 

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝

redis.conf 需要修改的参数

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝

port 6379 #端口号，不要相同，可以分别为6379 6380 6381

bind 192.168.1.2 #本机IP地址  
cluster-enabled yes #开启cluster模式，默认是no  
cluster-config-file nodes-6379.conf #cluster的配置文件，同端口一样，分别设为nodes-6379.conf、nodes-6380.conf、nodes-6381.conf  
cluster-node-timeout 5000 #cluster节点超时参数  
appendonly yes

dbfilename dump-6379.rdb #缓存实体化保存文件名，同端口一样，分别设为dump-6379.rdb、dump-6380.rdb、dump-6381.rdb

 

 然后，分别启动这3个目录下redis

/usr/local/redis-3.0.7\_01/src/redis-server /usr/local/redis-3.0.7\_01/redis.conf &

/usr/local/redis-3.0.7\_02/src/redis-server /usr/local/redis-3.0.7\_02/redis.conf &

/usr/local/redis-3.0.7\_03/src/redis-server /usr/local/redis-3.0.7\_03/redis.conf &

 

redis启动完成后，可以看到如下的提示：

\--------------------------------------------------------------------------------------------------

29338:M 13 Apr 21:05:00.214 * No cluster configuration found, I'm a1eec932d923b55e23a5fe6a488ed7a97e27c826  
                \_._                                                   
           \_.-\`\`\__ ''-._                                              
      \_.-\`\`    \`.  \`\_.  ''-._           Redis 3.0.0 (00000000/0) 64 bit  
  .-\`\` .-\`\`\`.  \`\`\`\\/    \_.,_ ''-._                                    
 (    '      ,       .-\`  | \`,    )     Running in cluster mode  
 |\`-.\_\`-...-\` \_\_...-.\`\`-.\_|'\` \_.-'|     Port: 6379  
 |    \`-._   \`._    /     \_.-'    |     PID: 29338  
  \`-._    \`-._  \`-./  \_.-'    \_.-'                                    
 |\`-.\_\`-._    \`-.\_\_.-'    \_.-'\_.-'|                                   
 |    \`-.\_\`-._        \_.-'\_.-'    |           http://redis.io         
  \`-._    \`-.\_\`-.\_\_.-'\_.-'    \_.-'                                    
 |\`-.\_\`-._    \`-.\_\_.-'    \_.-'\_.-'|                                   
 |    \`-.\_\`-._        \_.-'\_.-'    |                                   
  \`-._    \`-.\_\`-.\_\_.-'\_.-'    \_.-'                                    
      \`-._    \`-.\_\_.-'    \_.-'                                        
          \`-._        \_.-'                                            
              \`-.\_\_.-'                                              

29338:M 13 Apr 21:05:00.246 # Server started, Redis version 3.0.0  
29338:M 13 Apr 21:05:00.247 * DB loaded from disk: 0.000 seconds  
29338:M 13 Apr 21:05:00.247 * The server is now ready to accept connections on port 6379  
\--------------------------------------------------------------------------------------------------  
太多调试信息了，只有一句是最重要的：

No cluster configuration found, I'm a1eec932d923b55e23a5fe6a488ed7a97e27c826

这表示我们的redis服务器正在运行在cluster mode ，但是目前这3个redis尚未组成集群，继续执行下面的命令：

/usr/local/redis-3.0.7\_01/src/redis-trib.rb create 10.10.228.163:6379 10.10.228.163:6380 10.10.228.163:6381

执行正常的话，会显示如下提升信息：

&gt;&gt;&gt; Creating cluster  
Connecting to node 10.10.228.163:6379: OK  
Connecting to node 10.10.228.163:6380: OK  
Connecting to node 10.10.228.163:6381: OK  
&gt;&gt;&gt; Performing hash slots allocation on 3 nodes...  
Using 3 masters:  
10.10.228.163:6379  
10.10.228.163:6380  
10.10.228.163:6381  
M: 78a5bbdcd545848be8a66126a71dc69dd6d23bc4 10.10.228.163:6379  
   slots:0-5460 (5461 slots) master  
M: 1f6ed2478b461539f76b0b627de2e1b8565df719 10.10.228.163:6380  
   slots:5461-10922 (5462 slots) master  
M: 7a092b06c8c75e98176b7612e74d2e89e8b3eda7 10.10.228.163:6381  
   slots:10923-16383 (5461 slots) master  
Can I set the above configuration? (type 'yes' to accept):

看上去很美，每个节点负责数据的1/3，键入 ‘yes’…

&gt;&gt;&gt; Nodes configuration updated  
&gt;&gt;&gt; Assign a different config epoch to each node  
&gt;&gt;&gt; Sending CLUSTER MEET messages to join the cluster  
Waiting for the cluster to join.  
&gt;&gt;&gt; Performing Cluster Check (using node 127.0.0.1:7001)  
M: 78a5bbdcd545848be8a66126a71dc69dd6d23bc4 10.10.228.163:6379  
   slots:0-5460 (5461 slots) master  
M: 1f6ed2478b461539f76b0b627de2e1b8565df719 10.10.228.163:6380  
   slots:5461-10922 (5462 slots) master  
M: 7a092b06c8c75e98176b7612e74d2e89e8b3eda7 10.10.228.163:6381  
   slots:10923-16383 (5461 slots) master  
\[OK] All nodes agree about slots configuration.  
&gt;&gt;&gt; Check for open slots...  
&gt;&gt;&gt; Check slots coverage...  
\[OK] All 16384 slots covered.

完成了

 

\------------------------------------------------------------------  
redis创建集群时可能会遇到的错误或问题:  
\------------------------------------------------------------------

错误1:  
&gt;&gt;&gt; Creating cluster  
\[ERR] Sorry, can't connect to node 127.0.0.1:6379  
错误1解决方法：由于redis设置了密码，将redis.conf中的requirepass xxx这一行注释掉即可。  
\------------------------------------------------------------------

错误2:  
root@10-10-228-163:/usr/local/redis-3.0.7/src# ./redis-trib.rb create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381  
&gt;&gt;&gt; Creating cluster  
&gt;&gt;&gt; Performing hash slots allocation on 3 nodes...  
Using 3 masters:  
127.0.0.1:6379  
127.0.0.1:6380  
127.0.0.1:6381  
M: 4f82f807e8024c083595868c2a0bba81bdf19893 127.0.0.1:6379  
   slots:0-5460,11144,12596,13595,14103,15358,15369 (5467 slots) master  
M: 5102c6d494ed594975f5613a2653e9f3c72d07da 127.0.0.1:6380  
   slots:3225,3698,5461-10922,11144,12596,13595,14103,15358,15369 (5470 slots) master  
M: 05119d158b226c76ac0e83f89bb0e053d4dd1356 127.0.0.1:6381  
   slots:10923-16383 (5461 slots) master  
Can I set the above configuration? (type 'yes' to accept): yes  
/var/lib/gems/1.9.1/gems/redis-3.3.0/lib/redis/client.rb:121:in \`call': ERR Slot 3225 is already busy (Redis::CommandError)  
    from /var/lib/gems/1.9.1/gems/redis-3.3.0/lib/redis.rb:2700:in \`block in method\_missing'  
    from /var/lib/gems/1.9.1/gems/redis-3.3.0/lib/redis.rb:58:in \`block in synchronize'  
    from /usr/lib/ruby/1.9.1/monitor.rb:211:in \`mon\_synchronize'  
    from /var/lib/gems/1.9.1/gems/redis-3.3.0/lib/redis.rb:58:in \`synchronize'  
    from /var/lib/gems/1.9.1/gems/redis-3.3.0/lib/redis.rb:2699:in \`method\_missing'  
    from ./redis-trib.rb:212:in \`flush\_node\_config'  
    from ./redis-trib.rb:775:in \`block in flush\_nodes\_config'  
    from ./redis-trib.rb:774:in \`each'  
    from ./redis-trib.rb:774:in \`flush\_nodes\_config'  
    from ./redis-trib.rb:1295:in \`create\_cluster\_cmd'  
    from ./redis-trib.rb:1695:in \`&lt;main&gt;'  
root@10-10-228-163:/usr/local/redis-3.0.7/src#  
root@10-10-228-163:/usr/local/redis-3.0.7/src#

错误2解决方法：经检查，这是由于上一次配置集群失败时留下的配置信息导致的。 只要把redis.conf中定义的 cluster-config-file 所在的文件删除，重新启动redis-server及运行redis-trib即可.

\------------------------------------------------------------------

错误3:  
root@10-10-228-163:/usr/local/redis-3.0.7/src# ./redis-cli -p 6379  
127.0.0.1:6379&gt; set key1 zjq  
(error) MOVED 9189 127.0.0.1:6380  
错误3解决方法：必须带参数-c来连接redis操作，即执行命令:   ./redis-cli -c -p 6379

\------------------------------------------------------------------

安装ruby:  
apt-get install ruby-full

\------------------------------------------------------------------

安装ruby所需要的redis模块:  
root@10-10-228-163:/usr/local/redis-3.0.7# gem install redis  
Fetching: redis-3.3.0.gem (100%)  
Successfully installed redis-3.3.0  
1 gem installed  
Installing ri documentation for redis-3.3.0...  
Installing RDoc documentation for redis-3.3.0...  
root@10-10-228-163:/usr/local/redis-3.0.7#

如果用gem安装redis失败，可以手动下载redis的gem包  
下载地址 https://rubygems.global.ssl.fastly.net/gems/redis-3.3.0.gem  
然后使用gem安装命令:  
zhoujianqiangdeMacBook-Pro:soft zjq$ sudo gem install -l ./redis-3.3.0.gem  
Successfully installed redis-3.3.0  
Parsing documentation for redis-3.3.0  
Installing ri documentation for redis-3.3.0  
Done installing documentation for redis after 1 seconds  
1 gem installed  
zhoujianqiangdeMacBook-Pro:soft zjq$

\------------------------------------------------------------------

java链接redis-cluster

\------------------------------------------------------------------

 private static BinaryJedisCluster jc;    
  static {    
       //只给集群里一个实例就可以    
        Set&lt;HostAndPort&gt; jedisClusterNodes = new HashSet&lt;HostAndPort&gt;();    
        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 &lt; 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());    
    }