搭建Redis集群--简单

## 基础环境准备 ##
redis集群环境需要执行ruby脚本,所以需要执行如下命令

yum install ruby
yum install rubygems
gem install redis

准备redis环境

按照推荐,需要准备6个redis作为集群(3个master3个slave)
我准备的如下

[root@localhost redis-cluser]# ls -l
total 28
drwxr-xr-x 6 root root 4096 Jan 12 22:05 redis
drwxr-xr-x 6 root root 4096 Jan 12 22:32 redis-7001
drwxr-xr-x 6 root root 4096 Jan 12 22:32 redis-7002
drwxr-xr-x 6 root root 4096 Jan 12 22:32 redis-7003
drwxr-xr-x 6 root root 4096 Jan 12 22:32 redis-7004
drwxr-xr-x 6 root root 4096 Jan 12 22:32 redis-7005
drwxr-xr-x 6 root root 4096 Jan 12 22:32 redis-7006

设置配置文件

因为6个redis都在一台机器上,所以需要修改端口和其他信息
在redis根目录下建立start.sh,内容为:src/./redis-server redis.conf

port 7001 #这个需要每个都修改下
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf #暂时不需要管,建立集群的时候会新建
cluster-node-timeout 5000
appendonly yes

启动6个redis

[root@localhost redis-7001]# ./start.sh 
[root@localhost redis-7001]# cd /opt/redis-cluser/redis-7002/
[root@localhost redis-7002]# ./start.sh 
[root@localhost redis-7002]# cd /opt/redis-cluser/redis-7003/
[root@localhost redis-7003]# ./start.sh 
[root@localhost redis-7003]# cd /opt/redis-cluser/redis-7004/
[root@localhost redis-7004]# ./start.sh 
[root@localhost redis-7004]# cd /opt/redis-cluser/redis-7005/
[root@localhost redis-7005]# ./start.sh 
[root@localhost redis-7005]# cd /opt/redis-cluser/redis-7006/
[root@localhost redis-7006]# ./start.sh 
[root@localhost redis-cluser]# ps -ef|grep redis
root 12012 1  0 22:28 ?00:00:04 src/./redis-server 127.0.0.1:7001 [cluster]
root 12061 1  0 22:31 ?00:00:03 src/./redis-server 127.0.0.1:7002 [cluster]
root 12066 1  0 22:31 ?00:00:04 src/./redis-server 127.0.0.1:7003 [cluster]
root 12072 1  0 22:31 ?00:00:03 src/./redis-server 127.0.0.1:7004 [cluster]
root 12077 1  0 22:31 ?00:00:03 src/./redis-server 127.0.0.1:7005 [cluster]
root 12082 1  0 22:31 ?00:00:04 src/./redis-server 127.0.0.1:7006 [cluster]
root 12443 11994  0 22:50 pts/200:00:00 grep redis

执行建立集群的命令

[root@localhost src]#  ./redis-trib.rb  create --replicas 1 172.16.176.5:7001 172.16.176.5:7002 172.16.176.5:7003 172.16.176.5:7004 172.16.176.5:7005 172.16.176.5:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: a6a7ea6a84126787d498b5858492b06af5bbc4a7 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: 8d3ac45e55d58695b20ec33022a1258270aa720c 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: 4b5dc237f7f4a1afce0840f267708d5a834845fc 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: 20a05046c2c91b5e752f9fe22ef384707094d2a8 127.0.0.1:7004
   replicates a6a7ea6a84126787d498b5858492b06af5bbc4a7
S: 35857a6fcb6a2bd22ddc5c4368ecab4a4306343d 127.0.0.1:7005
   replicates 8d3ac45e55d58695b20ec33022a1258270aa720c
S: 7ef29ea0a0299a8b700e00f2ea31efbc4e2f8a7a 127.0.0.1:7006
   replicates 4b5dc237f7f4a1afce0840f267708d5a834845fc
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: a6a7ea6a84126787d498b5858492b06af5bbc4a7 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: 8d3ac45e55d58695b20ec33022a1258270aa720c 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: 4b5dc237f7f4a1afce0840f267708d5a834845fc 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
M: 20a05046c2c91b5e752f9fe22ef384707094d2a8 127.0.0.1:7004
   slots: (0 slots) master
   replicates a6a7ea6a84126787d498b5858492b06af5bbc4a7
M: 35857a6fcb6a2bd22ddc5c4368ecab4a4306343d 127.0.0.1:7005
   slots: (0 slots) master
   replicates 8d3ac45e55d58695b20ec33022a1258270aa720c
M: 7ef29ea0a0299a8b700e00f2ea31efbc4e2f8a7a 127.0.0.1:7006
   slots: (0 slots) master
   replicates 4b5dc237f7f4a1afce0840f267708d5a834845fc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

可以看出有3个master 7001 7002 7003  对应的slave分别为7004 7005 7006

具体使用

  • 新增数据

    [root@localhost redis-cluser]# cd redis/src/
     [root@localhost src]# ./redis-cli -c -p 7001
     127.0.0.1:7001> set name wangchy
     -> Redirected to slot [5798] located at 127.0.0.1:7002
     OK
     127.0.0.1:7002> set age 18
     -> Redirected to slot [741] located at 127.0.0.1:7001
     OK
     127.0.0.1:7001> get name
     -> Redirected to slot [5798] located at 127.0.0.1:7002
     “wangchy”
     127.0.0.1:7002> get age
     -> Redirected to slot [741] located at 127.0.0.1:7001
     “18”

可以看到name进入了7002,age进入了7001,并且都能正常查询
因为7001的slave是7004,所以我停止7001,7004变为master,然后查询age

  • 停止7001

    [root@localhost src]# ./redis-cli -p 7001 shutdown
     [root@localhost src]#
     [root@localhost src]#
     [root@localhost src]#
     [root@localhost src]# ps -ef|grep redis
     root 12061 1  0 22:31 ?00:00:10 src/./redis-server 127.0.0.1:7002 [cluster]
     root 12066 1  0 22:31 ?00:00:10 src/./redis-server 127.0.0.1:7003 [cluster]
     root 12072 1  0 22:31 ?00:00:10 src/./redis-server 127.0.0.1:7004 [cluster]
     root 12077 1  0 22:31 ?00:00:10 src/./redis-server 127.0.0.1:7005 [cluster]
     root 12082 1  0 22:31 ?00:00:10 src/./redis-server 127.0.0.1:7006 [cluster]
     root 12839 11994  0 23:14 pts/200:00:00 ./redis-cli -c -p 7001
     root 12873 12852  0 23:16 pts/300:00:00 ./redis-cli -p 7004
     root 13029 11894  0 23:20 pts/000:00:00 grep redis
     127.0.0.1:7002> get age
     -> Redirected to slot [741] located at 127.0.0.1:7004
     “18”

    重启7001后 7004还是master 7001变为slave,可以通过info进行查看

java客户端连接redis集群

外部机器连接redis需要修改redis的配置

注释掉bind 127.0.0.1
修改protected-mode yes为no
  • java代码

import java.util.HashSet;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
public class RedisMaster {
    private static int index = 1;
    public static void main(String[] args) {
        HashSet<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
        jedisClusterNodes.add(new HostAndPort("172.16.176.5", 7001));
        jedisClusterNodes.add(new HostAndPort("172.16.176.5", 7002));
        jedisClusterNodes.add(new HostAndPort("172.16.176.5", 7003));
        jedisClusterNodes.add(new HostAndPort("172.16.176.5", 7004));
        jedisClusterNodes.add(new HostAndPort("172.16.176.5", 7005));
        jedisClusterNodes.add(new HostAndPort("172.16.176.5", 7006));
        JedisCluster jc = new JedisCluster(jedisClusterNodes);
        for (int i = 1; i <= 20; i++) {
            String key = generateKey();
            try {
                System.out.println(jc.set(key,
                        "1111111111111111111111111111111"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static String generateKey() {
        return String.valueOf(Thread.currentThread().getId()) + "_" + (index++);
    }

}

查看结果

[root@localhost src]# ./redis-cli -p 7001
127.0.0.1:7001> keys *
1) "1_1"
2) "1_12"
3) "1_5"
4) "1_9"
5) "1_16"
127.0.0.1:7001> 
[root@localhost src]# ./redis-cli -p 7002
127.0.0.1:7002> keys *
1) "1_7"
2) "1_13"
3) "1_14"
4) "1_3"
5) "1_10"
6) "1_8"
7) "1_4"
8) "1_17"
9) "1_18"
127.0.0.1:7002> 
[root@localhost src]# ./redis-cli -p 7003
127.0.0.1:7003> keys *
1) "1_2"
2) "1_11"
3) "1_19"
4) "1_6"
5) "1_15"
6) "1_20"

猜你喜欢

转载自blog.csdn.net/lixiaoxiong55/article/details/81607312