Redis(五)Sentinel简介及SpringBoot访问

7 篇文章 0 订阅
7 篇文章 1 订阅

参考:https://blog.csdn.net/men_wen/article/details/72724406

1. Sentinel介绍
1.1 主从复制的问题

  Redis主从复制可将主节点数据同步给从节点,从节点此时有两个作用:

  一旦主节点宕机,从节点作为主节点的备份可以随时顶上来。扩展主节点的读能力,分担主节点读压力。

但是问题来了:

  • 一旦主节点宕机,从节点晋升成主节点,同时需要修改应用方的主节点地址,还需要命令所有从节点去复制新的主节点,整个过程需要人工干预。
  • 主节点的写能力受到单机的限制。
  • 主节点的存储能力受到单机的限制。

  第一个问题,我们接下来讲的Sentinel就可以解决。而后两个问题,Redis也给出了方案Redis Cluster。

1.2 Redis Sentinel的高可用

  Redis Sentinel是一个分布式架构,包含若干个Sentinel节点和Redis数据节点,每个Sentinel节点会对数据节点和其余Sentinel节点进行监控,当发现节点不可达时,会对节点做下线标识。

  如果被标识的是主节点,他还会选择和其他Sentinel节点进行“协商”,当大多数的Sentinel节点都认为主节点不可达时,他们会选举出一个Sentinel节点来完成自动故障转移工作,同时将这个变化通知给Redis应用方。

  整个过程完全自动,不需要人工介入,所以可以很好解决Redis的高可用问题。

  接下来我们就通过部署一个Redis Sentinel实例来了解整体框架。

sentinel 架构

2. SpringBoot 访问
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.0</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>       
public class JedisPoolUtil {
     
    private static JedisSentinelPool pool = null;
 
    public static Properties getJedisProperties() {
 
        Properties config = new Properties();
        InputStream is = null;
        try {
            is = JedisPoolUtil.class.getClassLoader().getResourceAsStream("cacheConfig.properties");
            config.load(is);
        } catch (IOException e) {
            logger.error("", e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    logger.error("", e);
                }
            }
        }
        return config;
    }
 
    /**
     * 创建连接池
     *
     */
    private static void createJedisPool() {
        // 建立连接池配置参数
        JedisPoolConfig config = new JedisPoolConfig();
        Properties prop = getJedisProperties();
        // 设置最大连接数
        config.setMaxTotal(StringUtil.nullToInteger(prop.getProperty("MAX_ACTIVE")));
        // 设置最大阻塞时间,记住是毫秒数milliseconds
        config.setMaxWaitMillis(StringUtil.nullToInteger(prop.getProperty("MAX_WAIT")));
        // 设置空间连接
        config.setMaxIdle(StringUtil.nullToInteger(prop.getProperty("MAX_IDLE")));
        // jedis实例是否可用
        boolean borrow = prop.getProperty("TEST_ON_BORROW") == "false" ? false : true;
        config.setTestOnBorrow(borrow);
        // 创建连接池
        // pool = new JedisPool(config, prop.getProperty("ADDR"), StringUtil.nullToInteger(prop.getProperty("PORT")), StringUtil.nullToInteger(prop.getProperty("TIMEOUT")));// 线程数量限制,IP地址,端口,超时时间
        //获取redis密码
        String password = StringUtil.nullToString(prop.getProperty("PASSWORD"));
 
        String masterName = "mymaster";
        // 添加所有sentinel的地址
        Set<String> sentinels = new HashSet<String>();
        sentinels.add("192.168.137.128:26379");
        sentinels.add("192.168.137.128:26380");
        sentinels.add("192.168.137.128:26381");
        pool = new JedisSentinelPool(masterName, sentinels, config);
    }
 
    /**
     * 在多线程环境同步初始化
     */
    private static synchronized void poolInit() {
        if (pool == null)
            createJedisPool();
    }
 
    /**
     * 获取一个jedis 对象
     *
     * @return
     */
    public static Jedis getJedis() {
        if (pool == null)
            poolInit();
        return pool.getResource();
    }
 
    /**
     * 释放一个连接
     *
     * @param jedis
     */
    public static void returnRes(Jedis jedis) {
        pool.returnResource(jedis);
    }
 
    /**
     * 销毁一个连接
     *
     * @param jedis
     */
    public static void returnBrokenRes(Jedis jedis) {
        pool.returnBrokenResource(jedis);
    }
     
     
    public static void main(String[] args){
        Jedis jedis=getJedis();
         
    }
 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值