小工具知识网

当前位置: 首页 > 行业知识 > Redis冲冲冲——Redis的主从复制,哨兵模式以及SpringBoot的配置

行业知识

Redis冲冲冲——Redis的主从复制,哨兵模式以及SpringBoot的配置

2026-10-29 19:22:34 来源:互联网转载

目录

  • 引出
  • Redis的主从复制,哨兵模式以及SpringBoot的配置
    • Redis的主从复制
    • Redis的哨兵模式
    • SpringBoot配置
    • 缓存三兄弟:缓存击穿、穿透、雪崩
      • 缓存击穿
      • 缓存穿透
      • 缓存雪崩
      • ���结

        引出

        Redis冲冲冲——Redis的主从复制,哨兵模式以及SpringBoot的配置


        Redis的主从复制,哨兵模式以及SpringBoot的配置

        Redis的主从复制

        问题:Redis 服务器一定不会崩溃?不能 ,整一个备用的

        按照上述的图分析,即便主服务器出问题,那么至少还有3台从服务器可以提供服务

        主从复制的作用:

        (1)、数据实时热备

        (2)、故障恢复(主出问题,从顶上)

        (3)、负载均衡

        (4)、读写分离

        (5)、提升系统的高可用

        常见的主从结构:

        1主1从、1主多从、多主多从


        1主2从集群:设定场景:1主2从 主(6379),从(6380,6381)

        (1)主从复制准备

        准备3份配置文件:

        redis6379.conf

        daemonize noprotected-mode norequirepass 150316#指定 从节点来连接时,需要使用的密码masterauth 150316#指定 服务器的IP地址,便于从节点来连接slave-announce-ip 192.168.3.23#指定 服务器的端口号,便于从节点来连接slave-announce-port 6379#0.0.0.0在服务器的环境中,指的就是服务器上所有的ipv4地址bind 0.0.0.0

        redis6380.conf

        daemonize no#关闭保护模式protected-mode no #设置密码       requirepass 150316#集群中其它客户端设置密码后,这里要相应的指定masterauth 150316#指定主节点的IP地址 以及端口slaveof 192.168.3.23 6379slave-announce-ip 192.168.3.23slave-announce-port 6380#0.0.0.0在服务器的环境中,指的就是服务器上所有的ipv4地址bind 0.0.0.0

        redis6381.conf

        daemonize no#关闭保护模式protected-mode no #设置密码       requirepass 150316#集群中其它客户端设置密码后,这里要相应的指定masterauth 150316#指定主节点的IP地址 端口slaveof 192.168.3.23 6379slave-announce-ip 192.168.3.23slave-announce-port 6381#0.0.0.0在服务器的环境中,指的就是服务器上所有的ipv4地址bind 0.0.0.0

        启动1主2从:

        docker run --name redis01 -p 6379:6379 -v /root/redis/redis6379.conf:/usr/local/etc/redis/redis.conf -v /root/redis/data6379:/data -d redis:latest redis-server /usr/local/etc/redis/redis.conf
        docker run --name redis02 -p 6380:6379 --privileged=true -v /root/redis/redis6380.conf:/usr/local/etc/redis/redis.conf -v /root/redis/data6380:/data -d redis:latest redis-server /usr/local/etc/redis/redis.conf
        docker run --name redis03 -p 6381:6379  --privileged=true -v /root/redis/redis6381.conf:/usr/local/etc/redis/redis.conf -v /root/redis/data6381:/data -d redis:latest redis-server /usr/local/etc/redis/redis.conf

        redis主从复制的原理:

        主从结构:先进行全量复制,然后进行增量复制

        (1)、当slave和master建立联系之后,slave节点发送psync至master上,master在收到该命令之后,开始使用bgsave进行RDB快照备份(当多个slave同时发送psync命令,主节点只备份一次)

        (2)、master节点备份完成之后,将RDB快照文件推送至所有的slave从节点,slave从节点读取快照,并删除自身所有的旧数据,完成全量复制

        (3)、应用程序针对master的所有写的操作,将被master以命令的方式,推送所有slave从节点上,从节点完成增量复制

        如果主节点崩溃了,怎么办?

        Redis的哨兵模式

        问题:配置的1主2从,主机可能会宕机?它宕机之后咋办?

        多个哨兵服务器构成一个哨兵系统,哨兵系统监控主从服务器的执行状态,当主节点下线时,哨兵可采取投票推荐机制,自动从剩余的从服务器中 ,选择出最新的主节点!

        具体操作:

        (1)、哨兵系统监控主从节点,每个节点都指定一个哨兵

        (2)、哨兵系统主要监控主节点,当主节点宕机,主节点的哨兵会不断尝试重新连接主节点,当多次连接之后,仍旧连接失败,主节点哨兵将主观意识认为主节点已经宕机,通知哨兵系统,让哨兵系统中所有哨兵试图再去连接主节点,当所有哨兵都连接不上,哨兵系统认为主节点真正的宕机了!

        (3)、哨兵系统将针对剩余的所有从节点进行“选举投票”,票数最多的从节点将会被 哨兵系统推举为新的“主节点”,其他节点默认为从节点。

        (为了快速的得到投票结果,哨兵系统最好是:单数)

        在/root/redis目录下,创建3个哨兵配置:

        sentinel26379.conf

        port 26379dir "/data"logfile "sentinel-26379.log"sentinel monitor mymaster 192.168.3.23 6379 2sentinel down-after-milliseconds mymaster 1000sentinel failover-timeout mymaster 60000sentinel auth-pass mymaster 150316

        port 哨兵的端口

        dir 哨兵的数据目录

        logfire 哨兵的日志文件

        sentinel monitor 哨兵监控 192.168.145.128 是主节点的IP地址 2代表(只要有2个哨兵连接不上,就认为主节点宕机)

        sentinel auth-pass 主节点的密码

        sentinel26380.conf

        port 26380dir "/data"logfile "sentinel-26380.log"sentinel monitor mymaster 192.168.3.23 6379 2sentinel down-after-milliseconds mymaster 10000sentinel failover-timeout mymaster 60000sentinel auth-pass mymaster 150316

        sentinel26381.conf

        port 26381dir "/data"logfile "sentinel-26381.log"sentinel monitor mymaster 192.168.3.23 6379 2sentinel down-after-milliseconds mymaster 10000sentinel failover-timeout mymaster 60000sentinel auth-pass mymaster 150316

        启动哨兵系统

        —net=host 容器内部什么端口 ,容器外部也使用什么端口!替代:-p 26379:26379

        docker run --net=host --restart=always --privileged=true --name sentinel-26379 -v /root/redis/sentinel26379.conf:/etc/redis/sentinel.conf -v /root/redis/sentinel-26379-data:/data -d redis:latest redis-sentinel /etc/redis/sentinel.conf
        docker run --net=host --restart=always --privileged=true --name sentinel-26380 -v /root/redis/sentinel26380.conf:/etc/redis/sentinel.conf -v /root/redis/sentinel-26380-data:/data -d redis:latest redis-sentinel /etc/redis/sentinel.conf
        docker run --net=host --restart=always --privileged=true --name sentinel-26381 -v /root/redis/sentinel26381.conf:/etc/redis/sentinel.conf -v /root/redis/sentinel-26381-data:/data -d redis:latest redis-sentinel /etc/redis/sentinel.conf
        firewall-cmd --zone=public --add-port=26379/tcp --add-port=26380/tcp --add-port=26381/tcp --permanent#重载防火墙的端口firewall-cmd --reload

        下载windows的Redis安装包:https://github.com/MicrosoftArchive/redis/releases

        查看哨兵:


        把redis01停掉,模拟主服务器宕机

        哨兵系统的作用:防止redis主从集群中的主节点宕机的问题

        检查哨兵系统是否正常:

        redis-cli -h 192.168.3.23 -p 26379 sentinel master mymasterredis-cli -h 192.168.3.23 -p 26380 sentinel master mymasterredis-cli -h 192.168.3.23 -p 26381 sentinel master mymaster

        如果能正常打印出 主redis的信息,就是成功的

        SpringBoot配置

        (1)、导入依赖

            io.lettuce    lettuce-core

        (2)、application.yml

        server:    port: 8080spring:    redis:        jedis:            pool:                max-active: 10 #最大连接数10根,默认8根                max-wait: 2000ms #2S都没获得到连接,抛出异常  -1表示永不超时                min-idle: 0 #最小连接空闲数                max-idle: 5 #最大连接空闲数        sentinel:            nodes: 192.168.3.23:26379,192.168.3.23:26380,192.168.3.23:26381            master: mymaster        password: 150316

        修改配置类

        @Configurationpublic class RedisConfigure {    /**     * 哨兵配置(1)取得详细的哨兵节点     */    @Value("#{'${spring.redis.sentinel.nodes}'.split(',')}")    private List nodes;    @Value("${spring.redis.sentinel.master}")    private String master;    @Value("${spring.redis.password}")    private String password;    /**     * 哨兵配置(2)哨兵配置     */    @Bean    public RedisSentinelConfiguration redisSentinelConfiguration() {        Set sentinelNodes = nodes.stream().collect(Collectors.toSet());        RedisSentinelConfiguration configuration = new RedisSentinelConfiguration(master, sentinelNodes);        configuration.setPassword(RedisPassword.of(password));        return configuration;    }    /**     * 哨兵配置(3)配置连接工厂     */    @Bean    public LettuceConnectionFactory lettuceConnectionFactory() {        return new LettuceConnectionFactory(redisSentinelConfiguration());    }    /**     * 哨兵配置(4):配置redisTemplate     * @return     */    @Bean    @Scope("singleton")    public RedisTemplate redisTemplate(){        RedisTemplate redisTemplate = new RedisTemplate();        //配置连接工厂        redisTemplate.setConnectionFactory(lettuceConnectionFactory());        //针对Key进行序列化        StringRedisSerializer krs = new StringRedisSerializer();        redisTemplate.setKeySerializer(krs);        redisTemplate.setHashKeySerializer(krs);        //针对Value进行序列化        Jackson2JsonRedisSerializer vrs = new Jackson2JsonRedisSerializer(Object.class);        ObjectMapper om = new ObjectMapper();        //设定Jackson工具,可以操作Value对象的所有的属性        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);        vrs.setObjectMapper(om);        redisTemplate.setValueSerializer(vrs);        redisTemplate.setHashValueSerializer(vrs);        //redisTemplate 在对象的属性设置之后使用        redisTemplate.afterPropertiesSet();        return redisTemplate;    }}

        哨兵配置到此结束

        缓存三兄弟:缓存击穿、穿透、雪崩

        缓存击穿

        缓存击穿:redis中没有,但是数据库有

        顺序:先查缓存,判断缓存是否存在;如果缓存存在,直接返回数据;如果缓存不存在,則查询数据库,将数据库的数据存入到缓存

        解决方案:将热点数据设置过期时间长一点;针对数据库的热点访问方法上分布式锁;

        缓存穿透

        缓存穿透:redis中没有,数据库也没有

        解决方案:

        (1)将不存在的key,在redis设置值为null;

        (2)使用布隆过滤器;

        原理:https://zhuanlan.zhihu.com/p/616911933

        布隆过滤器:

        如果确认key不存在于redis中,那么就一定不存在;

        它说key存在,就有可能存在,也可能不存在! (误差)

        布隆过滤器

        1、根据配置类中的 key的数量 ,误差率,计算位图数组【二维数组】

        2、通过布隆过滤器存放key的时候,会计算出需要多少个hash函数,由hash函数算出多少个位图位置需要设定为1

        3、查询时,根据对应的hash函数,判断对应的位置值是否都为1;如果有位置为0,则表示key一定不存在于该redis服务器中;如果全部位置都为1,则表示key可能存在于redis服务器中;

        缓存雪崩

        缓存雪崩:

        Redis的缓存雪崩是指当Redis中大量缓存数据同时失效或者被清空时,大量的请求会直接打到数据库上,导致数据库瞬时压力过大,甚至宕机的情况。

        造成缓存雪崩的原因主要有两个:

        1.相同的过期时间:当Redis中大量的缓存数据设置相同的过期时间时,这些数据很可能会在同一时间点同时失效,导致大量请求直接打到数据库上。

        2.缓存集中失效:当服务器重启、网络故障等因素导致Redis服务不可用,且缓存数据没有自动进行容错处理,当服务恢复时大量的数据同时被重新加载到缓存中,也会导致大量请求直接打到数据库上。

        预防缓存雪崩的方法主要有以下几种:

        1.设置不同的过期时间:可以将缓存数据的过期时间分散开,避免大量缓存数据在同一时间点失效。

        2.使用加锁:可以将所有请求都先进行加锁操作,当某个请求去查询数据库时,如果还没有加载到缓存中,则只让单个线程去执行加载操作,其他线程等待该线程完成后再次进行判断,避免瞬间都去访问数据库从而引起雪崩。

        3.提前加载预热:在系统低峰期,可以提前将部分热点数据加载到缓存中,这样可以避免在高峰期缓存数据失效时全部打到数据库上。

        4.使用多级缓存:可以在Redis缓存之上再使用一层缓存,例如本地缓存等,当Redis缓存失效时,还能够从本地缓存中获取数据,避免直接打到数据库上。

        本地缓存:ehcache oscache spring自带缓存 持久层框架的缓存


        总结

        Redis冲冲冲——Redis的主从复制,哨兵模式以及SpringBoot的配置

上一篇:猪肉烹饪秘诀大揭秘,轻松打造令人垂涎的猪肉佳肴!

下一篇:服务器里面怎么上网卡?