快速入门Redis Sentinel架构(哨兵)

一、什么是Sentinel(哨兵)

Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器。

注意:配置哨兵必须实现Redis读写分离如何配置请看:https://blog.csdn.net/qq_43791724/article/details/104908590

二、Sentinel(哨兵)执行流程

 三、配置Sentinel(哨兵)

  修改文件: sentinel.conf(三台节点都需要配置)

#修改bind配置,每台机器修改为自己对应的主机名

bind node01 

#配置sentinel服务后台运行

daemonize yes

#修改三台机器监控的主节点,现在主节点是node01服务器

sentinel monitor mymaster node01 6379 2

启动测试:(三台搜需要启动)

src/redis-sentinel sentinel.conf

ps -ef |grep redis 

四、测试redis的sentinel(哨兵)代码开发

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import java.util.Arrays;
import java.util.HashSet;

public class RedisDemo01 {
    public static void main(String[] args) {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(10);
        config.setMaxIdle(5);
        config.setMinIdle(5);
        //添加哨兵信息
        HashSet<String> lists = new HashSet<>(Arrays.asList("node01:26379","node02:26379","node03:26379"));
        JedisSentinelPool pool = new JedisSentinelPool("mymaster", lists, config);
        // 获取连接对象
        Jedis resource = pool.getResource();
        
        resource.set("mykey", "myvalue");
        // 控制台输入myvalue 说明执行成功
        System.out.println(resource.get("mykey"));

    }
}

发布了88 篇原创文章 · 获赞 99 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_43791724/article/details/104920804