Redis搭建集群 & Java读取集群中的key

Redis搭建集群 & Java读取集群中的key


一. Redis的安装

  1. 下载redis-4.0.2.tar.gz。到/usr/local直接使用此命令进行下载 wget http://download.redis.io/releases/redis-4.0.2.tar.gz
  2. tar -xvf redis-4.0.2.tar.gz。
  3. 解压后将文件夹改名为redis,进入redis目录。
  4. make
  5. make install

二.手动配置集群

该方案适用于服务器机房网络受限或者用户权限不够,无法安装ruby的情况下。这里模拟三主三从模式。为了方便,现将主从都放到一台服务器上。可根据实际情况自行分配服务器。

  • /data路径(位置随意)创建7001,7002,7003,7004,7005,7006文件夹。
  • /usr/local/redis/redis.conf分别复制到这些目录中。
  • 修改配置文件,主要修改这些配置(以7001为例,其他对应着修改):
    1. cluster-config-file nodes-6379.conf:修改为cluster-config-file nodes-7001.conf
    2. cluster-enabled yes:将注释去掉(或者直接在配置文件中添加)
    3. cluster-node-timeout 15000:将注释去掉(或者直接在配置文件中添加)
    4. port 6379:改为port 7001
  • 启动所有节点,比如:启动7001
    redis-server /data/7001/redis.conf &

    如果此命令报错,执行下面命令

[root@VM_3_220_centos src]# cd /usr/local/redis/src/
[root@VM_3_220_centos src]# ./redis-server /data/7001/redis.conf &
  • 查看启动情况
[root@VM_3_220_centos data]# ps -ef | grep redis
root     25888 20573  0 20:43 pts/1    00:00:00 redis-server 127.0.0.1:7001 [cluster]
root     25895 20573  0 20:43 pts/1    00:00:00 redis-server 127.0.0.1:7002 [cluster]
root     25899 20573  0 20:43 pts/1    00:00:00 redis-server 127.0.0.1:7003 [cluster]
root     25904 20573  0 20:43 pts/1    00:00:00 redis-server 127.0.0.1:7004 [cluster]
root     25910 20573  0 20:43 pts/1    00:00:00 redis-server 127.0.0.1:7005 [cluster]
root     25915 20573  0 20:43 pts/1    00:00:00 redis-server 127.0.0.1:7006 [cluster]
root     25921 20573  0 20:43 pts/1    00:00:00 grep --color=auto redis
  • 进入7001节点,开始配置集群:
[root@VM_3_220_centos src]# redis-cli -p 7001
127.0.0.1:7001> cluster meet 127.0.0.1 7002
OK
127.0.0.1:7001> cluster meet 127.0.0.1 7002
OK
127.0.0.1:7001> cluster meet 127.0.0.1 7003
OK
127.0.0.1:7001> cluster meet 127.0.0.1 7003
OK
127.0.0.1:7001> cluster meet 127.0.0.1 7004
OK
127.0.0.1:7001> cluster meet 127.0.0.1 7004
OK
127.0.0.1:7001> cluster meet 127.0.0.1 7005
OK
127.0.0.1:7001> cluster meet 127.0.0.1 7006
OK
127.0.0.1:7001> cluster nodes
e5c177341ac3fe5cd937f25b3a64af482c21ae86 127.0.0.1:7005@17005 master - 0 1531832105142 4 connected
99a72729d7767385afbfb4d444dd711b03f87e07 127.0.0.1:7002@17002 master - 0 1531832105000 1 connected
ecc40d1e59383fdc00cec5fa0824eacf321e0980 127.0.0.1:7001@17001 myself,master - 0 1531832106000 0 connected
18ff31dd76e12f29ecd6da58ef350fda071f441a 127.0.0.1:7003@17003 master - 0 1531832105000 2 connected
e08f6860cd623daed15816951032bdd0fd90adb8 127.0.0.1:7006@17006 master - 0 1531832107149 5 connected
5cfc64d81fb682b8394ba28d38960f56af6a7d0c 127.0.0.1:7004@17004 master - 0 1531832106145 3 connected
  • 分配hash槽
[root@VM_3_220_centos src]# redis-cli -c -p 7001 cluster addslots {0..5460}
OK
[root@VM_3_220_centos src]# redis-cli -c -p 7003 cluster addslots {5461..10922}
OK
[root@VM_3_220_centos src]# redis-cli -c -p 7005 cluster addslots {10923..16383}
25910:M 17 Jul 20:59:32.776 # Cluster state changed: ok
OK

可以看到当把16384个hash槽分配完成,可以看到集群的状态改变:Cluster state changed:ok

  • 开始配置主从节点

    命令后面的字符串为上面cluster nodes输出信息的第一列(节点id)

#配置7002为7001的从节点
[root@VM_3_220_centos src]# redis-cli -p 7002 cluster replicate ecc40d1e59383fdc00cec5fa0824eacf321e0980
#配置7004为7003的从节点
[root@VM_3_220_centos src]# redis-cli -p 7004 cluster replicate 18ff31dd76e12f29ecd6da58ef350fda071f441a
#配置7006为7005的从节点
[root@VM_3_220_centos src]# redis-cli -p 7006 cluster replicate e5c177341ac3fe5cd937f25b3a64af482c21ae86
  • 查看集群状态
[root@VM_3_220_centos src]# redis-cli -p 7001
127.0.0.1:7001> cluster nodes
e5c177341ac3fe5cd937f25b3a64af482c21ae86 127.0.0.1:7005@17005 master - 0 1531832809000 4 connected 10923-16383
99a72729d7767385afbfb4d444dd711b03f87e07 127.0.0.1:7002@17002 slave ecc40d1e59383fdc00cec5fa0824eacf321e0980 0 1531832809540 1 connected
ecc40d1e59383fdc00cec5fa0824eacf321e0980 127.0.0.1:7001@17001 myself,master - 0 1531832806000 0 connected 0-5460
18ff31dd76e12f29ecd6da58ef350fda071f441a 127.0.0.1:7003@17003 master - 0 1531832808000 2 connected 5461-10922
e08f6860cd623daed15816951032bdd0fd90adb8 127.0.0.1:7006@17006 slave e5c177341ac3fe5cd937f25b3a64af482c21ae86 0 1531832809000 5 connected
5cfc64d81fb682b8394ba28d38960f56af6a7d0c 127.0.0.1:7004@17004 slave 18ff31dd76e12f29ecd6da58ef350fda071f441a 0 1531832808538 3 connected

可以看到3主3从Redis集群已经搭建完成。

三.使用ruby方式进行搭建

  • 下载ruby http://www.ruby-lang.org/en/downloads/
  • 解压后进入到ruby文件夹里面,执行./configure --prefix=/usr/local/ruby,如果报错,先安装gcc:yum install -y gcc
  • 然后执行make & make install
  • 执行gem install redis
  • 启动所有节点(启动步骤和手动配置集群一样,这里省略)
  • 然后
[root@Sucre src]# cd /usr/local/redis/src
[root@Sucre src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
  • 当看到以下信息,证明集群配置成功:
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 查看集群状态:
[root@Sucre src]# redis-cli -p 7001
127.0.0.1:7001> cluster nodes
0fa91fb70c12ff81af2827d5f0b9a535a75c3415 127.0.0.1:7005@17005 slave ce1776ae717cd041b5edbf269b39cd5dab15503c 0 1531836422529 5 connected
8ed95014b74110322100bedac59a2ca8d4f2cc18 127.0.0.1:7006@17006 slave 59e5c8a0548b6867452f03aaa541c56fb576b4d5 0 1531836419512 6 connected
2854be57b4242160ca1241edb1b4102306e94c60 127.0.0.1:7001@17001 myself,master - 0 1531836421000 1 connected 0-5460
59e5c8a0548b6867452f03aaa541c56fb576b4d5 127.0.0.1:7003@17003 master - 0 1531836419000 3 connected 10923-16383
ce1776ae717cd041b5edbf269b39cd5dab15503c 127.0.0.1:7002@17002 master - 0 1531836420518 2 connected 5461-10922
682d5a88911bcca7347ff41d27cc897ad58b085a 127.0.0.1:7004@17004 slave 2854be57b4242160ca1241edb1b4102306e94c60 0 1531836421523 4 connected
到此ruby方式搭建3主3从完成。

四.Java中读取集群中所有key

其中构建JedisCluster过程省略。

public TreeSet keys(String pattern){   
    TreeSet keys = new TreeSet<>();  
    Map clusterNodes = jedisCluster.getClusterNodes();  
    for(String k : clusterNodes.keySet()){  
        JedisPool jp = clusterNodes.get(k);  
        try(Jedis connection = jp.getReource()) {  
            keys.addAll(connection.keys(pattern));  
        } catch(Exception e){  
            logger.error("Getting keys error: {}", e);  
        } 
    } 
    return keys;  
}
//直接通过keys("result*")来取得所有以result开头的key
//直接通过keys("*")来取得所有key

你可能感兴趣的:(study)