Redis主从模式及哨兵模式

一、硬件环境

假设有4台机,IP及主机名如下:

192.168.100.105 c1
192.168.100.110 c2
192.168.100.115 c3
192.168.100.120 c4

二、软件环境

操作系统:Ubuntu Server 18.04

三、安装单机版

1.下载源代码包

https://redis.io/

下载redis-6.2.6.tar

2.上传并解压

单机版先用c1机(192.168.100.105)测试,假定放在/home/目录

cd /home
rz
tar -xvf redis-6.2.6.tar
mv redis-6.2.6 redis

3.安装编译及运行的依赖组件

由于本文是使用源代码进行安装部署,所以需要先编译才能运行

apt install make
apt install make-guile
apt install pkg-config

4.编译

cd redis
make

编译后的运行文件都在/redis/src/目录中

5.运行

cd src
./redis-server

后台运行只需要在运行命令后面加上&,就是./redis-server &

6.测试

./redis-cli

能进入redis-cli指令工具即为成功,输入exit退出redis-cli指令工具

7.停止

如果是前台运行,只需要ctrl+c即可退出

如果是后台运行,运行以下命令:

./redis-cli shutdown

四、主从模式

1.上传安装

每台机都执行第三点的第1-4点,即执行到编译步骤

2.配置

假设c1机是主机,其它3台是从机。

(1) 编辑redis.conf

cd /home/redis
vim redis.conf

(2) 编辑绑定IP

* 该步骤每台机都要执行

查找bind 127.0.0.1 -::1,把这行改成bind 0.0.0.0

(3) 配置主机的IP和端口

* 该步骤只需要在从机执行,即c2、c3、c4机

在redis.conf中加入一行

replicaof 192.168.100.105 6379

* 新版是replicaof,旧版是slaveof

如果强迫症,可以设置从机为只读,在redis.conf中加入一行:

replica-read-only yes

* 新版是replica-read-only,旧版是slave-read-only

3.运行

先运行c1的Redis,再运行其余3台的。

cd /home/redis
src/redis-server redis.conf

后台运行只需要在运行命令后面加上&,就是src/redis-server redis.conf &

4.测试

在每台机执行以下命令:

src/redis-cli info replication

五、哨兵模式

* 以下步骤每台机都需要执行

1.编辑sentinel.conf

cd /home/redis
vim sentinel.conf

(1) 关闭保护模式

反注释# protected-mode no,也就是把“#”号删除,指保留protected-mode no

(2) 配置监视的主机

sentinel monitor mymaster 192.168.100.105 6379 2

其中的mymaster是主机的代名称(为了方便配置其它属性),可以自定义为其它名字,例如clothomaster

(3) 配置主机失联后切换的等待时间

该属性是指哨兵(sentinel)发现主机失联多久后自动切换其它从机作为主机。默认是30秒,这里改为10秒

sentinel down-after-milliseconds mymaster 10000

2.运行

cd /home/redis
src/redis-sentinel sentinel.conf

后台运行只需要在运行命令后面加上&,就是src/redis-sentinel sentinel.conf &

3.测试

src/redis-cli -h 192.168.100.105 -p 26379 sentinel sentinels mymaster

其中mymaster对应sentinel.conf文件中的主机代名称,如果里面自定义了其它名字,这里也跟着改

4.代码测试

(1) 引入依赖包

在pom.xml的<dependencies></dependencies>中加入以下配置:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.11.1</version>
</dependency>

由于JedisPoolConfig和JedisSentinelPool使用了apache.commons的commons-pool2组件,如果不引入,在SpringBoot运行的时候会报错。

(2) 代码访问Redis

通过哨兵模式操作Redis,就不能只绑定主机IP,而需要绑定所有哨兵入口。

public class RedisHandler
{
    public static void main(String[] args)
    {
        try
        {
            String masterName = "mymaster";
            
            HashSet<String> sentinels = new HashSet<>(Arrays.asList(
                    "192.168.100.105:26379",
                    "192.168.100.110:26379",
                    "192.168.100.115:26379",
                    "192.168.100.120:26379"
            ));
            
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(50);
            config.setMaxIdle(10);
            config.setMinIdle(5);

            JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, config);
            Jedis connection = jedisSentinelPool.getResource();
            Set<String> keySet = connection.keys("*");
            for (String key : keySet)
            {
                System.out.println(key);
            }

            jedisSentinelPool.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

六、自动化脚本

1.脚本代码

#!/bin/bash
#运行前需要先赋予运行权限:chmod u+x redis.sh
usage="Usage: $0 (start|stop)"

if [ $# -lt 1 ]; then
  echo $usage
  exit 1
fi

action=$1
iparray=(c1 c2 c3 c4)
path="/home/redis"

echo "$action redis replication"

for ip in ${iparray[*]}
do
    echo "ssh to $ip"
    case $action in
        "start")
        ssh $ip $path/src/redis-server $path/redis.conf &
        ;;

        "stop")
        ssh $ip $path/src/redis-cli -h $ip shutdown
        ;;
    esac
    sleep 1s
done

for ip in ${iparray[*]}
do
    echo "ssh to $ip sentinel"
    case $action in
        "start")
        ssh $ip $path/src/redis-sentinel $path/sentinel.conf &
        ;;

        "stop")
        pid=$(ssh $ip ps -ef | grep redis-sentinel | grep -v grep | awk '{print $2}')
        ssh $ip "kill -9 ${pid}"
        if [ $ip = "c1" ]; then
          kill -9 `ps -ef | grep redis-sentinel | grep -v grep|awk '{print $2}'`
        fi
        ;;
    esac
    sleep 1s
done

exit 0

保存为redis.sh文件

2.赋予运行权限

chmod u+x redis.sh

3.启动和停止

./redis.sh start
./redis.sh stop

4.测试

ps -ef |grep redis
原文地址:https://www.cnblogs.com/live41/p/15790836.html