Redis集群安装

时间:2024-02-01 22:39:20

Redis集群安装


title: Redis集群安装
date: 2019-07-02 20:55:00
categories:

  • Linux

    看了几年前的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上

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

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

yum install gcc
make MALLOC=libc

3.创建节点,在worker1

创建文件

cd /usr/local
mkdir redis_cluster
cd redis_cluster
mkdir 7000
mkdir 7001
mkdir 7002
cp /usr/local/redis-5.0.5/redis.conf /usr/local/redis_cluster/7000/
cp /usr/local/redis-5.0.5/redis.conf /usr/local/redis_cluster/7001/
cp /usr/local/redis-5.0.5/redis.conf /usr/local/redis_cluster/7002/

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

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

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

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

检查 redis 启动情况

ps -ef | grep redi
root      61020      1  0 02:14 ?        00:00:01 redis-server 0.0.0.0:7000 [cluster]   
root      61024      1  0 02:14 ?        00:00:01 redis-server 0.0.0.0:7001 [cluster]   
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

cd /usr/local/bin
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

遇到

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

输入yes

6.校验,等运行完成

redis-cli --cluster check 192.168.0.179:7000

[root@worker1 src]# redis-cli --cluster check 192.168.0.179:7000
192.168.0.179:7000 (27bce53b...) -> 0 keys | 5462 slots | 1 slaves.
192.168.0.100:7004 (6b0173d9...) -> 0 keys | 5461 slots | 1 slaves.
192.168.0.100:7003 (9f15a932...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.0.179:7000)
M: 27bce53bda92341ca4a5c82c2361ab99f24c0b27 192.168.0.179:7000
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: c7ebcd900fb7d9afb1980797acba45518cb7d877 192.168.0.100:7005
   slots: (0 slots) slave
   replicates 27bce53bda92341ca4a5c82c2361ab99f24c0b27
S: ed5256f8db1bf556a8dadbe8f2b07699507e17d9 192.168.0.179:7001
   slots: (0 slots) slave
   replicates 6b0173d925f70807a9081b7bc09bcd37be857342
S: 758609eaea88bac25b864f2badbab2171a68089b 192.168.0.179:7002
   slots: (0 slots) slave
   replicates 9f15a9329a9d0ec5c7fcb5abbba817730f0942f9
M: 6b0173d925f70807a9081b7bc09bcd37be857342 192.168.0.100:7004
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 9f15a9329a9d0ec5c7fcb5abbba817730f0942f9 192.168.0.100:7003
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

7.Jedis操作Redis集群

public class Demo {
    private static JedisCluster jedisCluster=null;
    private static Set<HostAndPort> hostAndPorts=null;

    public static  Set<HostAndPort> getHostAndPort(String hostAndPort){
        Set<HostAndPort> hap = new HashSet<HostAndPort>();
        String[] hosts = hostAndPort.split(",");
        String[] hs = null;
        for(String host:hosts){
            hs=host.split(":");
            hap.add(new HostAndPort(hs[0], Integer.parseInt(hs[1])));
        }
        return hap;
    }

    public static JedisCluster getJedisCluster(){
        GenericObjectPoolConfig gopc = new GenericObjectPoolConfig();
        gopc.setMaxTotal(32);
        gopc.setMaxIdle(4);
        gopc.setMaxWaitMillis(6000);
        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");
        jedisCluster = new JedisCluster(hostAndPorts, 2000, 2000, 3,null,gopc);
        return jedisCluster;
    }

    public static void main(String[] args) {
        jedisCluster = getJedisCluster();
        jedisCluster.set("name", "anthony");
        System.out.println(jedisCluster.get("name"));
    }
}

更多可以访问我的博客:https://yanganlin.com

posted on 2019-07-02 21:13 木木在唱歌 阅读(...) 评论(...) 编辑 收藏