0%

Redis集群安装

看了几年前的Redis集群安装,那还是3.4的Redis,恍如时光

在Github上下载了一个项目,spring也没有用上,各种都是那人自己手动封装,包括解析xml,牛逼是牛逼,倒是跑起来也是贼麻烦的,一直卡在Redis的集群里,本来想让公司的运维装个Redis集群直接让我用, 结过,弄了两次,代码连接redis一直报错

也不太想麻烦别人了,就自己临时搭一个,还是招这几年前的笔记,哈哈哈,有点怀恋

1.配置

用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master、3 salve 环境。

worker1 worker2
192.168.0.179 7000
192.168.0.179 7001
192.168.0.179 7002
192.168.0.100 7003
192.168.0.100 7004
192.168.0.100 7005

2.下载,解压,编译安装

在worker1上

1
cd /usr/local
2
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
3
tar -zxvf redis-5.0.5.tar.gz
4
cd redis-5.0.5
5
make && make instal

注意make的时候可能会报错,

1
yum install gcc
2
make MALLOC=libc

3.创建节点,在worker1

创建文件

1
cd /usr/local
2
mkdir redis_cluster
3
cd redis_cluster
4
mkdir 7000
5
mkdir 7001
6
mkdir 7002
7
cp /usr/local/redis-5.0.5/redis.conf /usr/local/redis_cluster/7000/
8
cp /usr/local/redis-5.0.5/redis.conf /usr/local/redis_cluster/7001/
9
cp /usr/local/redis-5.0.5/redis.conf /usr/local/redis_cluster/7002/

分别修改三个文件夹里的配置文件,修改如下内容

1
port  7000      //端口7000,7002,7003       
2
bind 0.0.0.0     //自己建议修改为0.0.0.0
3
daemonize yes   //redis后台运行
4
pidfile  /var/run/redis_7000.pid    //pidfile文件对应7000,7001,7002
5
cluster-enabled  yes    //开启集群  把注释#去掉
6
cluster-config-file  nodes_7000.conf   //集群的配置,配置文件首次启动自动生成7000,7001,7002
7
cluster-node-timeout  15000  //请求超时  默认15秒,可自行设置
8
appendonly  yes //aof日志开启

启动节点的redis
/usr/local/bin/redis-server 这是在make & make install生成的

1
/usr/local/bin/redis-server redis_cluster/7000/redis.conf
2
/usr/local/bin/redis-server redis_cluster/7001/redis.conf
3
/usr/local/bin/redis-server redis_cluster/7002/redis.conf

检查 redis 启动情况

1
ps -ef | grep redi
2
root      61020      1  0 02:14 ?        00:00:01 redis-server 0.0.0.0:7000 [cluster]   
3
root      61024      1  0 02:14 ?        00:00:01 redis-server 0.0.0.0:7001 [cluster]   
4
root      61029      1  0 02:14 ?        00:00:01 redis-server 0.0.0.0:7002 [cluster]

4.再在worker2 上做相同的操作

5.启动集群

装的redis是5.x的版本,这里没有应用到redis-trib.rb,所以就不需要装ruby

1
cd /usr/local/bin
2
redis-cli --cluster create 192.168.0.100:7003 192.168.0.100:7004 192.168.0.100:7005 192.168.0.179:7000 192.168.0.179:7001 192.168.0.179:7002 --cluster-replicas 1

遇到

1
Can I set the above configuration? (type 'yes' to accept): yes

输入yes

6.校验,等运行完成

redis-cli --cluster check 192.168.0.179:7000

1
[root@worker1 src]# redis-cli --cluster check 192.168.0.179:7000
2
192.168.0.179:7000 (27bce53b...) -> 0 keys | 5462 slots | 1 slaves.
3
192.168.0.100:7004 (6b0173d9...) -> 0 keys | 5461 slots | 1 slaves.
4
192.168.0.100:7003 (9f15a932...) -> 0 keys | 5461 slots | 1 slaves.
5
[OK] 0 keys in 3 masters.
6
0.00 keys per slot on average.
7
>>> Performing Cluster Check (using node 192.168.0.179:7000)
8
M: 27bce53bda92341ca4a5c82c2361ab99f24c0b27 192.168.0.179:7000
9
   slots:[5461-10922] (5462 slots) master
10
   1 additional replica(s)
11
S: c7ebcd900fb7d9afb1980797acba45518cb7d877 192.168.0.100:7005
12
   slots: (0 slots) slave
13
   replicates 27bce53bda92341ca4a5c82c2361ab99f24c0b27
14
S: ed5256f8db1bf556a8dadbe8f2b07699507e17d9 192.168.0.179:7001
15
   slots: (0 slots) slave
16
   replicates 6b0173d925f70807a9081b7bc09bcd37be857342
17
S: 758609eaea88bac25b864f2badbab2171a68089b 192.168.0.179:7002
18
   slots: (0 slots) slave
19
   replicates 9f15a9329a9d0ec5c7fcb5abbba817730f0942f9
20
M: 6b0173d925f70807a9081b7bc09bcd37be857342 192.168.0.100:7004
21
   slots:[10923-16383] (5461 slots) master
22
   1 additional replica(s)
23
M: 9f15a9329a9d0ec5c7fcb5abbba817730f0942f9 192.168.0.100:7003
24
   slots:[0-5460] (5461 slots) master
25
   1 additional replica(s)
26
[OK] All nodes agree about slots configuration.
27
>>> Check for open slots...
28
>>> Check slots coverage...
29
[OK] All 16384 slots covered.

7.Jedis操作Redis集群

1
public class Demo {
2
    private static JedisCluster jedisCluster=null;
3
    private static Set<HostAndPort> hostAndPorts=null;
4
5
    public static  Set<HostAndPort> getHostAndPort(String hostAndPort){
6
        Set<HostAndPort> hap = new HashSet<HostAndPort>();
7
        String[] hosts = hostAndPort.split(",");
8
        String[] hs = null;
9
        for(String host:hosts){
10
            hs=host.split(":");
11
            hap.add(new HostAndPort(hs[0], Integer.parseInt(hs[1])));
12
        }
13
        return hap;
14
    }
15
16
    public static JedisCluster getJedisCluster(){
17
        GenericObjectPoolConfig gopc = new GenericObjectPoolConfig();
18
        gopc.setMaxTotal(32);
19
        gopc.setMaxIdle(4);
20
        gopc.setMaxWaitMillis(6000);
21
        hostAndPorts = getHostAndPort("192.168.0.100:7003,192.168.0.100:7004,192.168.0.100:7005,192.168.0.179:7000,192.168.0.179:7001,192.168.0.179:7002");
22
        jedisCluster = new JedisCluster(hostAndPorts, 2000, 2000, 3,null,gopc);
23
        return jedisCluster;
24
    }
25
26
    public static void main(String[] args) {
27
        jedisCluster = getJedisCluster();
28
        jedisCluster.set("name", "anthony");
29
        System.out.println(jedisCluster.get("name"));
30
    }
31
}