Redis哨兵模式sentinel与spring-boot集成

Redis的哨兵模式是官方提供的一种高可用解决方案,而且配置非常简单。

安装Redis集群

本文使用redis-5.0.5,redis安装在/soft/redis目录下,需新建/soft/redis/data目录

主节点配置  
vim config/redis-6379.conf

# bind 127.0.0.1 
port 6379
protected-mode no
daemonize yes
pidfile "/var/run/redis_6379.pid"
dir "/soft/redis/data"
dbfilename "dump-6379.rdb"
logfile "log-6379.log"

从节点1配置
vim config/redis-6380.conf

# bind 127.0.0.1
port 6380
protected-mode no
daemonize yes
pidfile "/var/run/redis_6380.pid"
dir "/soft/redis/data"
dbfilename "dump-6380.rdb"
logfile "log-6380.log"

replicaof 192.168.4.176 6379

从节点2配置
vim config/redis-6381.conf

# bind 127.0.0.1
port 6381
protected-mode no
daemonize yes
pidfile "/var/run/redis_6381.pid"
dir "/soft/redis/data"
dbfilename "dump-6381.rdb"
logfile "log-6381.log"

replicaof 192.168.4.176 6379

配置说明

# bind 127.0.0.1  注释掉这配置,以便其他机器的能连接redis
protected-mode no 关闭保护模式,以便其他机器的能连接redis
daemonize后台模式启动
redis-v5版本使用replicaof替换旧的slaveof指令,想知道为什么要用replicaof替换slaveof,可以看看这些八卦文章

启动这3个节点,在/soft/redis目录下运行

redis-server config/redis-6379.conf
redis-server config/redis-6380.conf
redis-server config/redis-6381.conf

打开主节点客户端看看配置是否成功

redis-cli -p 6379
info replication

再配置3个哨兵,监控集群

哨兵节点1
vim config/redis-sentinel-26379.conf

port 26379
daemonize yes
pidfile "/var/run/redis-sentinel-26379.pid"
dir /tmp
logfile "log-sentinel-26379.log"
sentinel monitor mymaster 192.168.4.176 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

哨兵节点2
vim config/redis-sentinel-26380.conf

port 26380
daemonize yes
pidfile "/var/run/redis-sentinel-26380.pid"
dir /tmp
logfile "log-sentinel-26380.log"
sentinel monitor mymaster 192.168.4.176 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

哨兵节点3
vim config/redis-sentinel-26381.conf

port 26381
daemonize yes
pidfile "/var/run/redis-sentinel-26381.pid"
dir /tmp
logfile "log-sentinel-26381.log"
sentinel monitor mymaster 192.168.4.176 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

配置说明
monitor mymaster 192.168.4.176 6379 2 
    mymaster是master的名称,192.168.4.176是master主机ip。后面的2表示有2个sentinel认为master下线了,则线下master,建议设置为 sentinel节点数/2 + 1
down-after-milliseconds 
    发送ping请求给redis节点,在指定时间内未收到回复,则认为该节点应该被下线
parallel-syncs   
    在执行故障转移时,最多可以有多少个从节点同时对新的主服务器进行同步。

启动哨兵

redis-sentinel config/redis-sentinel-26379.conf
redis-sentinel config/redis-sentinel-26380.conf
redis-sentinel config/redis-sentinel-26381.conf

配置spring-boot

pom.xml中导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

application.properties加入两行配置

# 使用哨兵模式不能加以下两行配置,其他配置可以加
# spring.redis.host=192.168.4.176
# spring.redis.port=6379

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.4.176:26379, 192.168.4.176:26380, 192.168.4.176:26381
写一个测试类运行
@RunWith(SpringRunner.class)
@SpringBootTest
public class Sentinel001 {

    @Autowired
    RedisTemplate redisTemplate;

    @Test
    public void test001() throws Exception{
        while (true){
            String key = "time:" + new Date().getTime();
            redisTemplate.opsForValue().set(key, new Date().getTime());
            TimeUnit.MILLISECONDS.sleep(100L);
            System.out.println(redisTemplate.opsForValue().get(key));
        }

    }

}

然后杀掉master实例(端口号为6379的redis)的进程

ps -ef|grep redis
kill -9 11110

观察代码编辑器控制台输出,经过短暂的时间(大概是50s)后,程序重新运行正常

在6380和6381节点执行info replication,发现6381变成了主节点

查看下6380、6381的配置文件

cat config/redis-6380.conf
replicaof 192.168.4.176 6381
replicaof 变成了192.168.4.176 6381,而不是刚开始配置时的192.168.4.176 6379

cat config/redis-6381.conf 
replicaof 的配置被删除了

重启下6379这个redis实例

redis-server config/redis-6379.conf

6379变成了6381的从节点

有个比较坑爹的事情,RedisTemplate未实现读写分离,读写都是操作master节点。运行上面的代码,在3个redis客户端运行monitor发现,只有master会运行get、set命令,从节点只运行了set命令。

发布了51 篇原创文章 · 获赞 14 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/u010606397/article/details/94445132